我有一个相当简单的查询,可以在DB2中正确执行,但是在弄清楚如何对结果进行分组以给我2列时遇到麻烦 替换2个不同行的列的值。换句话说,对于每种产品,我都会得到2行(一个具有有效价格,一个具有临时价格) 但我想这样做,以便获得一个不同的产品行,其中每种价格类型和价格都有一个列
查询:
select distinct grouping, body, fabric,color,thread,detail, category,p.priceType,p.price
from ordering offs
inner join pricing p
on offs.body = p.bodyp
where priceType in ('Active','Temporary')
and offs.category in ('A','B','C');
我得到的是什么
grouping | body | fabric | color | thread | detail | category | p.priceType | price
-----------------------------------------------------------------------------------------------------
ABC 123 1234 Blue 1.1 1 TEXTILE Active 594.00
ABC 123 1234 Blue 1.1 1 TEXTILE Temporary 560.00
ABC 123 1234 Red 0.5 0 TEXTILE Active 584.00
ABC 123 1234 Red 0.5 0 TEXTILE Temporary 550.00
ABC 123 1234 Grn 3.3 12 TEXTILE Active 594.00
ABC 123 1234 Grn 3.3 12 TEXTILE Temporary 560.00
我想要得到什么:
grouping | body | fabric | color | thread | detail | category | ActivePrice | TemporaryPrice
------------------------------------------------------------------------------------------------------------
ABC 123 1234 Blue 1.1 1 TEXTILE 594.00 560.00
ABC 123 1234 Red 0.5 0 TEXTILE 584.00 550.00
ABC 123 1234 Grn 3.3 12 TEXTILE 594.00 560.00
答案 0 :(得分:1)
一种简单的方法使用条件聚合:
select grouping, body, fabric, color, thread, detail, category,
max(case when p.priceType = 'Active' then p.price end) as active_price,
max(case when p.priceType = 'Temporary' then p.price end) as temporary_price
from ordering offs inner join
pricing p
on offs.body = p.bodyp
where priceType in ('Active', 'Temporary') and
offs.category in ('A', 'B', 'C')
group by grouping, body, fabric, color, thread, detail, category;