1

在此处输入图像描述 这在 Excel 中应该很容易完成,但是,我希望通过 SQL 完成这种计算。我可以使用GROUP BY,OVER()来计算一年的总和和百分比。但我未能一次提供 3 年的数据。任何帮助将不胜感激。

4

3 回答 3

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
于 2013-07-29T19:00:06.527 回答
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);

请参阅演示。最后,您可以将 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;

请参阅演示。静态版本和动态版本都给出了结果:

| 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 |
于 2013-07-29T19:13:02.847 回答
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

Sql 小提琴演示

于 2013-07-29T18:52:00.083 回答