Sunday, February 23, 2014

Query in sql server2000

Does this help?

declare @tab table(Tid int,TValue int,Tdate date);
insert into @tab values
(1001,300,'05/05/2013'),
(1002,400,'05/05/2013'),
(1001,800,'06/05/2013'),
(1002,600,'06/05/2013');


/*
Tid TMonth Monthvalue
1001 May-June 500
1002 May-June 200
*/

if object_id('tempdb..#maxval') is not null
drop table #maxval
if object_id('tempdb..#minval') is not null
drop table #minval
select tid,tdate,tvalue into #maxval from @tab t1
where tdate=(select max(tdate) from @tab ti where ti.Tid=t1.Tid)

select tid,tdate,tvalue into #minval from @tab t1
where tdate=(select min(tdate) from @tab ti where ti.Tid=t1.Tid)


select mx.tid,mx.TValue-mn.TValue,datename(month,mn.Tdate)+'-'+datename(month,mx.Tdate) from #minval mn join #maxval mx on mn.Tid=mx.Tid





Satheesh

My Blog | How to ask questions in technical forum





No comments:

Post a Comment