create table Test(Tid int, TValue int,Tdate Datetime)
Insert into Test Values (1001,300,'05/05/2013')
Insert into Test Values (1002,400,'05/05/2013')
Insert into Test Values (1001,800,'06/05/2013')
Insert into Test Values (1002,800,'06/05/2013')
Insert into Test Values (1002,900,'07/05/2013')
Insert into Test Values (1002,1100,'08/05/2013')
Select a.Tid, datename(month,a.Tdate) +'-'+ datename(month,b.Tdate) TMonth, b.TValue-a.TValue as Monthvalue from (
select (select count(*) from Test as t2
where t2.Tid = t1.Tid and t2.tdate<=t1.Tdate) as rn, t1.Tid, t1.TValue,t1.Tdate
from Test as t1) a
inner Join (select (select count(*) from Test as t2
where t2.Tid = t1.Tid and t2.tdate<=t1.Tdate) as rn, t1.Tid, t1.TValue,t1.Tdate
from Test as t1) b on a.rn=b.rn-1 and a.Tid=b.Tid
drop table Test
Sunday, February 23, 2014
Query in sql server2000
Labels:
big loop My VS C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment