SQL Server计算年度、季度和月度的市场份额
是否可以使用SQL计算以下月度、季度和年度样本数据的市场份额? 我可以在excel中这样做,只是不知道如何在SQL中这样做SQL Server计算年度、季度和月度的市场份额,sql,sql-server,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,
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
不清楚您是如何定义市场份额的,但您的问题的答案是窗口功能 我可能会将市场份额解释为给定时间单位内品牌在所有销售额中所占的比例。如果是:
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;
请解释一下计算方法。谢谢你做了一些小的修改。这工作做得很好!