多列上的SQL Pivot

时间:2019-01-09 19:19:39

标签: sql-server tsql pivot

我有一个查询,我要查询赔率和足球成绩:

SELECT
    Div, BookieResult, COUNT(BookieResult) AS WinLossCount, OddsCategory
FROM 
    [dbo].[MatchOutcomes]
GROUP BY
    div, OddsCategory, BookieResult

这将返回如下结果:

enter image description here

我正在寻找结果,使数据更具可读性,并且每个类似的记录都在一行上,例如:

enter image description here

我一直试图以许多不同的方式来旋转数据,但是似乎无法在SQL中获得它。有谁知道上面的布局是否可以实现?

谢谢。

3 个答案:

答案 0 :(得分:1)

;WITH Table AS
(
  Select Div, BookieResult, Count(BookieResult) as WinLossCount, 
  OddsCategory
  FROM [dbo].[MatchOutcomes]
  group by div, OddsCategory, BookieResult
)
select Res1.Div
           ,Res1.[Loss]
           ,Res1.[Win]
           ,Res1.[oddscategory]
           ,Res2.[Loss]
           ,Res2.[Win]
           ,Res2.[oddscategory]
           ,Res3.[Loss]
           ,Res3.[Win]
           ,Res3.[oddscategory] 
           ,Res4.[Loss]
           ,Res4.[Win]
           ,Res4.[oddscategory]
    from
    (
    select Div
               ,[Loss]
               ,[Win]
               ,[oddscategory]
        from Table
        pivot
        (
             max(winlosscount)
             for BookResult IN ([Loose],[Win])
        )Rs1
        where oddscategory = 0 
     )Res1
    LEFT JOIN
    (
    select Div
               ,[Loss]
               ,[Win]
               ,[oddscategory]
        from Table
        pivot
        (
             max(winlosscount)
             for BookResult IN ([Loose],[Win])
        )Rs2
        where oddscategory = 1 
     )Res2 ON Res2.Div = Res1.Div
    LEFT JOIN
    (
    select Div
               ,[Loss]
               ,[Win]
               ,[oddscategory]
        from Table
        pivot
        (
             max(winlosscount)
             for BookResult IN ([Loose],[Win])
        )Rs3
        where oddscategory = 2 
     )Res3 Res3.Div = Res1.Div
    LEFT JOIN
    (
    select Div
               ,[Loss]
               ,[Win]
               ,[oddscategory]
        from Table
        pivot
        (
             max(winlosscount)
             for BookResult IN ([Loose],[Win])
        )Rs4
        where oddscategory = 3 
     )Res4 Res4.Div = Res1.Div

答案 1 :(得分:0)

我尝试如下所示,因为您对当前o / p进行了两次修改,分别是BookieResult
(Loss/Win)
和最后一个是OddsCategory as(0,1,2,3),同样仅绕过这些列以反映当前。希望它对您有用。

       Select Div, BookieResult, 
       WinLossCount, OddsCategory from
        (Select Div, BookieResult, Count(BookieResult) as 
       WinLossCount, OddsCategory
      FROM [dbo].[MatchOutcomes]
      group by div, OddsCategory, BookieResult)

        PIVOT
          (
   Count(BookieResult) as 
     WinLossCount, OddsCategory
   for OddsCategory IN (0,1,2,3) And 
      BookieResult IN ("Loss","Win")
     )

   )

答案 2 :(得分:0)

设法得到它。感谢您的帮助,他们带领我朝着正确的方向

  select * from (
  Select Div, CONCAT(BookieResult,OddsCategory) as [fg], Count(BookieResult) as 
  WinLossCount
  FROM [dbo].[MatchOutcomes]
  group by div, OddsCategory, BookieResult) as s
  PIVOT
  (
  SUM(WinLossCount)
  FOR [fg] IN (
  Lose0,
Win0,
Lose1,
Win1,
Lose2,
Win2,
Lose3,
Win3)
) AS PVT
order by div desc

Final Result