【发布时间】: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