Sunday, February 23, 2014

Query in sql server2000

Try the below: The below would work for the given sample data. If you have any deviation in the data structure then please post more info.)



create table Test_Kiran(Tid int, TValue int,Tdate Datetime)
Insert into Test_Kiran Values
(1001,300,'05/05/2013'),
(1002,400,'05/05/2013'),
(1001,800,'06/05/2013'),
(1002,600,'06/05/2013')

Select *,IDENTITY(int,1,1) as Rn into #Temp From Test_Kiran Order by TID asc,TDate asc

Select A.TId,Datename(month,A.TDate)+ '-'+Datename(month,B.TDate),B.TValue - A.TValue From #Temp A
Inner join #Temp B On A.Rn = B.Rn-1 and A.Tid = B.TID

Drop table Test_Kiran,#Temp





No comments:

Post a Comment