``` SELECT s.date , sum(s.zhangsan) , sum(s.lisi) , sum(s.zhangsan) + sum(s.lisi) 'count1+count2' FROM ( SELECT date , CASE WHEN NAME = 'zhangsan' THEN count ELSE 0 END 'zhangsan' , CASE WHEN NAME = 'lisi' THEN count ELSE 0 END 'lisi' FROM x_table ) s GROUP BY s.date;
```
![]( )
PulpFunction
2018-10-19 17:10:49 +08:00
是变成'20181018' | ( 100,120 ) | 220
dovme
2018-10-19 17:11:17 +08:00
应该是可以的,你试一下
Leigg
2018-10-19 17:13:32 +08:00
要用到子查询,子查询的条数和要 count 的 name 的个数相关,所以你这个需求仍然需要多条 sql 完成。追求一个 sql 毫无意义。
Select Date date, Sum(case when name=‘ laowang ’ then count else 0 end) count1, Sum(...) count2, Sum(...)+Sum(...) count1and2 From X_table Group by Date