SELECT
[P].[GUID] As [PlayerGUID],
[dbo].[GetPlayerTotalPickCount]([P].[GUID]) [PickCount]
FROM
[Players] [P]
CREATE FUNCTION [dbo].[GetPlayerTotalPickCount]
(
@PlayerGUID NVARCHAR(50) -- The foreign key to fetch corresponding records
)
RETURNS INT
AS
BEGIN
DECLARE @PickCount INT;
SELECT
@PickCount = COUNT([MR].[ID])
FROM
[MatchupResults] [MR]
WHERE
[MR].[PlayerGUID] = @PlayerGUID
RETURN
(
SELECT @PickCount
)
END