使用上个月和当前月份计算值

时间:2018-01-26 20:27:19

标签: sql sql-server tsql

我有三张桌子

股票表

ID  GlobalStock Date    Country
1   10  2017/01/01  India
1   20  2017/01/01  India
2   5   2017/02/01  Africa
3   6   2017/08/01  Japan
4   7   2017/04/01  Japan
5   89  2017/08/01  Japan
2   10  2017/03/01  Japan
5   8   2017/03/01  Japan
1   20  2017/02/01  India

ShipFile

ID  GlobalStock Date    Country
2   10  2017/03/01  Africa
3   60  2017/08/01  India
11  70  2017/08/01  India
1   8   2017/02/01  India
1   9   2017/02/01  India
2   4   2017/03/01  Japan
2   5   2017/04/01  Japan
5   3   2017/03/01  Japan
3   8   2017/08/01  Japan

SalesFiles

ID  GlobalStock Date    Country
2   10  2017/03/01  India
2   20  2017/03/01  Africa
3   30  2017/08/01  Japan
7   5   2017/02/01  Japan
8   8   2018/01/01  Japan
1   9   2017/02/01  India
1   70  2017/02/01  Africa
13  10  2017/08/01  Japan
10  60  2017/11/01  Japan

我想计算 - > StockTable(月 - 1)+ ShipFile(月) - 销售(月) 例如 对于ID 1,假设我们正在考虑Jan(GlobalStock - > 10 + 20)数据,那么在其他表格中我们必须采用2月值,所有表格的国家/地区应该相同。

所以计算就是 (10 + 20)+(8 + 9) - (9)= 38

如果我们考虑2月份的库存表ID,那么我们必须考虑其他表格的3月数据等等。

加入所有表我正在考虑ID和国家。

2 个答案:

答案 0 :(得分:3)

您可以使用子查询或cte进行查询,如下所示:

;With cte_Stock as (
    Select ID, [Date], Country, sum(GlobalStock) Sum_GlobalStock from Stock
    group by Id, [Date], Country
), cte_ShipFiles as (
    Select ID, [Date], Country, sum(GlobalStock) Sum_GlobalStock from ShipFile
    group by Id, [Date], Country
)
, cte_SalesFiles as (
    Select ID, [Date], Country, sum(GlobalStock) Sum_GlobalStock from SalesFiles
    group by Id, [Date], Country
)
select s.ID, s.[Date], sf.[Date], s.Country, 
       YourOutput = s.Sum_GlobalStock+sf.Sum_GlobalStock-sales.Sum_GlobalStock 
    from cte_Stock s
join cte_ShipFiles sf
    on s.ID = sf.ID
    and s.Country = sf.Country
    and s.[Date] = dateadd(mm,-1, sf.[Date])
join cte_SalesFiles sales
    on s.ID = sales.ID
    and s.Country = sales.Country
    and s.[Date] = dateadd(mm,-1, sales.[Date])

输出如下:

+----+------------+------------+---------+------------+
| ID |    Date    |    Date    | Country | YourOutput |
+----+------------+------------+---------+------------+
|  1 | 2017-01-01 | 2017-02-01 | India   |         38 |
|  2 | 2017-02-01 | 2017-03-01 | Africa  |         -5 |
+----+------------+------------+---------+------------+

答案 1 :(得分:0)

以下是派生表的方法:

DECLARE @CurrentMonth date = '20180101'
DECLARE @NextMonth date = DATEADD(MONTH,1,@CurrentMonth)

SELECT s.Country, SUM(s.GlobalStock) + ShipSum - SaleSum
FROM stock s
LEFT JOIN (SELECT ISNULL(SUM(GlobalStock),0) ShipSum, Country
           FROM ShipFile
           WHERE Date >= @NextMonth
           AND Date <= EOMONTH(@NextMonth)
           GROUP BY Country) sh on s.Country = sh.Country
LEFT JOIN (SELECT ISNULL(SUM(GlobalStock),0) SaleSum, Country
           FROM SalesFile
           WHERE Date >= @NextMonth
           AND Date <= EOMONTH(@NextMonth)
           GROUP BY Country) sa on s.Country = sa.Country
WHERE s.Date >= @CurrentMonth
AND s.Date <= EOMONTH(@CurrentMonth)
GROUP BY s.Country, ShipSum, SaleSum

注意:

  • 这会使用Country进行连接,因为ID似乎在表之间发生了变化。
  • 它还使用日期范围,假设date列的日期部分并非始终是该月的第一天 - 如果它始终是第一个可以简化为date = @CurrentMonth或{{ 1}}