简体   繁体   中英

Calculate value using previous and current month

I have below three tables

Stock Table

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

I want to calculate -> StockTable(Month - 1) + ShipFile (Month) - Sales (Month) For example For ID 1 suppose we are considering Jan (GlobalStock -> 10 + 20) data then in other tables we must take Feb values and country should be same for all tables.

So calculation would be (10 + 20) + (8 + 9) - (9) = 38

If we consider Feb ID of stocktable then we must consider March data from other tables and so on..

the joining all table i am considering ID and Country.

You can query using subquery or cte as below:

;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])

Output as below:

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

Here is an approach with derived tables:

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

Notes:

  • This uses Country for the joins because ID seems to change between tables.
  • It also uses a date range assuming that the day portion of your date column is not always the first of the month - if it is always the first that can be simplified to date = @CurrentMonth or date = @NextMonth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM