SQL Server计算年,季度和月的市场份额

时间:2018-10-03 01:23:32

标签: sql sql-server

是否可以使用SQL为以下月份,季度和年份的以下示例数据计算市场份额? 我可以在excel中做到这一点,只是不确定如何在SQL中做到这一点。

CREATE TABLE [dbo].[tblReturns](
[ReturnID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] [varchar](100) NULL,
[Brand] [varchar](100) NULL,
[Type] [varchar] (100) NULL,
[Quantity] [int] NULL,
[Datecreated] [datetime] NULL)
GO
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Coke', 'Bottle', 1, '1 sep 2018')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Coke', 'Bottle', 4, '2 sep 2018')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Coke', 'Can', 4, '10 sep 2018')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Fanta', 'Bottle', 1, '18 sep 2018')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Fanta', 'Can', 4, '1 Oct 2018')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Coke', 'Bottle', 2, '9 Oct 2017')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Fanta', 'Can', 6, '14 oct 2017')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Fanta', 'Can', 1, '30 oct 2017')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Coke', 'Bottle', 3, '2 dec 2017')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Coke', 'Bottle', 8, '3 dec 2017')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Coke', 'Bottle', 8, '3 dec 2017')
 INSERT INTO tblReturns (CustomerID,Brand,Type,Quantity,Datecreated)VALUES  (123, 'Fanta', 'Bottle', 5, '10 dec 2017')
 GO

  SELECT DateCreated
  ,CustomerID
  ,Product
  ,sum(Quantity) as Quantity
  ,DATENAME(MM,DateCreated) as Month
  ,DATENAME(QUARTER, DateCreated) as quarter
  ,DATENAME(yyyy, DateCreated) as Year
   FROM tblReturns
   GROUP BY
  DateCreated
  ,CustomerID
  ,Product
  ,DATENAME(MM,DateCreated)
  ,DATENAME(QUARTER, DateCreated)
  ,DATENAME(yyyy, DateCreated)
  order by DateCreated

我需要按月份,季度和年份每个字段的百分比显示市场份额。

下面的预期结果

+-------------+------------+-------+--------+----------+-----------+---------+------+---------+----------+--------+
| Datecreated | CustomerID | Brand |  Type  | Quantity |   Month   | Quarter | Year | %Month  | %Quarter | %Year  |
+-------------+------------+-------+--------+----------+-----------+---------+------+---------+----------+--------+
| 9/10/2017   |        123 | Coke  | Bottle |        2 | October   |       4 | 2017 | 22.22%  | 63.64%   | 63.64% |
| 14/10/2017  |        123 | Fanta | Can    |        6 | October   |       4 | 2017 | 77.78%  | 36.36%   | 36.36% |
| 30/10/2017  |        123 | Fanta | Can    |        1 | October   |       4 | 2017 | 77.78%  | 36.36%   | 36.36% |
| 2/12/2017   |        123 | Coke  | Bottle |        3 | December  |       4 | 2017 | 79.17%  | 63.64%   | 63.64% |
| 3/12/2017   |        123 | Coke  | Bottle |        8 | December  |       4 | 2017 | 79.17%  | 63.64%   | 63.64% |
| 3/12/2017   |        123 | Coke  | Bottle |        8 | December  |       4 | 2017 | 79.17%  | 63.64%   | 63.64% |
| 10/12/2017  |        123 | Fanta | Bottle |        5 | December  |       4 | 2017 | 20.83%  | 36.36%   | 36.36% |
| 1/09/2018   |        123 | Coke  | Bottle |        1 | September |       3 | 2018 | 90.00%  | 90.00%   | 64.29% |
| 2/09/2018   |        123 | Coke  | Bottle |        4 | September |       3 | 2018 | 90.00%  | 90.00%   | 64.29% |
| 10/09/2018  |        123 | Coke  | Can    |        4 | September |       3 | 2018 | 90.00%  | 90.00%   | 64.29% |
| 18/09/2018  |        123 | Fanta | Bottle |        1 | September |       3 | 2018 | 10.00%  | 10.00%   | 35.71% |
| 1/10/2018   |        123 | Fanta | Can    |        4 | October   |       4 | 2018 | 100.00% | 100.00%  | 35.71% |
+-------------+------------+-------+--------+----------+-----------+---------+------+---------+----------+--------+

谢谢!

编辑: 谢谢大家,下面的工作非常完美!

select r.*,DATENAME(MM,DateCreated) as Month
  ,DATENAME(QUARTER, DateCreated) as quarter
  ,DATENAME(yyyy, DateCreated) as Year
   ,round((sum(quantity) over (partition by brand, year(datecreated), month(datecreated))* 100.0 /
    (sum(quantity) over (partition by year(datecreated), month(datecreated)))),2) as ms_month

   ,round((sum(quantity) over (partition by brand, year(datecreated), datepart(quarter, datecreated)) * 100.0 /
    (sum(quantity) over (partition by year(datecreated), datepart(quarter, datecreated)))),2) as ms_quarter

    ,round((sum(quantity) over (partition by brand, year(datecreated)) * 100.0 /
    (sum(quantity) over (partition by year(datecreated)))),2) as ms_year
from tblreturns r
order by r.DateCreated

1 个答案:

答案 0 :(得分:1)

目前尚不清楚如何定义市场份额,但问题的答案是窗口函数。

我可以将市场份额解释为在给定时间单位内品牌在所有销售中所占的比例。如果是这样:

select r.*,
       (sum(quantity) over (partition by brand, year(datecreated), month(datecreated) * 1.0 /
        sum(quantity) over (partition by year(datecreated), month(datecreated)
       ) as ms_month,
       (sum(quantity) over (partition by brand, year(datecreated), datepart(quarter, datecreated) * 1.0 /
        sum(quantity) over (partition by year(datecreated), datepart(quarter, datecreated)
       ) as ms_quarter,
       (sum(quantity) over (partition by brand, year(datecreated) * 1.0 /
        sum(quantity) over (partition by year(datecreated)
       ) as ms_year
from tblreturns r;