Link to home
Start Free TrialLog in
Avatar of infotechelg
infotechelgFlag for United States of America

asked on

T-SQL Query Question

I have this query:

SELECT p.PLAYER_ID , p.FIRST_NAME, p.LAST_NAME,
(
	SELECT SUM(w.NUMBER_OF_PLAYERS)
	FROM tbl_WEEK w INNER JOIN xref_PLAYER_WEEK x ON x.WEEK_ID = w.WEEK_ID
	WHERE x.PLAYER_ID = p.PLAYER_ID
	 AND w.SEASON_ID = @SEASON_ID
) AS AVAILABLE_POINTS
FROM tbl_PLAYER p

Open in new window

Basically, it gets players in a league and sums up the number of players there were each week. As you can see, tbl_WEEK has a column called "NUMBER_OF_PLAYERS".

However, a new rule has been implemented that I need to figure out how to include in the query.

A new column in tbl_WEEK has been added called "HAS_GHOST_PLAYER".

If this new column is "TRUE", then i need to subtract one from NUMBER_OF_PLAYERS. If it's "FALSE", then the query stays as-is.

I tried doing a CASE on the nested SELECT statement, which works great if there's only one WEEK in the season. But if there are multiple weeks, I get the "subquery returns multiple columns" errors.

Any help on how to go about this would be greatly appreciated.
Avatar of Mayank Gairola
Mayank Gairola
Flag of India image

What's the issue in directly joining(or left joining) the tables to get the Sum?
That ways you won't run into the sub-query error.
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of infotechelg

ASKER

It's so simple, it's brilliant. That did the trick! Though, I had to cast w.HAS_GHOST_PLAYER as an INT in order to use MAX.

Thanks!!!
Actually BIT data type stores only 0 and 1, so it's already numeric ;)
Vitor,

Well, when I did it, it said I could not covert BIT to INT, so I had to do the CAST.