Sql 基于价格变化(运行计数)计算产品利润损失
我很难根据一段时间内发生的价格变化来计算产品的利润损失。当价格在这段时间内涨跌时,我在苦苦计算损失 示例:Sql 基于价格变化(运行计数)计算产品利润损失,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我很难根据一段时间内发生的价格变化来计算产品的利润损失。当价格在这段时间内涨跌时,我在苦苦计算损失 示例: /*Create Table*/ CREATE TABLE #StoreSales ( SalesDate date, ProductCode int, Retail decimal(5,2), Quantity int ); /*Insert Sales data into Temp Table*/ INSERT INTO #StoreSales (Sa
/*Create Table*/
CREATE TABLE #StoreSales
(
SalesDate date,
ProductCode int,
Retail decimal(5,2),
Quantity int
);
/*Insert Sales data into Temp Table*/
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-01',1264,'47.96',4);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-02',1264,'47.96',4);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-03',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-03',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-04',1264,'5.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-04',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-05',1264,'22.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-06',1264,'35.97',3);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-07',1264,'22.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-07',1264,'23.98',2);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-08',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-08',1264,'91.96',4);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-09',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-10',1264,'45.98',2);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-11',1264,'11.99',1);
/*Query Temp Table*/
DECLARE @s date, @e date
SET @s = '2014-11-01'
SET @e = '2014-11-30';
SELECT x.SalesDate
,x.ProductCode
,(x.Retail/nullif(x.Quantity,0)) as Current_Retail
,x.Quantity
,x.Retail as total_sales
FROM #StoreSales x
WHERE x.ProductCode = 1264
AND x.SalesDate between @s and @e
ORDER BY x.SalesDate
DROP TABLE #StoreSales
SalesDate ProductCode Current_Retail Quantity total_sales
---------- ----------- ----------------- ---------- ------------
2014-11-01 1264 11.99 4 47.96
2014-11-02 1264 11.99 4 47.96
2014-11-03 1264 11.99 1 11.99
2014-11-03 1264 11.99 1 11.99
2014-11-04 1264 5.99 1 5.99
2014-11-04 1264 11.99 1 11.99
2014-11-05 1264 22.99 1 22.99
2014-11-06 1264 11.99 3 35.97
2014-11-07 1264 22.99 1 22.99
2014-11-07 1264 11.99 2 23.98
2014-11-08 1264 11.99 1 11.99
2014-11-08 1264 22.99 4 91.96
2014-11-09 1264 11.99 1 11.99
2014-11-10 1264 22.99 2 45.98
2014-11-11 1264 11.99 1 11.99
---- ---------
28 417.72
结果:
/*Create Table*/
CREATE TABLE #StoreSales
(
SalesDate date,
ProductCode int,
Retail decimal(5,2),
Quantity int
);
/*Insert Sales data into Temp Table*/
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-01',1264,'47.96',4);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-02',1264,'47.96',4);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-03',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-03',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-04',1264,'5.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-04',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-05',1264,'22.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-06',1264,'35.97',3);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-07',1264,'22.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-07',1264,'23.98',2);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-08',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-08',1264,'91.96',4);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-09',1264,'11.99',1);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-10',1264,'45.98',2);
INSERT INTO #StoreSales (SalesDate,ProductCode,Retail,Quantity) VALUES ('2014-11-11',1264,'11.99',1);
/*Query Temp Table*/
DECLARE @s date, @e date
SET @s = '2014-11-01'
SET @e = '2014-11-30';
SELECT x.SalesDate
,x.ProductCode
,(x.Retail/nullif(x.Quantity,0)) as Current_Retail
,x.Quantity
,x.Retail as total_sales
FROM #StoreSales x
WHERE x.ProductCode = 1264
AND x.SalesDate between @s and @e
ORDER BY x.SalesDate
DROP TABLE #StoreSales
SalesDate ProductCode Current_Retail Quantity total_sales
---------- ----------- ----------------- ---------- ------------
2014-11-01 1264 11.99 4 47.96
2014-11-02 1264 11.99 4 47.96
2014-11-03 1264 11.99 1 11.99
2014-11-03 1264 11.99 1 11.99
2014-11-04 1264 5.99 1 5.99
2014-11-04 1264 11.99 1 11.99
2014-11-05 1264 22.99 1 22.99
2014-11-06 1264 11.99 3 35.97
2014-11-07 1264 22.99 1 22.99
2014-11-07 1264 11.99 2 23.98
2014-11-08 1264 11.99 1 11.99
2014-11-08 1264 22.99 4 91.96
2014-11-09 1264 11.99 1 11.99
2014-11-10 1264 22.99 2 45.98
2014-11-11 1264 11.99 1 11.99
---- ---------
28 417.72
在上面的场景中,我需要找到销售时的价格
为此,我需要将其加入零售表
/* Retail Table */
ProductCode ValidFrom ValidTo Retail
---------- ------------ ------------- ----------
1264 2014-11-01 2014-11-04 11.99
1264 2014-11-05 NULL 22.99
从上面可以看出,以下日期之间的任何销售的零售额应为:
- 2014-11-01应该是11.99
- 2014-11-05到今天应该是22.99
ProductCode Loss
------------- ---------
1264 94.00
如果我理解正确的话,任何帮助都将是非常好的如果所有的销售都是以最高的价格进行的,您想知道区别。您可以将其计算为:
select ProductCode,
( max(s.Current_Retail) * sum(s.quantity) - sum(total_sales) ) as loss
from #StoreSales s
group by ProductCode;
或: