【问题标题】:Find number of position n each deparment查找每个部门的职位数量
【发布时间】:2022-11-22 19:10:03
【问题描述】:

犯罪嫌疑人表

有多少女性和多少男性在汽车盗窃案中作案。

餐桌犯罪: 1 2 3 等等...

表嫌疑人: 1 2 3 等等...

我做了一些工作,但我的灵魂没有被 SQL 接受

select name, sex, crime_type, count(case when sex='Male' then 1 end) as male_cnt, count(case when sex='Female' 然后 1 end) as female_cnt 来自犯罪嫌疑人 NATURAL JOIN crimes where crime_type='Car Theft' group 按姓名、性别;

块引用

【问题讨论】:

  • 您好,欢迎来到 SO。艾库尔。您能否用链接或图像以外的某种格式的测试数据更新问题?谢谢
  • 乍一看,您在 GROUP BY 子句中缺少 CRIME_TYPE:GROUP BY name, sex, crime_type;
  • 问题标题与问题正文有何关系?

标签: oracle find max


【解决方案1】:

有多少女性和多少男性在汽车盗窃案中作案。

该问题仅询问性别和计数,因此您无需在输出中包含姓名或犯罪类型。

如果你想要一行,那么你想要聚合整个结果集,并且可以对每个性别使用你的条件聚合:

select count(case when s.sex='Male' then 1 end) as male_cnt,
       count(case when s.sex='Female' then 1 end) as female_cnt
from   suspects s
       INNER JOIN crimes c
       ON (s.suspect_id = c.suspect_id)
where  c.crime_type='Car Theft';

或者,如果你想要每性别一行,那么:

select s.sex,
       count(*) as cnt
from   suspects s
       INNER JOIN crimes c
       ON (s.suspect_id = c.suspect_id)
where  c.crime_type='Car Theft'
GROUP BY s.sex;

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2021-12-19
    • 2018-11-23
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多