如何使PIVOT成为表/交叉表查询?

时间:2016-11-30 16:38:17

标签: sql oracle

我已经发出SELECT语句来获取此输出:

MOVIE                           Critic Type  Average Rating
------------------------        ------------ --------------
30 Minutes or Less              Critic                 2.00
30 Minutes or Less              User                   4.20
A Lonely Place to Die           Critic                10.00
A Lonely Place to Die           User                   8.50
Taken                           Critic                 6.17
Taken                           User                   7.27
Taken 2                         Critic                 4.00
Taken 2                         User                   6.29

8 rows selected

以下是SELECT声明:

SELECT MovieTitle AS "MOVIE", TBLCRITICCLASS.CRITICCLASSDESC AS "Critic Type", AVG(TBLREVIEW.REVIEWSTAR) AS "Average Rating"
FROM TBLMOVIE 
INNER JOIN TBLREVIEW ON TBLMOVIE.MOVIEID = TBLREVIEW.MOVIEID 
INNER JOIN TBLCRITIC ON TBLREVIEW.CRITICID = TBLCRITIC.CRITICID 
INNER JOIN TBLCRITICCLASS ON TBLCRITIC.CRITICCLASSID = TBLCRITICCLASS.CRITICCLASSID 
GROUP BY MovieTitle, TBLCRITICCLASS.CRITICCLASSDESC
ORDER BY Movietitle;

我想用PIVOT将此表格转换为此输出,以获得具有评论评级和用户评级的输出,因为它是自己的类别:

MOVIE                           Critic Rating   User Rating
------------------------        -------------   -----------
30 Minutes or Less              2.00                   4.20
A Lonely Place to Die           10.00                  8.50
Taken                           6.17                   7.27
Taken 2                         4.00                   6.29

我尝试这样做:

SELECT * FROM
(
  SELECT MovieTitle AS "MOVIE", AVG(TBLREVIEW.REVIEWSTAR) AS "Critic Rating", AVG(TBLREVIEW.REVIEWSTAR) AS "User Rating"
  FROM TBLMOVIE 
  INNER JOIN TBLREVIEW ON TBLMOVIE.MOVIEID = TBLREVIEW.MOVIEID 
  INNER JOIN TBLCRITIC ON TBLREVIEW.CRITICID = TBLCRITIC.CRITICID 
  INNER JOIN TBLCRITICCLASS ON TBLCRITIC.CRITICCLASSID = TBLCRITICCLASS.CRITICCLASSID 
  GROUP BY MovieTitle, TBLCRITICCLASS.CRITICCLASSDESC
  ORDER BY Movietitle
)

PIVOT
(
  AVG(TBLREVIEW.REVIEWSTAR) AS "Critic Rating"
  FOR TBLREVIEW.REVIEWSTAR IN (TBLCRITICCLASS.CRITICCLASSDESC)

  AVG(TBLREVIEW.REVIEWSTAR) AS "User Rating"
  FOR TBLREVIEW.REVIEWSTAR IN (TBLCRITICCLASS.CRITICCLASSDESC)
)

我收到错误,可能是由于我缺乏子查询技能,我想了解为什么我没有得到我喜欢的输出以及如何解决它。

编辑:

以下是相关的表格:

File1 (Pastebin)

File2 (Pastebin)

1 个答案:

答案 0 :(得分:2)

您的枢轴声明错误。你可以用下面的CTE替换你的查询,它应该可以工作。

SELECT * 
FROM (SELECT MovieTitle AS "MOVIE"
           , TBLCRITICCLASS.CRITICCLASSDESC AS      "Critic Type"
           , AVG(TBLREVIEW.REVIEWSTAR) AS "Average Rating"
      FROM TBLMOVIE 
      INNER JOIN TBLREVIEW 
        ON TBLMOVIE.MOVIEID = TBLREVIEW.MOVIEID 
      INNER JOIN TBLCRITIC 
        ON TBLREVIEW.CRITICID = TBLCRITIC.CRITICID 
      INNER JOIN TBLCRITICCLASS 
        ON TBLCRITIC.CRITICCLASSID = TBLCRITICCLASS.CRITICCLASSID 
      GROUP BY MovieTitle, TBLCRITICCLASS.CRITICCLASSDESC
      )
PIVOT (AVG("Average Rating") for "Critic Type" 
   in ('Critic' as "Critic Rating", 'User' as "User Rating")))
ORDER BY Movie

至于,"为什么我没有得到我的首选输出"

你正在批评"评论家类型"对于您有两个值('评论家'和用户'),聚合avg是有意义的;但在你的例子中,每个记录只有1个值,所以min / max也可以。

在数据透视表中,您可以指定" in"中的每一列。枢轴的一部分,让数据库引擎根据"评论家类型"中的值确定如何对数据进行交互。列。

因为你必须指定每个值,所以你没有动态范围,没有动态SQL。

注意:您可以为" IN"中的列添加别名。如果你想要的名称不同于" for"枢轴场。

enter image description here