------------------------------------------------------------------
-- CREATE A TEMPORARY REGISTERED PLAYERS TABLE TO FURTHER QUERY --
------------------------------------------------------------------
SELECT *
INTO
[RegisteredPlayers]
FROM
[func_GetRegisteredPlayers](@PollID)
-------------------------------------------
-- TEMPORARY TABLE CREATED, NOW QUERY IT --
-------------------------------------------
-- TOTAL REGISTERED PLAYERS (PROFILES) --
SELECT @RegisteredPlayersCount = COUNT([ID]) FROM [RegisteredPlayers]
-- PLAYERS RATIO BY GENDER : MALE --
SELECT @MalePlayersCount = COUNT([ID]) FROM [RegisteredPlayers] WHERE [Gender] = 'male'
-- PLAYERS RATIO BY GENDER : FEMALE --
SELECT @FemalePlayersCount = COUNT([ID]) FROM [RegisteredPlayers] WHERE [Gender] = 'female'
-- PLAYERS RATIO BY AGE GROUP : 18-29 --
SELECT @PlayersAge18Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '18-29'
-- PLAYERS RATIO BY AGE GROUP : 30-49 --
SELECT @PlayersAge30Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '30-49'
-- PLAYERS RATIO BY AGE GROUP : 50-64 --
SELECT @PlayersAge50Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '50-64'
-- PLAYERS RATIO BY AGE GROUP : 65+ --
SELECT @PlayersAge65Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '65'
-------------------------------------------------------------------
-- CLEAN UP DATABASE FROM THE TEMPORARY TABLE (NO LONGER NEEDED) --
-------------------------------------------------------------------
DROP TABLE [RegisteredPlayers]
CREATE FUNCTION [dbo].[func_GetRegisteredPlayers]
(
@PollID INT
)
RETURNS TABLE
AS
RETURN
(
SELECT
[ID],
[GUID],
[AgeGap],
[Gender]
FROM
[Players] AS [PL]
WHERE
(
[GUID] IN
(
SELECT
DISTINCT [MR].[PlayerGUID]
FROM
[MatchupResults] AS [MR]
INNER JOIN [MatchupItems] AS [MI] ON [MI].[ID] = [MR].[SelectedID]
WHERE
[MI].[PollID] = @PollID
)
)
)