SQL - 如何实现矩阵?

时间:2018-05-26 21:13:47

标签: sql-server matrix

我对SQL比较陌生,并且在下面的输出中给自己留下了深刻的印象。

'HomeTeamSequence'和'AwayTeamSequence'列分别是主队和客队的最后两个结果。 'FTR'是该特定比赛的实际结果。

   with games as (

   SELECT t.Div, t.Date, t.HomeTeam, t.AwayTeam, t.FTHG, t.FTAG, t.FTR, 
   t.MatchID, U.team_id
   FROM All_Games.[dbo].[All_Games_Full] t 
   cross apply (values(HomeTeam), (AwayTeam)) as u(team_id)

    ), 

    games_update as (

    select *, 
    case when HomeTeam = team_id and FTR = 'H' then 'HW'
         WHEN HomeTeam = team_id and FTR = 'D' then 'HD'
         WHEN HomeTeam = team_id and FTR = 'A' then 'HL'
         when AwayTeam = team_id and FTR = 'A' then 'AW'
         WHEN AwayTeam = team_id and FTR = 'D' then 'AD'
         WHEN AwayTeam = team_id and FTR = 'H' then 'AL'
         ELSE NULL END NewFTR
    from games

    ), seq as (

    select MatchID, Div, Date, team_id,
    concat(case when LAG(team_id,2) over (order by team_id, Date) != team_id then null else LAG(NewFTR, 2) OVER (ORDER BY team_id, Date) end, ' ' ,
    case when LAG(team_id,1) over (order by team_id, Date) != team_id then null else LAG(NewFTR, 1) OVER (ORDER BY team_id, Date) end) as lastgame,
    NewFTR AS latestgame
    from games_update

    )

    select a.MatchID, a.Div, a.Date, a.HomeTeam, a.AwayTeam,
    case when a.HomeTeam = b.team_id then b.lastgame end HomeTeamSequence,
    case when a.AwayTeam = c.team_id then c.lastgame end AwayTeamSequence, 
    a.FTR
    from All_Games.[dbo].[All_Games_Full] a
    left join  seq b
    on a.MatchID = b.MatchID and a.HomeTeam = b.team_id
    left join seq c
    on a.MatchID = c.MatchID and a.AwayTeam = c.team_id
    where a.Div = 'SP1'
    ORDER BY MatchID

示例输出:

MatchID Div    Date     HomeTeam    AwayTeam   HomeTeamSequence    AwayTeamSequence    FTR
135038  SP1 1995-10-01  La Coruna   Albacete    HW AD              AL HW        H
135039  SP1 1995-10-01  Merida      Salamanca   HL AL              AL HD        D
135040  SP1 1995-10-01  Oviedo      Betis       AW HD              AW HD        A
135041  SP1 1995-10-01  Sevilla     Ath Madrid  HW AD              AW HW        D
135042  SP1 1995-10-01  Valladolid  Tenerife    HD AD              AD HD        H
135043  SP1 1995-10-01  Vallecano   Valencia    HL AL              AW HD        H
135044  SP1 1995-10-01  Zaragoza    Compostela  HL AD              AD HW        H
135045  SP1 1995-10-04  Albacete    Valladolid  HW AL              AD HW        H
135046  SP1 1995-10-04  Barcelona   Oviedo      HW AD              HD HL        H 

我想要实现的是以下矩阵(只是标题/行组合的示例)。只关注'FTR'='H':

      AD AD    AD AL   AD AW   AD HD   AD HL
AD AD   0       0       0       0       0
AD AL   0       0       0       0       0
AD AW   0       0       0       0       0
AD HD   3       0       1       1       1
AD HL   1       2       3       2       2
AD HW   1       3       3       1       1
AL AD   0       0       0       0       0
AL AL   0       0       0       0       0 

我在上面的查询中添加了以下内容以测试一些组合,但它看起来并不接近。

    select
    case when HomeTeamSequence = 'HW HW' THEN COUNT(*) else 0 END 'HW HW',
    case when HomeTeamSequence = 'HW HD' THEN COUNT(*) else 0 END 'HW HD',
    case when HomeTeamSequence = 'HW HL' THEN COUNT(*) else 0 END 'HW HL',
    case when HomeTeamSequence = 'HW AW' THEN COUNT(*) else 0 END 'HW AW',
    case when HomeTeamSequence = 'HW AD' THEN COUNT(*) else 0 END 'HW AD',
    case when HomeTeamSequence = 'HW AL' THEN COUNT(*) else 0 END 'HW AL'
    from fixture_list
    WHERE ftr = 'H'
    GROUP BY HomeTeamSequence

我假设我需要在那里设置一些支点。

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

通过在初始查询中添加以下内容来解决此问题:

    select Team1,
    coalesce(max(case when Team2 = 'HW HW' then Total end), 0) as 'HW HW',
    coalesce(max(case when Team2 = 'HW HD' then Total end), 0) as 'HW HD',
    coalesce(max(case when Team2 = 'HW HL' then Total end), 0) as 'HW HL'
    --creates row headers
    --repeat for other combinations
    from
    (
    select AwayTeamSequence Team1, HomeTeamSequence Team2,
    COUNT(*) as Total
    from fixture_list
    where FTR = 'H'
    group by HomeTeamSequence, AwayTeamSequence
    ) src
    group by Team1;

以下链接帮助我得到了这个答案:SQL Statement - SQL Matrix