简体   繁体   English

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

[英]How to make PIVOT a table / Cross tab query?

I have made a SELECT statement to get this output: 我已经执行了一条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

Here is the SELECT statement: 这是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;

I want to turn this table into this output with a PIVOT to get the output with the critic rating and user rating as it's own category: 我想用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

My attempt to do this: 我尝试这样做:

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)
)

I get errors, It's probably due to my lack of skill with subqueries, I would like to understand why I am not getting my preferred output and how to fix it. 我收到错误,这可能是由于我缺乏对子查询的技巧,我想了解为什么我没有得到我喜欢的输出以及如何解决它。

EDIT: 编辑:

Here are the tables associated: 以下是相关的表:

File1 (Pastebin) File1(Pastebin)

File2 (Pastebin) File2(Pastebin)

You just have your pivot statement wrong. 您只是将枢纽声明弄错了。 You could substitute your query for the CTE below and it should work. 您可以将查询替换为下面的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

As to, "Why I am not getting my preferred output" 关于“为什么我没有得到我的首选输出”

You are piviting on "Critic Type" for which you have two values ('critic' and user') the aggregrate avg makes sense; 您正在使用“临界类型”,因为它具有两个值(“临界”和“用户”),所以avg是有意义的; but in your example you only have 1 value for each record so min/max would work as well. 但在您的示例中,每条记录只有1个值,因此最小/最大也会起作用。

In a pivot you specify each column in the "in" portion of the pivot, letting the DB engine determine how to orgainze the data based on the values in the "Critic Type" column. 在数据透视图中,您可以在数据透视的“输入”部分中指定每一列,让数据库引擎根据“关键类型”列中的值确定如何组织数据。

Because you have to specify each value, you can't have a dynamic range, without dynamic SQL. 因为必须指定每个值,所以如果没有动态SQL,就无法拥有动态范围。

Note: you can alias the columns in the "IN" portion of the pivot if you desire different names than the values in the "for" pivot field. 注意:如果您希望使用与“ for”数据透视表字段中的值不同的名称,则可以对数据透视表的“ IN”部分中的列进行别名命名。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM