我有电影和电影类型。我展示的是电影类型,正确的是,电影类型中存在的电影数量。这使用一个组在电影类型和计数电影ids。
但是,我不知道如何使用组和案例语句,例如显示电影类型“喜剧”和“动作”的每一种电影类型的计数,而不是显示其他类型放映的每一部电影的数量,然后是不属于喜剧或动作的其余电影的计数,例如:
Action 10
Comedy 7
Remaining 15
你知道要实现这个目标需要什么吗?因为在电影类型与“喜剧”或“动作”不同的情况下,总有必要按电影类型分组,但在这种情况下,有必要对那些不属于“动作”和“喜剧”类型的电影进行分类。
发布于 2019-02-25 13:16:21
重复case
表达式:
select (case when genre in ('Action', 'Comedy') then genre
else 'Remaining'
end) as new_genre,
count(*)
from t
group by (case when genre in ('Action', 'Comedy') then genre
else 'Remaining'
end);
一些数据库在group by
中识别列别名,因此有时可以将其简化为:
select (case when genre in ('Action', 'Comedy') then genre
else 'Remaining'
end) as new_genre,
count(*)
from t
group by new_genre;
发布于 2019-02-25 13:24:34
对case
表达式使用派生表。然后GROUP BY
其结果:
select genre, count(*)
from
(
select case when genre in ('Action', 'Comedy') then genre
else 'Remaining'
end as genre
from tablename
) dt
group by genre
符合ANSI SQL!
发布于 2019-02-25 13:16:19
你可以在下面试试-
select case when genres in ('Comedy','Action') then genres
else 'Remaining' end as genre,count(*) from tablename
group by case when genres in ('Comedy','Action') then genres
else 'Remaining' end
https://stackoverflow.com/questions/54867072
复制相似问题