代码之家  ›  专栏  ›  技术社区  ›  Pierre97

如何使用“与枢轴合并”

  •  1
  • Pierre97  · 技术社区  · 8 年前

    您好,我正试图找出如何使用合并和枢轴,这样我就可以用零替换空值。以下是我所拥有的:

         SELECT * FROM ( SELECT MovieTitle,AwardResultDesc, COALESCE(COUNT(p.AwardResultID),'0') AS T
         FROM tblMovie t1
                INNER JOIN tblAwardDetail p
                  ON p.MovieID = t1.MovieID
                INNER JOIN tblAwardResult c
                  ON c.AwardResultID = p.AwardResultID
        GROUP BY MovieTitle, AwardResultDesc,p.AwardResultID)
    
        PIVOT
        (
            max(T) FOR AwardResultDesc IN ('Won' AS "Won",'Nominated' AS "Nominated")
        ) 
        ORDER BY MovieTitle;
    

    我的轴心似乎出于某种原因忽略了我的合并,有什么建议吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sentinel    8 年前

    tblMovie

    WITH dta AS (
     SELECT MovieTitle
          , AwardResultDesc
       FROM tblAwardResult ar
       JOIN tblAwardDetail ad
         ON ad.AwardResultID = ar.AwardResultID
      RIGHT JOIN tblMovie m
         ON m.MovieID = ad.MovieID
    )
     SELECT *
       FROM dta
       PIVOT( count(*)
         FOR AwardResultDesc IN( 'Won' AS "Won"
                               , 'Nominated' AS "Nominated" ) )
      ORDER BY MovieTitle;