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.
infotechelgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank GairolaSr. Application Support Engg.Commented:
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.
Vitor MontalvãoMSSQL Senior EngineerCommented:
I didn't test it but if the column "HAS_GHOST_PLAYER" is BIT data type you can try this:
SELECT p.PLAYER_ID , p.FIRST_NAME, p.LAST_NAME,
(
	SELECT SUM(w.NUMBER_OF_PLAYERS) - MAX(w.HAS_GHOST_PLAYER)
	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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
infotechelgAuthor Commented:
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!!!
Vitor MontalvãoMSSQL Senior EngineerCommented:
Actually BIT data type stores only 0 and 1, so it's already numeric ;)
infotechelgAuthor Commented:
Vitor,

Well, when I did it, it said I could not covert BIT to INT, so I had to do the CAST.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.