| Author |
Topic |
|
android123
Starting Member
3 Posts |
Posted - 2012-01-26 : 12:31:51
|
| Team_Name Played Won Drawn Lost PointsMan Utd 26 23 2 1 7Arsenal 27 5 6 16 21 Chelsea 27 4 2 21 14 West Ham 27 15 6 6 51Everton 26 24 1 1 73 Select the two teams that are at the bottom of the league?need help - iknow about the MIN value - but what if they want the 2 lowest?thanksAndy |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-01-26 : 13:06:37
|
| Search for ROW_NUMBER() function.http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-26 : 13:25:44
|
| [code]SELECT Team_Name, Played, Won, Drawn, Lost, PointsFROM(SELECT *,ROW_NUMBER() OVER (ORDER BY Points) AS RnFROM Table)tWHERE Rn <=2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
android123
Starting Member
3 Posts |
Posted - 2012-01-26 : 14:11:48
|
| so something likeSELECT team_name FROM tbl_teams WHERE points BETWEEN 0 and 25would be totally wrong?? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
android123
Starting Member
3 Posts |
Posted - 2012-01-26 : 14:35:25
|
| Sorry - pretty new with Sql,hadnt seen TOP used before |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-01-26 : 22:05:37
|
| TOP or ROW_NUMBER() won't work if you have more than one team in lowest/second lowest Use visakh16's answer with DENSE_RANK instead:SELECT Team_Name, Played, Won, Drawn, Lost, PointsFROM(SELECT *,DENSE_RANK() OVER (ORDER BY Points) AS RnFROM Table)tWHERE Rn <=2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-27 : 08:27:01
|
quote: Originally posted by LoztInSpace TOP or ROW_NUMBER() won't work if you have more than one team in lowest/second lowest Use visakh16's answer with DENSE_RANK instead:SELECT Team_Name, Played, Won, Drawn, Lost, PointsFROM(SELECT *,DENSE_RANK() OVER (ORDER BY Points) AS RnFROM Table)tWHERE Rn <=2
what about TOP 2 WITH TIES?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|