根据价格变化计算产品利润损失(运行计数)

时间:2014-11-19 01:04:31

标签: 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 (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
  

因此难以比较RETAIL表和StoreSales表,检查零售是否匹配,如果不匹配则计算损失。

e.g。

  

销售额应为(11.99 * 12)+(22.99 * 9)= 511.72。

     

实际销售额为417.72

     

损失= 94

尝试获取结果

ProductCode       Loss
-------------   ---------
   1264           94.00

任何帮助都会很棒

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您想知道所有销售是否以最高价格进行的差异。您可以将其计算为:

select ProductCode,
       ( max(s.Current_Retail) * sum(s.quantity) - sum(total_sales) ) as loss
from #StoreSales s
group by ProductCode;

或者:

select ProductCode,
       ( max(s.Current_Retail) * sum(s.quantity) - sum(s.Current_Retail * s.quantity) ) as loss
from #StoreSales s
group by ProductCode;