从这个输入到输出的最佳方式是什么?
输入:
date id name info price qty
-----------------------------------------------
20140523 10036 ABC B 12 100
20140523 10036 ABC S 13 75
20140523 10034 XYZ B 22 56
20140523 10034 XYZ S 24 41
20151023 10037 PQR B 30 45
20151023 10037 PQR S 5 20
输出:
date id name b_price b_qty s_price s_qty
---------------------------------------------------------
20140523 10036 ABC 12 100 13 75
20140523 10034 XYZ 22 56 24 41
20140523 10037 PQR 30 45 5 20
答案 0 :(得分:1)
使用条件聚合:
select date,
id,
name,
sum(case info = 'b' then price end) as bprice,
sum(case info = 'b' then qty end) as bqty,
sum(case info = 's' then price end) as sprice,
sum(case info = 's' then qty end) as sqty
from tablename
group by date, id, name