SQL加法公式

时间:2019-02-06 19:44:17

标签: sql sql-server

菜鸟警报...

我有一个如下的示例表。

enter image description here

我正在尝试在SQL中创建一列,以显示每个客户每年拥有大小S的百分比。

因此输出应类似于: (更正:2019年的客户C百分比应为1)

enter image description here

4 个答案:

答案 0 :(得分:0)

假设一年中没有重复的客户行,则可以使用窗口功能:

select t.*,
       sum(count) over (partition by year) as year_cnt,
       count * 1.0 / sum(count) over (partition by year) as ratio
from t;

答案 1 :(得分:0)

将其分解为任务-在涉及SQL时,这可能是最好的规则。因此,我创建了一个变量表@tmp,其中填充了您的示例数据,并从以下查询开始:

select
    customer,
    year
from @tmp
where size = 'S'
group by customer, year

...对于“ S”条目,每个客户/年份组合都会获得一行。

接下来,我想要该客户/年份组合的总数:

select
    customer,
    year,
    SUM(itemCount) as customerItemCount
from @tmp
where size = 'S'
group by customer, year

...现在,我们如何获得特定年份 all 个客户的数量?我们需要一个子查询-我们需要该子查询引用主查询中的年份。

select
    customer,
    year,
    SUM(itemCount) as customerItemCount,
    (select SUM(itemCount) from @tmp t2 where year=t.year) as FullTotalForYear
from @tmp t
where size = 'S'
GROUP BY customer, year

...有意义吗? ()中的新行是一个子查询-并且再次击中该表-但这次,它只是在与主表匹配的特定年份中获得了SUM()。

最后,我们只需要将其中一列除以另一列即可获得实际百分比(确保不要将其设置为int / int-始终为int),我们将得到最终答案:

select
    customer,
    year,
    cast(SUM(itemCount) as float) / 
        (select SUM(itemCount) from @tmp t2 where year=t.year)
        as PercentageOfYear
from @tmp t
where size = 'S'
GROUP BY customer, year

有道理吗?

答案 2 :(得分:0)

加入2个分组:
根据大小,年份,客户
排名第一 第二个是 size,year

select 
  t.customer, t.year, t.count, t.size, 
  ty.total_count, 1.0 * t.count / ty.total_count percentage 
from (
  select t.customer, t.year, sum(t.count) count, t.size
  from tablename t
  group by t.size, t.year, t.customer
) t inner join (
  select t.year, sum(t.count) total_count, t.size
  from tablename t
  group by t.size, t.year
) ty
on ty.size = t.size and ty.year = t.year
order by t.size, t.year, t.customer;

请参见demo

答案 3 :(得分:0)

窗口功能将带您到达那里。

DECLARE @TestData TABLE
    (
        [Customer] NVARCHAR(2)
      , [CustomerYear] INT
      , [CustomerCount] INT
      , [CustomerSize] NVARCHAR(2)
    );

INSERT INTO @TestData (
                          [Customer]
                        , [CustomerYear]
                        , [CustomerCount]
                        , [CustomerSize]
                      )
VALUES ( 'A', 2017, 1, 'S' )
     , ( 'A', 2017, 1, 'S' )
     , ( 'B', 2017, 1, 'S' )
     , ( 'B', 2017, 1, 'S' )
     , ( 'B', 2018, 1, 'S' )
     , ( 'A', 2018, 1, 'S' )
     , ( 'C', 2017, 1, 'S' )
     , ( 'C', 2019, 1, 'S' );

SELECT   DISTINCT [Customer]
                , [CustomerYear]
                , SUM([CustomerCount]) OVER ( PARTITION BY [Customer]
                                                 , [CustomerYear]
                                    ) AS [CustomerCount]
                , SUM([CustomerCount]) OVER ( PARTITION BY [CustomerYear] ) AS [TotalCount]
                , SUM([CustomerCount]) OVER ( PARTITION BY [Customer]
                                                 , [CustomerYear]
                                    ) * 1.0 / SUM([CustomerCount]) OVER ( PARTITION BY [CustomerYear] ) AS [CustomerPercentage]
FROM     @TestData
ORDER BY [CustomerYear]
       , [Customer];

会给你

Customer CustomerYear CustomerCount TotalCount  CustomerPercentage
-------- ------------ ------------- ----------- ---------------------------------------
A        2017         2             5           0.400000000000
B        2017         2             5           0.400000000000
C        2017         1             5           0.200000000000
A        2018         1             2           0.500000000000
B        2018         1             2           0.500000000000
C        2019         1             1           1.000000000000