Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
reddy_vam
Starting Member
43 Posts |
Posted - 2013-04-06 : 17:14:11
|
Here is my test tableid month sales1 Jan 10001 Feb 20002 April 2002 May 5002 June 8003 Jan 40003 Feb 20003 April 100Exepected output should be like belowid month sales1 Feb 20002 June 8003 Jan 4000Please help me with a T-SQL query for the above outputThanks,Reddy |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2013-04-07 : 08:47:32
|
;with SalesAS( select 1 as ID, 'Jan' as [month], 1000 as sales union all select 1 ,'Feb' , 2000 union all select 2 ,'April', 200 union all select 2 ,'May' ,500 union all select 2 ,'June', 800 union all select 3 ,'Jan' ,4000 union all select 3 ,'Feb' ,2000 union all select 3, 'April', 100)select S.* from Sales as S outer apply ( select top 1 id ,[month] as[month] ,sales From Sales as SS where S.ID=SS.id order by SS.sales desc ) SRwhere S.id=SR.id and S.[month]=SR.[month]order by S.ID SCe-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mutsabinWeb |
|
|
|
|
|
|
|