我要查表中的发货数量,价格,金额,包装费用。但是表中的发货数量中不包括包装费用,但是实际上表中发货数量与包装费是在一个表中的,只是存货编码不同。现在数据表的结构是这样的
存货编码,发货数量,价格,金额
001 ,10 ,100,1000,
002 ,22 ,300,6600。001为包装物,002为销售的产品1,怎么写SQL让
商品,发货数量,价格,金额,包装费用
产品1,22 ,300 ,7600,1000
存货编码,发货数量,价格,金额
001 ,10 ,100,1000,
002 ,22 ,300,6600。001为包装物,002为销售的产品1,怎么写SQL让
商品,发货数量,价格,金额,包装费用
产品1,22 ,300 ,7600,1000
商品,
sum(case 存货编码 when '002' then 发货数量 else 0 end) as 发货数量,
max(case 存货编码 when '002' then 价格 else 0 end) as 价格,
sum(金额) as 金额,
sum(case 存货编码 when '001' then 金额 else 0 end) as 包装费用
from
表
group by
商品
如果只有两条记录,并且确认第一条是包装费用,那么2楼的code是对的
min(商品) as 商品,
sum(case 存货编码 when '002' then 发货数量 else 0 end) as 发货数量,
max(case 存货编码 when '002' then 价格 else 0 end) as 价格,
sum(金额) as 金额,
sum(case 存货编码 when '001' then 金额 else 0 end) as 包装费用
from
表