简体   繁体   English

联合 - 关键字“order”附近的语法不正确

[英]Unioning - Incorrect syntax near the keyword 'order'

Table: Movies表:电影

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| movie_id      | int     |
| title         | varchar |
+---------------+---------+

movie_id is the primary key for this table. movie_id 是该表的主键。 title is the name of the movie. title 是电影的名字。 Table: Users表:用户

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | int     |
| name          | varchar |
+---------------+---------+

user_id is the primary key for this table. user_id 是该表的主键。 Table: Movie_Rating表:Movie_Rating

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| movie_id      | int     |
| user_id       | int     |
| rating        | int     |
| created_at    | date    |
+---------------+---------+

(movie_id, user_id) is the primary key for this table. (movie_id, user_id) 是该表的主键。 This table contains the rating of a movie by a user in their review.此表包含用户在评论中对电影的评分。 created_at is the user's review date. created_at 是用户的评论日期。

Write the following SQL query:编写以下 SQL 查询:

Find the name of the user who has rated the greatest number of the movies.查找对电影数量最多的用户的姓名。 In case of a tie, return lexicographically smaller user name.如果出现平局,则返回字典序较小的用户名。

Find the movie name with the highest average rating in February 2020. In case of a tie, return lexicographically smaller movie name.查找 2020 年 2 月平均评分最高的电影名称。如果出现平局,则返回字典序较小的电影名称。

Query is returned in 2 rows, the query result format is in the folowing example:查询分2行返回,查询结果格式如下例:

Movies table:电影表:

+-------------+--------------+
| movie_id    |  title       |
+-------------+--------------+
| 1           | Avengers     |
| 2           | Frozen 2     |
| 3           | Joker        |
+-------------+--------------+

Users table:用户表:

+-------------+--------------+
| user_id     |  name        |
+-------------+--------------+
| 1           | Daniel       |
| 2           | Monica       |
| 3           | Maria        |
| 4           | James        |
+-------------+--------------+

Movie_Rating table: Movie_Rating 表:

+-------------+--------------+--------------+-------------+
| movie_id    | user_id      | rating       | created_at  |
+-------------+--------------+--------------+-------------+
| 1           | 1            | 3            | 2020-01-12  |
| 1           | 2            | 4            | 2020-02-11  |
| 1           | 3            | 2            | 2020-02-12  |
| 1           | 4            | 1            | 2020-01-01  |
| 2           | 1            | 5            | 2020-02-17  | 
| 2           | 2            | 2            | 2020-02-01  | 
| 2           | 3            | 2            | 2020-03-01  |
| 3           | 1            | 3            | 2020-02-22  | 
| 3           | 2            | 4            | 2020-02-25  | 
+-------------+--------------+--------------+-------------+

Result table:结果表:

+--------------+
| results      |
+--------------+
| Daniel       |
| Frozen 2     |
+--------------+

Daniel and Maria have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically. Daniel 和 Maria 对 3 部电影(《复仇者联盟》、《冰雪奇缘 2》和《小丑》)进行了评分,但 Daniel 的字典序较小。 Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically. 《冰雪奇缘 2》和《小丑》在 2 月份的平均评分为 3.5,但《冰雪奇缘 2》的字典序较小。

Here is my solution:这是我的解决方案:

(
  select top 1 u.name as results 
  from users as u
  join movie_rating as m on u.user_id = m.user_id
  group by user_id
  order by count(rating) desc, name
)
union all
(
  select top 1 m.title as results 
  from movies as m
  join movie_rating as r on m.movie_id = r.movie_id
  where CONVERT(varchar(7), r.created_at, 121) = '2020-02'
  group by m.title
  order by avg(rating) desc, m.title
);

I get this error that I can't figure it out我收到此错误,我无法弄清楚

[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near the keyword 'order'. [42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]关键字“order”附近的语法不正确。 (156) (SQLExecDirectW) (156) (SQLExecDirectW)

any idea and how to fix it?任何想法以及如何解决它?

OK I think this might work, as you didn't provide DDL/DML statements I haven't been able to test it.好的,我认为这可能有效,因为您没有提供 DDL/DML 语句,我无法对其进行测试。

Points to note:注意事项:

  1. Instead of a sub-query you need a full derived query for each part of the union.您需要对联合的每个部分进行完整的派生查询,而不是子查询。
  2. For any computed order by column you need to compute it in the select and then order by the alias.对于任何order by列计算的order by您需要在选择中计算它,然后order by别名order by
  3. You need to group by u.[name] not u.[user_id] , you have to group by whatever you are selecting.您需要group by u.[name]而不是u.[user_id] group by ,您必须group by您选择group by任何内容group by
  4. You had some missing aliases which caused an ambiguous column error.您丢失了一些别名,这导致了不明确的列错误。
  5. If you want to avg an int column you have to convert it to a decimal of some sort to avoid SQL Server automatically rounding it to an int .如果要对int列求avg ,则必须将其转换为某种decimal ,以避免 SQL Server 自动将其四舍五入为int
    select results, rating
    from (
      select top 1 u.[name] as results, count(convert(decimal(9,2),m.rating)) rating 
      from users as u
      join movie_rating as m on u.[user_id] = m.[user_id]
      group by u.[name]
      order by rating desc, results asc
    ) X

    union all

    select results, rating
    from (
      select top 1 m.title as results, avg(convert(decimal(9,2),r.rating)) Rating
      from movies as m
      join movie_rating as r on m.movie_id = r.movie_id
      where CONVERT(varchar(7), r.created_at, 121) = '2020-02'
      group by m.title
      order by rating desc, results asc
    ) X;

Per @Dale K, here is revised solution that works.根据@Dale K,这里是有效的修订解决方案。

select a.name as results from (
  select top 1 u.name 
  from users as u
  join movie_rating as m on u.user_id = m.user_id
  group by u.name
  order by count(m.rating) desc, u.name
) a
union all
select title as results from (
  select top 1 m.title 
  from movies as m
  join movie_rating as r on m.movie_id = r.movie_id
  where CONVERT(varchar(7), r.created_at, 121) = '2020-02'
  group by m.title
  order by avg(cast(rating as decimal)) desc, m.title
) b;

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

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