分组依据/查询中没有值
No values from group by / having query
我正在尝试 运行 SQL 服务器中的查询,以便为那些对不止一部电影进行评分的评论者查找电影的标题。请检查以下查询和图表。
SELECT [mov_id] FROM [dbo].[rating$]
GROUP BY [mov_id]
HAVING COUNT ([rev_id])>1
table评分$的数据如下:
mov_id | rev_id | rev_stars | num_o_ratings
--------+--------+-----------+---------------
901 | 9001 | 8.40 | 263575
902 | 9002 | 7.90 | 20207
903 | 9003 | 8.30 | 202778
906 | 9005 | 8.20 | 484746
924 | 9006 | 7.30 |
908 | 9007 | 8.60 | 779489
909 | 9008 | | 227235
910 | 9009 | 3.00 | 195961
911 | 9010 | 8.10 | 203875
912 | 9011 | 8.40 |
914 | 9013 | 7.00 | 862618
915 | 9001 | 7.70 | 830095
916 | 9014 | 4.00 | 642132
925 | 9015 | 7.70 | 81328
918 | 9016 | | 580301
920 | 9017 | 8.10 | 609451
921 | 9018 | 8.00 | 667758
922 | 9019 | 8.40 | 511613
923 | 9020 | 6.70 | 13091
目前我在结果中没有任何值,而我应该检索值 901 和 915 作为值。
任何人帮助理解小姐将不胜感激。
SELECT [mov_id] FROM [dbo].[rating$]
GROUP BY [mov_id]
HAVING COUNT ([rev_id])>1
你不能同时查询这两个东西。因此,您可以使用子查询来获取感兴趣的评论者。或者使用 window 函数计算出哪些行有效,例如
declare @Rating table (mov_id int, rev_id int, rev_stars decimal(9,2), num_of_ratings int);
insert into @Rating (mov_id, rev_id, rev_stars, num_of_ratings)
values
(901,9001,8.40,263575),
(902,9002,7.90,20207),
(903,9003,8.30,202778),
(906,9005,8.20,484746),
(924,9006,7.30,null),
(908,9007,8.60,779489),
(909,9008,null,227235),
(910,9009,3.00,195961),
(911,9010,8.10,203875),
(912,9011,8.40,null),
(914,9013,7.00,862618),
(915,9001,7.70,830095),
(916,9014,4.00,642132),
(925,9015,7.70,81328),
(918,9016,null,580301),
(920,9017,8.10,609451),
(921,9018,8.00,667758),
(922,9019,8.40,511613),
(923,9020,6.70,13091);
-- Approach 1: Using Subquery
SELECT mov_id
FROM @Rating
WHERE rev_id IN (
SELECT rev_id
FROM @Rating
GROUP BY rev_id
HAVING COUNT (*) > 1
);
-- Approach 2: Using CTE + Window Function
WITH cte AS (
SELECT mov_id, COUNT(*) OVER (PARTITION BY rev_id) num_reviews
FROM @Rating
)
SELECT *
FROM cte
WHERE num_reviews > 1;
注意 1:我根据您的数据创建了 DDL+DML,所以如果您以后可以这样做,请。
注意 2:请不要使用 link 建议的隐式(逗号分隔)联接。它们远非最佳实践。而是使用适当的连接。
我正在尝试 运行 SQL 服务器中的查询,以便为那些对不止一部电影进行评分的评论者查找电影的标题。请检查以下查询和图表。
SELECT [mov_id] FROM [dbo].[rating$]
GROUP BY [mov_id]
HAVING COUNT ([rev_id])>1
table评分$的数据如下:
mov_id | rev_id | rev_stars | num_o_ratings
--------+--------+-----------+---------------
901 | 9001 | 8.40 | 263575
902 | 9002 | 7.90 | 20207
903 | 9003 | 8.30 | 202778
906 | 9005 | 8.20 | 484746
924 | 9006 | 7.30 |
908 | 9007 | 8.60 | 779489
909 | 9008 | | 227235
910 | 9009 | 3.00 | 195961
911 | 9010 | 8.10 | 203875
912 | 9011 | 8.40 |
914 | 9013 | 7.00 | 862618
915 | 9001 | 7.70 | 830095
916 | 9014 | 4.00 | 642132
925 | 9015 | 7.70 | 81328
918 | 9016 | | 580301
920 | 9017 | 8.10 | 609451
921 | 9018 | 8.00 | 667758
922 | 9019 | 8.40 | 511613
923 | 9020 | 6.70 | 13091
目前我在结果中没有任何值,而我应该检索值 901 和 915 作为值。 任何人帮助理解小姐将不胜感激。
SELECT [mov_id] FROM [dbo].[rating$]
GROUP BY [mov_id]
HAVING COUNT ([rev_id])>1
你不能同时查询这两个东西。因此,您可以使用子查询来获取感兴趣的评论者。或者使用 window 函数计算出哪些行有效,例如
declare @Rating table (mov_id int, rev_id int, rev_stars decimal(9,2), num_of_ratings int);
insert into @Rating (mov_id, rev_id, rev_stars, num_of_ratings)
values
(901,9001,8.40,263575),
(902,9002,7.90,20207),
(903,9003,8.30,202778),
(906,9005,8.20,484746),
(924,9006,7.30,null),
(908,9007,8.60,779489),
(909,9008,null,227235),
(910,9009,3.00,195961),
(911,9010,8.10,203875),
(912,9011,8.40,null),
(914,9013,7.00,862618),
(915,9001,7.70,830095),
(916,9014,4.00,642132),
(925,9015,7.70,81328),
(918,9016,null,580301),
(920,9017,8.10,609451),
(921,9018,8.00,667758),
(922,9019,8.40,511613),
(923,9020,6.70,13091);
-- Approach 1: Using Subquery
SELECT mov_id
FROM @Rating
WHERE rev_id IN (
SELECT rev_id
FROM @Rating
GROUP BY rev_id
HAVING COUNT (*) > 1
);
-- Approach 2: Using CTE + Window Function
WITH cte AS (
SELECT mov_id, COUNT(*) OVER (PARTITION BY rev_id) num_reviews
FROM @Rating
)
SELECT *
FROM cte
WHERE num_reviews > 1;
注意 1:我根据您的数据创建了 DDL+DML,所以如果您以后可以这样做,请。
注意 2:请不要使用 link 建议的隐式(逗号分隔)联接。它们远非最佳实践。而是使用适当的连接。