以下问题需要SQL查询

时间:2019-02-14 05:22:07

标签: sql postgresql

我有一个类似

的表格
mss | circle |  psr  |   date
-----+--------+-------+----------
 m1  | mum    |  99.6 | 20190211
 m2  | mum    |  98.6 | 20190211
 m3  | mum    |  97.6 | 20190211
 m1  | mum    |   9.6 | 20190212
 m2  | mum    |  93.6 | 20190212
 m3  | mum    |    46 | 20190212
 m1  | mum    |  9.36 | 20190213
 m2  | mum    | 99.36 | 20190213
 m3  | mum    | 29.36 | 20190213

需要输出为 enter image description here

2 个答案:

答案 0 :(得分:2)

使用条件聚合

select mss,circle, max(case when date='20190211' then psr end) date11th,
max(case when date='20190212' then psr end) date12th,
max(case when date='20190213' then psr end) date13th
from table group by mss,circle

答案 1 :(得分:0)

下面是查询-

select t.mss, t.circle,
max(case when t.date=20190211 then t.psr else 0 end) as date11th,
max(case when t.date=20190212 then t.psr else 0 end) as date12th,
max(case when t.date=20190213 then t.psr else 0 end) as date13th
from table t group by t.mss, t.circle;