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 |
ssunny
Posting Yak Master
133 Posts |
Posted - 2012-08-02 : 10:46:39
|
Hello All,I need your help to write a query. Below is my table structure and expected output.testaid aname 100 a200 b300 ctest1aid eid 100 1000100 2000100 3000100 4000100 5000200 1300200 1400200 1500300 1800test2eid ename1000 'aaa'1300 'bbb'1400 'ccc' 1500 'ddd' 1800 'eee'2000 'fff' 3000 'ggg'4000 'hhh'5000 'iii'expected outputaid aname eid ename100 a 5000 'iii'100 a 4000 'hhh'100 a 3000 'ggg'200 b 1500 'ddd'200 b 1400 'ccc'200 b 1300 'bbb'300 c 1800 'eee'SO I want top 3 eid,e.name (order by eid desc) for a given aid. test1 is a junction table which joins aid and eid.Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-02 : 10:54:15
|
[code]SELECT t.aid,t.aname,t2.enameFROM test tINNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY aid ORDER BY eid DESC) AS Seq,*FROM test1)t1ON t1.aid = t.aidAND t1.Seq <=3INNER JOIN test2 t2ON t1.eid = t2.eid[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2012-08-02 : 11:25:56
|
As always works like a charm and as always THANK YOU so much Visakh!Visakhbhai Zindabad!! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-02 : 11:37:16
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|