gpt4 book ai didi

sql - 在Oracle查询中显示总计、小计、百分比

转载 作者:行者123 更新时间:2023-12-01 17:43:15 28 4
gpt4 key购买 nike

问题,

我有这样的表格:

PID Category Year
1 AAA 2011
2 AAA 2012
3 BBB 2011
4 CCC 2010
5 CCC 2011
6 CCC 2012

我需要将输出显示为:

Subtotal Total Category  Year   Percentage
1 1 CCC 2010 100%
1 2 AAA 2011 50%
1 2 BBB 2011 50%
1 2 AAA 2012 50%
1 2 CCC 2012 50%

其中小计是特定年份该类别的计数。总计是特定年份的计数,包括所有类别。百分比为小计/总计 *100

最佳答案

select subtotal, 
total,
category,
year,
subtotal / total * 100 as percentage
from (
select count(*) over (partition by category, year) as subtotal,
count(*) over (partition by year) as total,
category,
year
from the_unknown_table
) t
order by year;

关于sql - 在Oracle查询中显示总计、小计、百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13826569/

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