Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于价格变化(运行计数)计算产品利润损失_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

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
因此,困难在于比较零售表和StoreSales表,检查零售是否匹配,如果不匹配,则计算损失

e、 g

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

实际销售额为417.72美元

损失=94

试图获得的结果

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;
或: