【问题标题】:SQL Server calculate market share for year, quarter and monthSQL Server 计算年、季度和月的市场份额
【发布时间】:2019-03-08 04:14:37
【问题描述】:

是否可以使用 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

【问题讨论】:

  • 请解释计算。

标签: sql sql-server


【解决方案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;

【讨论】:

  • 感谢您进行了一些小的修改,这非常有效!
猜你喜欢
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多