Chandra Sekhar
asked on
SQL Query comparing previous date and identify top sales increase week
Hi,
I have data below,needs to identify top sales increase week,need to compare week wise and identify which week made highest sales increase. below query answer is 23 (Increase) column.How to achieve this please
Week Sale Increase
20 10000 0
21 10023 23
22 10012 12
Thanks
I have data below,needs to identify top sales increase week,need to compare week wise and identify which week made highest sales increase. below query answer is 23 (Increase) column.How to achieve this please
Week Sale Increase
20 10000 0
21 10023 23
22 10012 12
Thanks
if this is not actual data. then it should be
I guess :) if it is based on previous week
Week Sale Increase
20 10000 0
21 10023 23
22 10012 -11
I guess :) if it is based on previous week
Pls provide some sample data and expected output in tabular format.
ASKER
@HainKurt yes, you are spot on..I am looking based on previous week
maybe this
with t as (
select 20 week, 10000 Sale
union all select 21, 10023
union all select 22, 10012
),
s as (
select *, isnull(sale-lag(sale,1) over(order by week),0) Increase
from t
)
select top 1 * from s order by Increase desc
week Sale Increase
21 10023 23
order by increase desc
with t as (
select 20 week, 10000 Sale
union all select 21, 10023
union all select 22, 10012
),
s as (
select *,
isnull(sale-lag(sale,1) over(order by week),0) Increase
from t
)
select *, row_number() over (order by Increase desc) rn
from s
order by rn
week Sale Increase rn
21 10023 23 1
20 10000 0 2
22 10012 -11 3
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks @HainKurt really appreciate your help
this looks like a summary data
other wise
Open in new window
is what you need