| Author |
Topic |
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2009-02-24 : 15:22:48
|
| Hi all. I have a problem with rank over statement.I have a simple table.col1 col2a 1b 58...I want to add the rank over col2 and select the rows with the rank equal to 7.Here is my query----------SELECT a ,b ,rank() over (order by col2) as RankNumfrom mytablewhere RankNum=7----------But SQL can not recognize the new column "RankNum".I do not want to put it into a temporary table and then select from that table. Is there anyway to let SQL recognize the new column "RankNum"?Thanks |
|
|
8022421
Starting Member
45 Posts |
Posted - 2009-02-24 : 15:33:24
|
| Try Like this...SELECT tb.a, tb.b, tb1.RankNumFROM mytable tb, ( SELECT a ,b ,rank() over (order by col2) as RankNum from mytable) tb1where RankNum=7AND tb.a = tb1.a |
 |
|
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2009-02-24 : 15:35:40
|
That is basically similar as putting into a temporary table. But thank you anyway.quote: Originally posted by 8022421 Try Like this...SELECT tb.a, tb.b, tb1.RankNumFROM mytable tb, ( SELECT a ,b ,rank() over (order by col2) as RankNum from mytable) tb1where RankNum=7AND tb.a = tb1.a
|
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-02-24 : 15:36:00
|
| May be this...SELECT col1,col2FROM (SELECT *,rank() over (order by col2) as RankNumfrom mytable) awhere a.RankNum=7 |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-02-24 : 15:39:55
|
| Oops..dint see your reply above..must have posted at the same time...never mind. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-24 : 15:57:11
|
quote: Originally posted by mavershang Hi all. I have a problem with rank over statement.I have a simple table.col1 col2a 1b 58...I want to add the rank over col2 and select the rows with the rank equal to 7.Here is my query----------SELECT a ,b ,rank() over (order by col2) as RankNumfrom mytablewhere RankNum=7----------But SQL can not recognize the new column "RankNum".I do not want to put it into a temporary table and then select from that table. Is there anyway to let SQL recognize the new column "RankNum"?Thanks
what is you Ranking based on? |
 |
|
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2009-02-24 : 15:58:04
|
Thanks for your reply.quote: Originally posted by vijayisonly Oops..dint see your reply above..must have posted at the same time...never mind.
|
 |
|
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2009-02-24 : 15:58:41
|
The rank is based on the value of Col2.quote: Originally posted by sodeep
quote: Originally posted by mavershang Hi all. I have a problem with rank over statement.I have a simple table.col1 col2a 1b 58...I want to add the rank over col2 and select the rows with the rank equal to 7.Here is my query----------SELECT a ,b ,rank() over (order by col2) as RankNumfrom mytablewhere RankNum=7----------But SQL can not recognize the new column "RankNum".I do not want to put it into a temporary table and then select from that table. Is there anyway to let SQL recognize the new column "RankNum"?Thanks
what is you Ranking based on?
|
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-24 : 15:59:35
|
| What if you have duplicate Col2? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-25 : 09:26:01
|
| you cant use windowing functions like rank() directly in where, so you need to make the derived table as above and then select from them. |
 |
|
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2009-02-25 : 10:18:50
|
| Thank you guys. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-25 : 10:22:55
|
| welcome |
 |
|
|
|