我有一个这样的表,让我们称之为主
位置 | 项目 | 价格 | |
---|---|---|---|
l1 | 第1项 | 3.00 | |
l1 | 第2项 | <1.00>||
l2 | 第1项 | 3.00 | |
l2 | 第2项 | <1.50>||
l2 | 第3项 | <2.00>||
l3 | 第4项 | 5.00 | |
l3 | 第5项 | 5.00 |
似乎您希望每个项目和位置都有一个结果行,无论该对在主表中是否有条目。因此,首先使用交叉连接生成这些行。只有在外部连接您的原始数据。
with main as (<subquery to create main>),
items as (select distinct item from main),
locations as (select distinct location from main)
select i.item, l.location, m.price
from items i
cross join locations l
left join main m on m.item = i.item and m.location = l.location
order by i.item, l.location;