Bad Dub Bad Dub - 7 months ago 13
SQL Question

Stored procedure - Group By Id another value equals parameter

This is for SQL Server 2012

I have the following results

GameId | TeamId
------------------------
1 50
1 40
2 50
2 40
3 30
3 20


I want to pass in a
TeamId
into my stored procedure (
TeamId = 40
) and return results grouped by
GameId
where one of the
TeamId = 40
.

When I do this it just returns all the records that contain
TeamId = 40
. But I need to maintain the other record and return the following results.

GameId | TeamId
------------------------
1 50
1 40
2 50
2 40


Thanks for any replies in advance.

Answer

You can query from your result where TeamId = 40:

SELECT GameId, TeamId
FROM yourTable
WHERE GameId IN (SELECT GameId FROM yourTable WHERE TeamId = 40);

This should give you the same results you posted