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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 please help with this query??

Author  Topic 

android123
Starting Member

3 Posts

Posted - 2012-01-26 : 12:31:51
Team_Name Played Won Drawn Lost Points
Man Utd 26 23 2 1 7
Arsenal 27 5 6 16 21
Chelsea 27 4 2 21 14

West Ham 27 15 6 6 51
Everton 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?

thanks
Andy

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.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 13:25:44
[code]
SELECT Team_Name, Played, Won, Drawn, Lost, Points
FROM
(
SELECT *,ROW_NUMBER() OVER (ORDER BY Points) AS Rn
FROM Table
)t
WHERE Rn <=2
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-26 : 13:39:21
SELECT TOP 2 * FROM Table ORDER BY Points DESC

Is what your Prof wants to see right about now





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

android123
Starting Member

3 Posts

Posted - 2012-01-26 : 14:11:48
so something like

SELECT team_name FROM tbl_teams WHERE points BETWEEN 0 and 25

would be totally wrong??
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-26 : 14:31:19
what if you have 10 teams with 5 points?

You don't like my TOP 2?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

android123
Starting Member

3 Posts

Posted - 2012-01-26 : 14:35:25
Sorry - pretty new with Sql,

hadnt seen TOP used before
Go to Top of Page

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, Points
FROM
(
SELECT *,DENSE_RANK() OVER (ORDER BY Points) AS Rn
FROM Table
)t
WHERE Rn <=2
Go to Top of Page

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, Points
FROM
(
SELECT *,DENSE_RANK() OVER (ORDER BY Points) AS Rn
FROM Table
)t
WHERE Rn <=2



what about TOP 2 WITH TIES?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -