【问题标题】:SQL count sum and joining tableSQL 计数和连接表
【发布时间】:2015-03-16 07:52:59
【问题描述】:

我在 SQL 课程中遇到了一个问题,但没有解决方案。我的查询只需要显示女性和男性演员数量相同的电影。

我有三张桌子: (表:字段 1,字段 2):

Casting: actor_number, movie_number
Actor_List: id, name, gender
Movie_List: id, movie_name

【问题讨论】:

  • 你尝试过什么吗?
  • 如果你只是在这里得到一个解决方案,那么你将如何学习?创建表、添加数据并尝试不同的 SQL 构造!
  • 您需要调查SQL中的JOIN子句。
  • 我自己尝试了count()和join子句;我真的不知道如何将它们嵌套在一起以使这项工作......

标签: sql oracle count


【解决方案1】:

子查询使用一点 CASE() 技巧来有条件地增加计数(即countif)。子查询分解语法意味着我们只执行一次查询。

with cte as (
    select m.movie_name
           , sum(case when a.gender = 'M' then 1 else 0 end) as male_tot 
           , sum(case when a.gender = 'F' then 1 else 0 end) as female_tot 
    from casting c
         join movie_list m
          on c.movie_number = m.id
         join actor_list a
          on c.actor_number = a.id
   group by m.name
    )
select cte.* 
from cte
where cte.male_tot = cte.female_tot ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多