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
 Order By Statement

Author  Topic 

vb89
Starting Member

9 Posts

Posted - 2008-09-22 : 18:07:49
I have the following SQL statement

CURSOR player_cursor IS
select *
from customer_data.cd_soccer_players p
where p.team_id = tID
and p.league_id = p_leagueID
and p.status = 'Y';


I would like to order the following statement by a column called player_position. Player position has four different options: goalie, forward, center, defense. I would like to have the player be ordered in the following order:
1. Forward
2. Defense
3. Goalie
4. Center

Any help would be greatly appreciated

Mapper
Starting Member

12 Posts

Posted - 2008-09-22 : 18:16:33
Use a case statement to create a new field called "OrderStatus"

CASE WHEN Player-Position = "Forward
Order Status = 1
C W PP = "Defense"
Order Status = 2

Then Order on the Order Status Field.

http://www.BackcountrySecrets.com - Find and share places to play outdoors
Go to Top of Page

Mapper
Starting Member

12 Posts

Posted - 2008-09-22 : 18:22:27
Sorry I just read that and it made no sense.

Try this
SELECT *
FROM customer_data.cd_soccer_players p
WHERE p.team_id = tID
AND p.league_id = p_leagueID
AND p.status = 'Y'
ORDER BY
CASE WHEN p.player_position = 'Forward' THEN 1
CASE WHEN p.player_position = 'Defense' THEN 2
CASE WHEN p.player_position = 'Goalie' THEN 3
ELSE 4
END;

http://www.BackcountrySecrets.com - Find and share places to play outdoors
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-23 : 00:15:06
quote:
Originally posted by Mapper

Sorry I just read that and it made no sense.

Try this
SELECT *
FROM customer_data.cd_soccer_players p
WHERE p.team_id = tID
AND p.league_id = p_leagueID
AND p.status = 'Y'
ORDER BY
CASE WHEN p.player_position = 'Forward' THEN 1
CASE WHEN p.player_position = 'Defense' THEN 2
CASE WHEN p.player_position = 'Goalie' THEN 3
ELSE 4
END;

http://www.BackcountrySecrets.com - Find and share places to play outdoors



no need to repeat CASE keyword each time
Go to Top of Page
   

- Advertisement -