gpt4 book ai didi

sql - SQL-计算计数百分比(列)

转载 作者:行者123 更新时间:2023-12-04 09:23:45 26 4
gpt4 key购买 nike

我有以下一段代码,它计算事件列中出现的次数。

SELECT event, count(event) as event_count   
FROM event_information
group by event

event event_count
a 34
b 256
c 45
d 117
e 3


我希望能够像这样计算每一行的百分比。

event   event_count event_percent
a 34 7.47
b 256 56.26
c 45 9.89
d 117 25.71
e 3 0.66

最佳答案

SELECT event, 
count(event) as event_count,
count(event) * 100.0 / (select count(*) from event_information) as event_percent
FROM event_information
group by event

关于sql - SQL-计算计数百分比(列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37303779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com