【发布时间】:2018-05-23 08:37:07
【问题描述】:
select type, created_at::date , count(type)
from txns
group by created_at::date, type
order by created_at::date;
上面的 SQL 查询给出以下输出:
type | created_at | count
---------+------------+-------
full | 2017-05-20 | 2
virtual | 2017-05-20 | 2
full | 2017-05-21 | 1
virtual | 2017-05-21 | 1
full | 2017-05-22 | 3
如何将上述结果按created_at 分组以获得以下输出:
created_at | full_count | virtual_count
------------+-----------------
2017-05-20 | 2 | 2
2017-05-21 | 1 | 1
2017-05-22 | 3 | 0
我想在一行中按created_at 获取full 和virtual 类型计数。
【问题讨论】:
标签: sql postgresql postgresql-9.5