有关在查询中组合不同计数的SQL问题

时间:2018-11-02 21:49:02

标签: sql oracle

我刚刚在Oracle SQL Developer中为我的研究所启动一个数据库项目,并且在第一个查询中遇到了问题,在该查询中,我必须返回“建筑物名称”,“部门总数”,“拥有1个房间的部门”,“拥有2个房间的部门数”,依此类推直到5个房间。

到目前为止,我有这个:

    select  E.EDI_NOMBRE_EDIFICIO as "NOMBRE EDIFICIO",
            count(D.NRO_DEPARTAMENTO) as "TOTAL DEPTOS",
            (
             select count(D.NRO_DEPARTAMENTO) as "TOTAL DEPTOS 1 DORMITORIO"
             from (edificio E join departamento D 
             on E.id_edificio = D.ID_EDIFICIO)
             where D.TOTAL_DORMITORIOS = 1
             group by E.EDI_NOMBRE_EDIFICIO
            ) AS "DEPTOS 1 DORM"        
    from (edificio E join departamento D on E.id_edificio = D.ID_EDIFICIO)
    group by E.EDI_NOMBRE_EDIFICIO
    order BY E.EDI_NOMBRE_EDIFICIO;

问题是,我很好地获得了前2列,但是我用来获取具有1个房间的部门总数的子查询却抛出了01427错误,因为(我认为)我正在放置一个SELECT子句中的多行子查询。

如果不使用Select子句中的子查询,我不知道如何处理正确的查询。如果您能帮助我,我将不胜感激。预先感谢。

这是我编程课程的第二个学期,所以我什么都不知道。实际上,我只是阅读了“合并不同count(*)条sql查询的结果”的问题和答案,我听不懂。

3 个答案:

答案 0 :(得分:0)

如何将子查询替换为:

Sum(Case when D.TOTAL_DORMITORIOS = 1 then 1 else 0 end) as "DEPTOS 1 DORM" 

...等等

Sum(Case when D.TOTAL_DORMITORIOS = 2 then 1 else 0 end) as "DEPTOS 2 DORM"
Sum(Case when D.TOTAL_DORMITORIOS = 3 then 1 else 0 end) as "DEPTOS 3 DORM" 

答案 1 :(得分:0)

以这种方式解决:

SELECT  E.EDI_NOMBRE_EDIFICIO as "NOMBRE EDIFICIO",
         (
         SELECT COUNT(D.NRO_DEPARTAMENTO)         
         FROM departamento D 
         WHERE E.id_edificio = D.ID_EDIFICIO
         ) AS "TOTAL DEPTOS",
         (
          SELECT COUNT(D.NRO_DEPARTAMENTO) AS "TOTAL DEPTOS 1 DORMITORIO"
          FROM departamento D
          WHERE D.TOTAL_DORMITORIOS = 1
          AND D.id_edificio = E.id_edificio
         ) AS "DEPTOS 1 DORM",
         (
          SELECT COUNT(D.NRO_DEPARTAMENTO)
          AS "TOTAL DEPTOS 1 DORMITORIO"
          FROM departamento D
          WHERE D.TOTAL_DORMITORIOS = 2
          AND D.id_edificio = E.id_edificio
         ) AS "DEPTOS 2 DORM",
         (
          SELECT COUNT(D.NRO_DEPARTAMENTO)
          AS "TOTAL DEPTOS 1 DORMITORIO"
          FROM departamento D
          WHERE D.TOTAL_DORMITORIOS = 3
          AND D.id_edificio = E.id_edificio
         ) AS "DEPTOS 3 DORM",
         (
          SELECT COUNT(D.NRO_DEPARTAMENTO)
          AS "TOTAL DEPTOS 1 DORMITORIO"
          FROM departamento D
          WHERE D.TOTAL_DORMITORIOS = 4
          AND D.id_edificio = E.id_edificio
         ) AS "DEPTOS 4 DORM",
         (
          SELECT COUNT(D.NRO_DEPARTAMENTO)
          AS "TOTAL DEPTOS 1 DORMITORIO"
          FROM departamento D
          WHERE D.TOTAL_DORMITORIOS = 5
          AND D.id_edificio = E.id_edificio
         ) AS "DEPTOS 5 DORM"
FROM EDIFICIO E
ORDER BY E.EDI_NOMBRE_EDIFICIO ASC;

答案 2 :(得分:0)

如果您首先将departamento表中的结果归为一组,您的答案可能会更准确,因为那样您将只对表查询一次,而不是对每个id_edificio查询6次,就像这样:

SELECT e.edi_nombre_edificio AS "NOMBRE EDIFICIO",
       d.total_deptos AS "TOTAL DEPTOS",
       d.deptos_1_dorm AS "DEPTOS 1 DORM",
       d.deptos_2_dorm AS "DEPTOS 2 DORM",
       d.deptos_3_dorm AS "DEPTOS 3 DORM",
       d.deptos_4_dorm AS "DEPTOS 4 DORM",
       d.deptos_5_dorm AS "DEPTOS 5 DORM"
FROM   edificio e
       INNER JOIN (SELECT id_edificio,
                          COUNT(nro_departamento) total_deptos,
                          COUNT(CASE WHEN total_dormitorios = 1 THEN nro_departamento END) deptos_1_dorm,
                          COUNT(CASE WHEN total_dormitorios = 1 THEN nro_departamento END) deptos_2_dorm,
                          COUNT(CASE WHEN total_dormitorios = 1 THEN nro_departamento END) deptos_3_dorm,
                          COUNT(CASE WHEN total_dormitorios = 1 THEN nro_departamento END) deptos_4_dorm,
                          COUNT(CASE WHEN total_dormitorios = 1 THEN nro_departamento END) deptos_5_dorm
                   FROM   departamento) d ON e.id_edificio = d.id_edificio;