这应该在Excel中轻松完成,但是,我希望通过SQL完成这种计算。我可以使用GROUP BY
,OVER()
来计算单年的SUM和%。但我没有立即提供3年的数据。任何帮助将不胜感激。
答案 0 :(得分:4)
SUMIF
在SQL中复制 SUM(case statement)
SELECT Type
,SUM(CASE WHEN Year = '2010' THEN Total ELSE 0 END)'2010 Total'
,SUM(CASE WHEN Year = '2010' THEN Total ELSE 0 END)*1.0/SUM(SUM(CASE WHEN Year = '2010' THEN Total ELSE 0 END)) OVER () '2010 Percent of Total'
,SUM(CASE WHEN Year = '2011' THEN Total ELSE 0 END)'2011 Total'
,SUM(CASE WHEN Year = '2011' THEN Total ELSE 0 END)*1.0/SUM(SUM(CASE WHEN Year = '2011' THEN Total ELSE 0 END)) OVER () '2011 Percent of Total'
,SUM(CASE WHEN Year = '2012' THEN Total ELSE 0 END)'2012 Total'
,SUM(CASE WHEN Year = '2012' THEN Total ELSE 0 END)*1.0/SUM(SUM(CASE WHEN Year = '2012' THEN Total ELSE 0 END)) OVER () '2012 Percent of Total'
,SUM(Total) 'Total'
,SUM(Total)*1.0/SUM(SUM(Total)) OVER () 'Percent of Total'
FROM Table
GROUP BY Type
答案 1 :(得分:4)
由于您使用的是SQL Server,因此如果您使用的是SQL Server 2005+,则可以使用PIVOT函数来获取结果。此解决方案实现了一个unpivot和一个pivot过程来获得结果。此结果的起点是按类型计算总百分比和总数:
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
见SQL Fiddle with Demo。这将给出一个您想要转动的多个列的结果,这样您就可以使用CROSS APPLY将数据展开成多行:
select type,
col = cast(year as varchar(4))+'_'+col,
value,
t_type
from
(
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
) d
cross apply
(
select 'total', total union all
select 't_per', t_per
) c (col, value);
见Demo。最后,您可以将PIVOT函数应用于col
:
select type,
[2010_total], [2010_t_per],
[2011_total], [2011_t_per],
[2012_total], [2012_t_per],
t_type,
tot_per
from
(
select type,
col = cast(year as varchar(4))+'_'+col,
value,
t_type,
tot_per
from
(
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
) d
cross apply
(
select 'total', total union all
select 't_per', t_per
) c (col, value)
) s
pivot
(
max(value)
for col in ([2010_total], [2010_t_per],
[2011_total], [2011_t_per],
[2012_total], [2012_t_per])
) piv
见SQL Fiddle with Demo。这可以重构为使用CTE而不是子查询,如果年份未知,也可以将其转换为使用动态SQL。
如果您有未知数量的值,则动态SQL代码将为:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(cast(year as varchar(4))+'_'+col)
from tablea
cross apply
(
select 'total', 1 union all
select 't_per', 2
) c (col, so)
group by year, col, so
order by year, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT type,' + @cols + ', t_type, tot_per
from
(
select type,
col = cast(year as varchar(4))+''_''+col,
value,
t_type,
tot_per
from
(
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
) d
cross apply
(
select ''total'', total union all
select ''t_per'', t_per
) c (col, value)
) x
pivot
(
max(value)
for col in (' + @cols + ')
) p '
execute sp_executesql @query;
见Demo。静态版本和动态版本都给出了结果:
| TYPE | 2010_TOTAL | 2010_T_PER | 2011_TOTAL | 2011_T_PER | 2012_TOTAL | 2012_T_PER | T_TYPE | TOT_PER |
---------------------------------------------------------------------------------------------------------
| A | 1 | 16.7 | 1 | 16.7 | 1 | 16.7 | 3 | 16.7 |
| B | 2 | 33.3 | 2 | 33.3 | 2 | 33.3 | 6 | 33.3 |
| C | 3 | 50 | 3 | 50 | 3 | 50 | 9 | 50 |
答案 2 :(得分:1)
为简单起见,我希望垂直显示此结果:
SELECT
Type,
Year,
SUM(Total) as Dollars,
ROUND(SUM(Total) * 100 / (SELECT SUM(TOTAL) FROM TableA t2 WHERE t2.Year = t1.Year),1) as Per
FROM TableA t1
Group By Type, Year
<强>输出:强>
TYPE YEAR DOLLARS PERCENT
A 2010 1 16.7
B 2010 2 33.3
C 2010 3 50
A 2011 1 16.7
B 2011 2 33.3
C 2011 3 50
A 2012 1 16.7
B 2012 2 33.3
C 2012 3 50