| Author |
Topic |
|
vivsriaus
Starting Member
8 Posts |
Posted - 2009-02-23 : 17:48:02
|
| Sample table:Col1 Col2-----------ARR 156ARR 123ARR 234ARR 345Oscar 111Oscar 222Oscar 333Oscar 444Oscar 555.........Problem: I want to select top 3 records for each Col1 value. The resulting table should look something like this:Col1 Col2-----------ARR 156ARR 123ARR 234Oscar 111Oscar 222Oscar 333Any help will be much appreciated! |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-23 : 18:02:11
|
| [code]Select Col1,Col2from(Select ROW_NUMBER() OVER(Partition by Col1 Order by Col1) as seq,* from Table)ZWhere Z.seq<=3[/code] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
vivsriaus
Starting Member
8 Posts |
Posted - 2009-02-24 : 13:46:46
|
| Thanks for the help!I tried doing this, but for some reason I'm not able to get the correct result. I end up getting only one entry each for col1, even after specifying the Where clause correctly. I tried seeing the results of the inner Select clause alone, and I did see the row number tagged on to each of the record as expected. I don't know why the Where predicate isn't applied, or if I'm missing something here. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-24 : 13:49:14
|
| Can you show us your query? |
 |
|
|
vivsriaus
Starting Member
8 Posts |
Posted - 2009-02-24 : 14:46:14
|
| Sure! Here is my query:Select Col1, Col2, Col3from(Select ROW_NUMBER() OVER(Partition by Col1 ORDER BY Col3 DESC) as RowNumber, Col1, Col2, Col3 from Table)TWhere T.RowNumber <= 3Now, when I run the inner query alone:Select ROW_NUMBER() OVER(Partition by Col1 ORDER BY Col3 DESC) as RowNumber, Col1, Col2, Col3 from TableI get all the records, partitioned by Col1 with row number for each, as expected. |
 |
|
|
vivsriaus
Starting Member
8 Posts |
Posted - 2009-02-24 : 14:54:33
|
| I'm getting this to work if I change the query to something like thisSelect RowNumber, Col1, Col2, Col3from(Select ROW_NUMBER() OVER(Partition by Col1 ORDER BY Col3 DESC) as RowNumber, Col1, Col2, Col3from Table)TWhere RowNumber <= 3 |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-24 : 14:56:11
|
quote: Originally posted by vivsriaus I'm getting this to work if I change the query to something like thisSelect RowNumber, Col1, Col2, Col3from(Select ROW_NUMBER() OVER(Partition by Col1 ORDER BY Col3 DESC) as RowNumber, Col1, Col2, Col3from Table)TWhere RowNumber <= 3
Because you haven't shown us COL3 in original post. |
 |
|
|
vivsriaus
Starting Member
8 Posts |
Posted - 2009-02-24 : 16:30:45
|
| sodeep: I added Col3 just to make my statement more clearer and broad. Even if I had included just Col1 and Col2, it didn't work for some reason. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-24 : 17:02:15
|
quote: Originally posted by vivsriaus sodeep: I added Col3 just to make my statement more clearer and broad. Even if I had included just Col1 and Col2, it didn't work for some reason.
What didn't work? Well that was correct answer according to your requirement? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-25 : 10:00:06
|
quote: Originally posted by vivsriaus I'm getting this to work if I change the query to something like thisSelect RowNumber, Col1, Col2, Col3from(Select ROW_NUMBER() OVER(Partition by Col1 ORDER BY Col3 DESC) as RowNumber, Col1, Col2, Col3from Table)TWhere RowNumber <= 3
how is this different from what you posted earlier? you've just included the extra column in select |
 |
|
|
|