Forum Discussion

nickname2485's avatar
nickname2485
Copper Contributor
Aug 25, 2023

Write SQL query

Hello,

 

I could solve this:

 

For each customer/date show daily profit/loss and compare it with previous day same customers profit/loss.

 

I tried this but I am not sure if it is ok:

select fullname,

       profitloss,

       date,

       lag(profitloss) over (partition by DailyProfitLoss.PersonId order by DailyProfitLoss.Date) - DailyProfitLoss.ProfitLoss as diff_to_prev,

       lead(profitloss) over (partition by DailyProfitLoss.PersonId order by DailyProfitLoss.Date) - DailyProfitLoss.ProfitLoss as diff_to_next

from Customers, DailyProfitLoss WHERE Customers.PersonID=DailyProfitLoss.personid;
No RepliesBe the first to reply