I have to compare the profit and Loss amount of the current timeline with the very previous current timeline. The result should show if proft and Loss data is different of current timeline with the very previous timeline from current timeline. The key field of this table is R1,TP,O1,Date1.
Create table #R1 (CL char(10), R1 char(10), TP char(10), O1 char(10), Date1 datetime, amount1 numeric(10,2), profit int , Loss int)
Insert into #R1 values ('111','AA','AAAA','11','2021-01-01', 12.35,1000, 10)
Insert into #R1 values ('111','AA','AAAA','11','2022-01-01', 15.55,1000, 10)
Insert into #R1 values ('111','RA','AAAA','11','2021-01-01', 16.99,1000, 10)
Insert into #R1 values ('111','RA','AAAA','11','2022-01-01', 17.99,500, 10)
Insert into #R1 values ('111','AB','AAAA','11','2021-01-01', 12.35,1000, 10)
Insert into #R1 values ('111','AB','AAAA','11','2022-01-01', 15.55,1000, 10)
Insert into #R1 values ('111','BA','AAAA','11','2021-01-01', 16.99,1000, 10)
Insert into #R1 values ('111','BA','AAAA','11','2022-01-01', 17.99,500, 9)
Select * from #R1 order by r1, Tp, o1, Date1
Desired output:
CL R1 TP O1 Date1 amount1 profit Loss
111 BA AAAA 11 2022-01-01 17.99 500 9
111 RA AAAA 11 2022-01-01 17.99 500 10