【问题标题】:imdb dataset select movie count according to year with movies with only female castimdb 数据集根据年份选择只有女性演员的电影数量
【发布时间】:2020-08-03 08:58:51
【问题描述】:

每年报告当年只有女性演员的电影的百分比,以及当年制作的电影总数。例如,一个答案是:1990 31.81 13522 表示 1990 年有 13,522 部电影,其中 31.81% 只有女性演员。你不需要四舍五入你的答案。

以下代码

select a.year, a.c*100.00/b.c as percentage, b.c as total_overall
from (select z.year, count(*) as c
      from movie z
      where not exists (select *
                        from person x,M_cast xy
                        where x.pid = xy.pid and xy.mid = z.mid and x.Gender!='Female')
      group by z.year) a,
     (select z.year, count(*) as c from movie z group by z.year) b
where a.year=b.year
order by a.year;

以下代码不起作用

select z.year, count(*)
from movie z
where not exists (select *
                  from actor x, casts xy
                  where x.id = xy.pid and xy.mid = z.id and x.gender!='F')
group by z.year;

请告诉我查询只有女性的电影如何选择 我得到以下输出

指导我如何选择只有女性演员的电影

如何为上述语句编写sql查询

【问题讨论】:

    标签: python mysql sql count average


    【解决方案1】:

    您可以使用两个级别的聚合:

    select movie_year, count(*) no_movies, avg(has_male_actor = 0) ratio
    from (
        select m.year movie_year, max(p.gender = 'Male') has_male_actor
        from movie m
        inner join m_cast mc on mc.mid = m.mid
        inner join person p on pid = mc.pid
        group by m.mid, m.year
    ) t
    group by movie_year
    

    子查询为每部电影生成一行,并带有一个标志,指示演员中是否有男性演员。然后,外部查询按年份聚合,并计算只有女性演员的电影的数量和比例(表示为01 之间的小数)。

    【讨论】:

    • 感谢您的建议,但我想计算电影的百分比
    • @pratikpatil 。 . .你运行查询了吗?这似乎可以满足您的需求。
    • 它不返回任何东西
    【解决方案2】:

    @gmb 给出的答案是正确的,因为他不知道您的数据是否包含任何噪声,并且他根据您在图像中上面提到的架构编写了查询。但是当我遇到同样的问题语句时,我运行它,发现电影表的年份列在它前面包含一些spaces and roman values,例如:-'I 1945',经过一些修改,它在你提到的@987654322 上成功运行@ 并返回正确的结果。

    这是对上述问题陈述的修改查询:-

    select  movie_year, count(*) no_movies, avg(has_male_actor = 0)*100 ratio
    from (
        select CAST(SUBSTR(m.year,-4) AS UNSIGNED) movie_year, max(p.gender = 'Male') has_male_actor
        from movie m
        inner join m_cast mc on TRIM(mc.mid) = TRIM(m.mid)
        inner join person p on TRIM(p.pid) = TRIM(mc.pid)
        group by m.mid, m.year
    ) t
    group by movie_year
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2016-06-19
      • 2020-07-02
      • 1970-01-01
      • 2017-05-08
      相关资源
      最近更新 更多