﻿CREATE PROCEDURE [session].[ReadSessionPlayerCount]
	@PlayerName [user].UserName
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @PlayerId as BIGINT;
	SELECT @PlayerId = Id
	FROM [user].[User] 
	WHERE [Name] = @PlayerName;

	-- Result set of sessions which @PlayerName participates in.
	SELECT
		[Name],
		CASE 
			WHEN Player2Id IS NULL THEN 1
			ELSE 2 
		END AS PlayerCount
	FROM [session].[Session]
	WHERE Player1Id = @PlayerId OR Player2Id = @PlayerId;

	-- Result set of sessions which @PlayerName does not participate in.
	SELECT
		[Name],
		CASE 
			WHEN Player2Id IS NULL THEN 1
			ELSE 2 
		END AS PlayerCount
	FROM [session].[Session]
	WHERE Player1Id <> @PlayerId AND ISNULL(Player2Id, 0) <> @PlayerId;
END