【问题标题】:SQL comparing two select clauses and grouping sumsSQL比较两个选择子句和分组总和
【发布时间】:2018-04-30 13:38:01
【问题描述】:

我目前正在大学从事一个项目,但在使用 SQL 中的特定查询时遇到问题。 tables

我正在尝试从 DEPARTEMENTS 检索 NOM 列表,其中使用 ANNEE(年)和 LABEL(级别)毕业的女性比男性多,我们必须通过 HTML 表单输入自己。

到目前为止我有这个:

select distinct NOM from 
    DEPARTEMENTS as d1, 
    NB_DIPLOMES_DEPT as d2, 
    NIVEAUX_DIPLOMES as d3 
  where d1.NUM=d2.DEPT and d2.NIVEAU=d3.ID 
    and (select sum(num) 
  from NB_DIPLOMES_DEPT 
  where sexe='F') > 
    (select sum(num) from NB_DIPLOMES_DEPT where sexe='H') 
        and ANNEE='{0}' AND LABEL='{1}'".format(year,level)

但我不知道如何比较男女人数的总和。

【问题讨论】:

  • NIVEAUX_DIPLOMES 表有什么用?为什么需要它?
  • @TheImpaler 因为您从中获得了 LABEL(我使用 html 中的选择标签检索的值)

标签: python html sql


【解决方案1】:

使用子查询时,请记住与主 FROM 连接。否则你会得到意想不到的结果。

select NOM 
from DEPARTEMENTS as dep 
where (
    select sum(num)
    from NB_DIPLOMES_DEPT ndd
        , NIVEAUX_DIPLOMES nd
    where ndd.sexe='F'
        and ndd.DEPT = dep.NUM
        and ndd.ANNE = ?
        and ndd.NIVEAU = nd.ID
        and nd.LABEL = ?
) > (
    select sum(num) 
    from NB_DIPLOMES_DEPT 
    where sexe='H'
        and ndd.DEPT = dep.NUM
        and ndd.ANNE = ?
        and ndd.NIVEAU = nd.ID
        and nd.LABEL = ?
) 

在你的 SQL 中,你只在哪里加入

DEPARTEMENTS as d1, 
NB_DIPLOMES_DEPT as d2, 
NIVEAUX_DIPLOMES as d3

子查询总是返回所有值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2022-08-02
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多