我在SQL中具有以下数据:
date code price
2019-01-22 HDBUY 18.99
2019-01-22 HDRENT 3.99
2019-01-22 SDBUY 13.99
2019-01-22 SDRENT 2.99
这将如何完成?目前我正在做:
SELECT
date,
CASE WHEN s.code = <all the differe case statements>
FROM table
GROUP BY date
但这给了我除第一个以外的所有空条目:
date HDBUY SDBUY HDRENT SDRENT
2019-01-22 18.99 NULL NULL NULL
答案 0 :(得分:0)
以下查询应为您提供结果:
select
date,
sum(case when code = 'HDBUY' then price end) as hdbuy,
sum(case when code = 'HDRENT' then price end) as hdrent,
sum(case when code = 'SDBUY' then price end) as sdbuy,
sum(case when code = 'SDRENT' then price end) as sdrent
from my_table
group by date