0

I need an efficient way to convert the data returned in rows to columns for a football database I'm working on.

2 tables, one holding fixtures, one holding predictions.

Fixtures:

enter image description here

Predictions

enter image description here

I would like the data in those two table returned as follows (Can't post another link, but this query will give you an idea):

SELECT  1 as UserID, 
    2 as [Stoke vs Man United], 
    1 as [Bournemouth vs Crystal Palace], 
    2 as [Swansea vs West Brom],
    1 as [Chelsea vs Watford],
    3 as [Liverpool vs Leicester],
    1 as [Tottenham vs Norwich],
    2 as [Aston Villa vs West Ham]

The problem is, each week, the chosen teams will change so I couldn't get a pivot query to work. Any ideas?

1
  • 6
    I couldn't get a pivot query to work. Show your trial, tag question properly (SQL Server) and prepare sqlfiddle.com. Anyway this question is 1001th duplicate of the same problem pivot/dynamic pivot. Dec 28, 2015 at 20:21

2 Answers 2

0

2 things you need to know about to make this work.

  1. Dynamic Pivot
  2. sp_executesql

Example

-- Param Definitions
DECLARE @Sql NVARCHAR(MAX),
        @Columns NVARCHAR(MAX),
        @WeekNo INT = 6,
        @UserID INT = 1,
        @Params NVARCHAR(MAX) = N'@Week INT, @User INT'

-- Dynamic Column Names
SELECT  @Columns = COALESCE(@Columns + ',','') + QUOTENAME(HomeTeamName + ' vs ' + AwayTeamName)
FROM    usr_SS_Fixtures
WHERE   WeekNo = @WeekNo

-- Dynamic SQL
SET @Sql = N'
SELECT *
FROM
(
    SELECT 
        pred.UserId,
        pred.ResultCode,
        CONCAT(HomeTeamName,'' vs '', AwayTeamName) AS Teams
    FROM
        usr_SS_Fixtures fix
        JOIN usr_SS_Predictions pred ON fix.EventID = pred.EventID AND fix.WeekNo = pred.WeekNo
    WHERE 
        fix.WeekNo = @Week
        AND pred.UserID = @User
) t
PIVOT 
(
    MAX(ResultCode)
    FOR Teams IN (' + @Columns + ')
) p
'

-- Executes the dynamic SQL
EXECUTE sp_executesql @Sql, @Params, @Week = @WeekNo, @User = @UserID

DEMO

0

You have an interesting variation on the normal dynamic pivot problem, due to the known-in-advance maximum number of games, that makes one possible solution a little simpler. Try the SQL below; it creates a first row that contains the desired column headings, which may be sufficient for simple display purposes.

declare @weekNo as int = 6
       ,@UserID as int = 1;   
with
fixture(UserID, EventID, [Value], GameNo) as (
    select -1, EventID, AwayTeamName + ' at ' + HomeTeamName
        , row_number() over (partition by WeekNo order by EventID)
    from usr_SS_Fixtures
),
prediction(UserID, EventID, [Value], GameNo)  as (
    select UserID, EventID, ResultCode
        , row_number() over (partition by WeekNo order by EventID)
    from usr_SS_Predictions
),
data as (
    select
         WeekNo, UserID
        ,[1],[2],[3],[4],[5],[6],[7]
    from (
        select WeekNo, UserID, EventID, [Value] from fixture
        union all
        select WeekNo, UserID, EventID, [Value] from prediction
    ) T
    pivot (
        max([Value]) for GameNo in ([1],[2],[3],[4],[5],[6],[7])
    ) pvt
)
select
    UserID,[1],[2],[3],[4],[5],[6],[7]
from data
where WeekNo = @WeekNo
order by
    UserID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.