在PostgreSQL中查找分组数据的比率
我想在按QY和列2分组数据后找到比率。
在每个季度的比例=计算(B/(B+C))的计数中,即1-2021中的比率为2000/(2000+3000)= 0.4。
分组数据如下:
QY | 第2列 | 计数 |
---|---|---|
1-2021 | A | 1000 |
1-2021 | B | 2000 |
1-2021 | C | 3000 |
2-2021 | A | 5000 |
2-2021 | B | 3000 |
2-2021 | C | 4000 |
您可以帮助我完成Postgres中的脚本?
I'd like to find ratios after grouping data by QY and Column2.
In every quarter ratio = count of( B/(B+C) ) to be calculated, i.e. in 1-2021 the ratio is 2000/(2000+3000)=0.4.
Grouped data is as follows:
QY | Column 2 | Count |
---|---|---|
1-2021 | A | 1000 |
1-2021 | B | 2000 |
1-2021 | C | 3000 |
2-2021 | A | 5000 |
2-2021 | B | 3000 |
2-2021 | C | 4000 |
Could you kindly help me with the script in postgres?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用总和和过滤器获取所需的记录。这样的事情应该可以工作:
CTE只是为了模拟现有表,仅用于测试。
Use SUM and FILTER to get the records that you need. Something like this should work:
The CTE is there just to simulate an existing table, just for testing.