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
 SQL Server Query Question

Author  Topic 

spooke
Starting Member

7 Posts

Posted - 2012-01-07 : 20:37:53
Hi guys,

I am making a database that lists sports bet results etc (decided to do something interesting for my first go) and I want to write a query that finds the minimum score of the [Team Two Score] column and then return the name of the team responsible for that score from column [Team Two].

I so far have the following which returns the lowest score, I just cant work out how to match it up with the team name.. Any help would be awesome!


USE SportsCentre
GO
SELECT MIN ([Team Two Score]) as "Lowest Scoring Opposition"
FROM NBA;
GO

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-07 : 20:44:09
You can use an order by clause and select the first one, as in:
USE SportsCentre
GO
SELECT TOP(1)
[Team Two],
MIN([Team Two Score]) AS "Lowest Scoring Opposition"
FROM NBA
GROUP BY
[Team Two]
ORDER BY
MIN([Team Two Score]);
GO
Go to Top of Page

spooke
Starting Member

7 Posts

Posted - 2012-01-07 : 21:18:28
Sweet thanks!

One more question if I may:
If I wanted to compare [Team One Score] and [Team Two Score] and display the full row for the games when Team Two wins what should I do? Can you do IF statements in SQL Server?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-08 : 02:30:20
quote:
Originally posted by spooke

Sweet thanks!

One more question if I may:
If I wanted to compare [Team One Score] and [Team Two Score] and display the full row for the games when Team Two wins what should I do? Can you do IF statements in SQL Server?


not If you should be using CASE expressions instead
but didnt understand why you need it in above requirement. wont below requirement suffice?


SELECT *
FROM NBA
WHERE [Team Two Score]> [Team One Score]


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

Go to Top of Page
   

- Advertisement -