Below is my query. In brief, my data are year to date numbers, so the query works out the month to date movement by getting the current month and minus the month-1.
It works great if the current month contains all the combinations from the month before. But this month one of the combos have been zero - therefore YTD=0 and no record exist for September.
But the record still exist in August, so it should give me a movement ie August displays 100 and Sep displays nothing. so movement should be 0-100 = -100.
I don't know how to adjust the query, I have to state which month I need first, where curr.month = 9, but since the combination doesn't exist in September, my joins doesn't work too well. Please help. It's using the Sep data as base, where it should be using both Aug and Sep combinations.
SELECT curr.[Month],
curr.[GRCARef],
curr.[IEItem],
curr.currYTD,
prev.prevYTD,
CASE curr.[Month]
When 1 then currYTD
ELSE
cast((currYTD - COALESCE(prevYTD,0)) as numeric(15,2))
END as MTD
FROM
(SELECT [Month],
[GRCARef],
[Ac Code] IEItem,
sum([BalLCY]) currYTD
FROM [PWC_2017Q2].[dbo].[SS620_PL_FullYear]
Group by [Month], [Bch], [GRCARef],[Ac Code], [ProdType],[GHO]
) curr
Full Outer JOIN
(SELECT [Month],
[GRCARef],
[Ac Code] IEItem,
sum([BalLCY]) prevYTD
FROM [PWC_2017Q2].[dbo].[SS620_PL_FullYear]
Group by [Month], [Bch], [GRCARef], [Ac Code], [ProdType],[GHO]
) prev
ON curr.[GRCARef] = prev.[GRCARef]
and curr.[IEItem] = prev.[IEItem]
and curr.[Month] - 1 = prev.[Month]
-- Join with Month -1
where
curr.[MONTH] = 9
尝试这个。
cast((ISNULL(currYTD,0) - COALESCE(prevYTD,0)) as numeric(15,2))
You need to force existence of months and then join your summary:
;with
m as (
-- get numbers from 1 to 12
select top 12 ROW_NUMBER() over (order by id) [Month]
from sysobjects
),
g as (
SELECT [Month], [GRCARef], [Ac Code] IEItem, sum([BalLCY]) YTD
FROM [PWC_2017Q2].[dbo].[SS620_PL_FullYear]
Group by [Month], [Bch], [GRCARef], [Ac Code], [ProdType], [GHO]
)
select
m.[Month],
ISNULL(curr.GRCARef, prev.GRCARef) GRCARef,
ISNULL(curr.IEItem, prev.IEItem) IEItem,
ISNULL(curr.YTD, 0) currYTD, ISNULL(prev.YTD, 0) prevYTD,
ISNULL(curr.YTD, 0) - ISNULL(prev.YTD, 0) MTD
from m
left join g curr on m.[Month] = curr.[Month]
left join g prev on m.[Month] = prev.[Month] + 1
where
m.[Month] = 9
pay attention, you have [Bch], [ProdType], [GHO]
in your group by that should not be there.. if they are 'irrelevant' just remove them, if they are relevant you should put them in select clause and also in join condition
EDIT: If you work with SQL Server 2012 and above you can also use LAG
function, to get previous month summary:
;with
m as (
-- get numbers from 1 to 12
select top 12 ROW_NUMBER() over (order by id) [Month]
from sysobjects
),
g as (
SELECT [Month], [GRCARef], [Ac Code] IEItem, sum([BalLCY]) YTD
FROM [PWC_2017Q2].[dbo].[SS620_PL_FullYear]
Group by [Month], [Bch], [GRCARef], [Ac Code], [ProdType], [GHO]
)
f as (
select
m.[Month], GRCARef, IEItem, ISNULL(YTD, 0) currYTD, ISNULL(LAG(YTD, 1, 0) over (order by m.[Month]), 0) as prevYTD
from m
left join g curr on m.[Month] = curr.[Month]
)
select *, currYTD-prevYTD MTD
from f
where
[Month] = 9
order by [Month]
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.