我想从表中的两列中获得这些列中值的统一计数。
例如,两列是:
表:报告
| type | place |
-----------------------------------------
| one | home |
| two | school |
| three | work |
| four | cafe |
| five | friends |
| six | mall |
| one | work |
| one | work |
| three | work |
| two | cafe |
| five | cafe |
| one | home |
如果我做:
从报告中选择类型、计数(*)
按类型分组
I get:
| type | count |
-----------------------------
| one | 4 |
| two | 2 |
| three | 2 |
| four | 1 |
| five | 2 |
| six | 1 |
我试图得到这样的结果:(最右边的一列将我的类型分组在一起,多列包含每个位置的计数值)
我得到:
| type | home | school | work | cafe | friends | mall |
-----------------------------------------------------------------------------------------
| one | 2 | | 2 | | | |
| two | | 1 | | 1 | | |
| three | | | 2 | | | |
| four | | | | 1 | | |
| five | | | | 1 | 1 | |
| six | | | | | | 1 |
这将是对每个地方运行如上所述的计数的结果,如下所示:
SELECT type, count(*) from reports where place = 'home'
group by type
SELECT type, count(*) from reports where place = 'school'
group by type
SELECT type, count(*) from reports where place = 'work'
group by type
SELECT type, count(*) from reports where place = 'cafe'
group by type
SELECT type, count(*) from reports where place = 'friends'
group by type
SELECT type, count(*) from reports where place = 'mall'
group by type
postgresql 可以做到这一点吗?
提前致谢。