繁体   English   中英

SQL 在查询计数结果中使用列值作为列名

[英]SQL Use column values as column names in query count results

我有一个表,其中包括tdatenode_name作为列等。

tdate                   node_name
2021-04-15 15:37:04.000 node1
2021-04-15 15:37:04.000 node2
2021-04-15 15:37:04.000 node1
2021-04-15 15:37:04.000 node3
2021-04-15 15:37:04.000 node1

我想 plot 按tdate计算node_name组的实例数。 所以做这样的事情

SELECT
date_trunc('minute',tdate),
node_name,
count(1)
GROUP BY 1,2

我明白了

_col0                    node_name      _col2
2021-04-15 15:37:00.000   node1           3
2021-04-15 15:37:00.000   node2           1
2021-04-15 15:37:00.000   node3           1

我想看到的是

_col0                   node1 node2 node3
2021-04-15 15:37:00.000 3     1     1 

我知道我将sum(node_name = 'node1') as 'node1'等,但问题是根据查询可能有数百个唯一节点名称,并且使用sum()count(case )语句不可行. 是否有某种方法可以在不明确包含每个 node_name 值的情况下对其进行概括? sum(node_name) as node_name )?

You can use the histogram function that will return a map of your columns with the respective counts, see the doc here: https://prestodb.io/docs/current/functions/aggregate.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM