What I have been trying to do is compare every 4 weeks we receive a payment but I need to know if that value changes (higher or Lower) and what the new values are and what the old value was.
Ive been trying to use a case Statement but I cant get my head around it.
Below is my table which I need to compare. (there would be multiple account numbers in this table)
What I sort of need to see is the results would be:
I would use LAG()
function to find "week-4" number (assuming there is data for all weeks) - for multiple accounts you will have to add partition by
to over
clause. How it works, see here .
I do not know logic for showing/not showing data for particular week so I did not include this in my answer.
with s as (
select v.weekNum, v.amount, LAG(v.weekNum, 4)over(order by v.weekNum) refWeekNum
from (values('201901','2.3'),('201902','3.6'),('201903','9.9'),('201904','8.7'),('201905','10.8'),('201906','8.5'),('201907','10.4'),('201908','10.8'),('201909','4.1'),('201910','8.4'),('201911','5.5'),('201912','5.2'),('201913','4.1'),('201914','5.5'),('201915','5.1'),('201916','5.8'),('201917','2.3'),('201918','10.1'),('201919','10.1'),('201920','9.9'),('201921','3.8'),('201922','3.2'),('201923','1.3'),('201924','2.3'),('201925','1.5'),('201926','5.10'),('201927','3.6'),('201928','9.9'),('201929','10.7'),('201930','4.8'))v(weekNum,amount)
)
select s.weekNum, s.amount, s.refWeekNum, ref.amount refAmount
from s
left outer join s ref on s.refWeekNum = ref.weekNum
You really only need a CASE
expression for your Y\N
change column. LAG
will do all the rest of the heavy lifting for you.
The COALESCE
in the CASE
is just there to indicate no change when there's no comparison value.
You could wrap more CASE
expressions around the last columns to only display them if there are differences, but that's really a job for the presentation layer, not the data layer.
This yields your desired results, but I won't chew up screen space with the output.
SELECT
v.AcctNum
,v.WkNum
,v.Amt
,CASE
WHEN v.Amt = COALESCE(LAG(v.Amt, 4) OVER (PARTITION BY v.AcctNum ORDER BY v.WkNum), v.Amt)
THEN 'No'
ELSE 'Yes'
END AS Changed
,v.Amt AS OldValue
,LAG(v.Amt, 4) OVER (PARTITION BY v.AcctNum ORDER BY v.WkNum) AS NewValue
FROM
(
VALUES
(10586,'201901', -330.14)
,(10586,'201902', 0)
,(10586,'201903', 0)
,(10586,'201904', 0)
,(10586,'201905', -425.84)
,(10586,'201906', 0)
,(10586,'201907', 0)
,(10586,'201908', 0)
,(10586,'201909', -425.84)
,(10586,'201910', 0)
,(10586,'201911', 0)
,(10586,'201912', 0)
,(10586,'201913', -335.12)
,(2,'201901', -130.14)
,(2,'201902', 0)
,(2,'201903', 0)
,(2,'201904', 0)
,(2,'201905', -225.84)
,(2,'201906', 0)
,(2,'201907', 0)
,(2,'201908', 0)
,(2,'201909', -225.84)
,(2,'201910', 0)
,(2,'201911', 0)
,(2,'201912', 0)
,(2,'201913', -235.12)
) AS v (AcctNum, WkNum, Amt);
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.