Reputation: 15
I have data like
EmpID Salary Paid Date Salary Paid
100 628 1/11/2019
100 1086 1/25/2019
100 1055 2/8/2019
100 981 2/22/2019
100 653 3/8/2019
100 871 3/22/2019
100 385 4/18/2019
101 2032 1/4/2019
101 3466 1/18/2019
101 2652 2/1/2019
101 2013 4/26/2019
Is there any way identify a "Trend Decreasing" flag (Yes/ No). only one record or preferably decision on "Decreasing" can be repeat for all the rows. something like
EmpID Salary Paid Date Salary Paid Decreasing Flag
100 628 1/11/2019 Y
100 1086 1/25/2019 Y
100 1055 2/8/2019 Y
100 981 2/22/2019 Y
100 653 3/8/2019 Y
100 871 3/22/2019 Y
100 385 4/18/2019 Y
101 2032 1/4/2019 N
101 3466 1/18/2019 N
101 2652 2/1/2019 N
101 2013 4/26/2019 N
Y/N is the valued for each empid (not for each row)
Thanks
I have tired but couldn't find a proper way
Upvotes: 0
Views: 1822
Reputation: 1269493
If you want to compare the first and last salaries for the trend, you can use first_value()
:
select t.*,
(case when first_value(salary) over (partition by empid order by paid_date) <
first_value(salary) over (partition by empid order by paid_date desc)
then 'N' else 'Y'
end) as decreasing_flag
from t;
Note that the syntax for first_value()
may vary slightly among databases.
Upvotes: 3