为啥子查询会在 group by 查询中抛出错误?

Posted

技术标签:

【中文标题】为啥子查询会在 group by 查询中抛出错误?【英文标题】:Why does the subquery throw errors in a group by query?为什么子查询会在 group by 查询中抛出错误? 【发布时间】:2018-08-23 08:41:01 【问题描述】:

好的,我会尝试第二次,因为第一次没有那么聪明

我的查询:

SELECT distinct  kg.datum,
   sum(case when leis.code = 'Oph3001' then leis.anzahl END) as leis3001,
   sum(case when leis.code = 'Oph3003' then leis.anzahl END) as leis3003,
   (select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual) as real_datum 
  FROM kg_eintraege kg
   INNER JOIN aufenthalte a
   ON kg.patient_nr = a.patient_nr
   and kg.fall_nr = a.fall_nr
   INNER JOIN MF_LEIS_DIAG_OP_MD leis 
   on leis.aufenthalte_nr = a.nr
   group by kg.datum, kg.kg_id
    order by /*real_datum*/ kg.datum desc

以下子查询导致问题:

(select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual)

将其放入 group by 表达式会引发 ORA-22818 错误(“此处不允许子查询”)。

不放会引发 ORA-00979 错误(“不是按表达式分组”)。

有人可以帮助我吗?

【问题讨论】:

您是否尝试过使用外连接到kg_eintraege 的第二个实例而不是子查询? (同样,不相关,但nvl(<somthing>,'') 毫无意义——在 Oracle 中 null 和空字符串是相同的,所以结果仍然是 null。) @Alex Poole 谢谢你的回答。几个月前我尝试了外部连接,但不知何故我把它搞砸了。我会再试一次。我不知道,为什么我把nvl放在那里,我也会看看。编辑:对不起,我认为这是一个内部连接,我试过了。 【参考方案1】:

您可以改用外连接;类似:

SELECT distinct  kg.datum,
   sum(case when leis.code = 'Oph3001' then leis.anzahl END) as leis3001,
   sum(case when leis.code = 'Oph3003' then leis.anzahl END) as leis3003,
   to_date(KG2.KURZTEXT, 'dd.mm.yyyy') as real_datum 
FROM kg_eintraege kg
INNER JOIN aufenthalte a
ON kg.patient_nr = a.patient_nr
and kg.fall_nr = a.fall_nr
INNER JOIN MF_LEIS_DIAG_OP_MD leis 
on leis.aufenthalte_nr = a.nr
LEFT OUTER JOIN kg_eintraege kg2 
on kg2.kgtitel_nr = 1003350007
and kg.fall_nr = kg2.fall_nr
and kg.patient_nr = kg2.patient_nr
and kg.kg_id = kg2.kontext
group by kg.datum, kg.kg_id, to_date(KG2.KURZTEXT, 'dd.mm.yyyy')
order by /*real_datum*/ kg.datum desc

【讨论】:

【参考方案2】:

你可以使用子查询

select datum,
       sum(case when code = 'Oph3001' then anzahl END) as leis3001,
       sum(case when code = 'Oph3003' then anzahl END) as leis3003,
       real_datum
from (       
      SELECT distinct  kg.datum,
             leis.code,
             leis.anzahl,       
            (select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual) as real_datum 
        FROM kg_eintraege kg
        INNER JOIN aufenthalte a ON kg.patient_nr = a.patient_nr
                                and kg.fall_nr = a.fall_nr
        INNER JOIN MF_LEIS_DIAG_OP_MD leis on leis.aufenthalte_nr = a.nr
      )
group by datum, kg_id, real_datum
order by /*real_datum*/ kg.datum desc

【讨论】:

以上是关于为啥子查询会在 group by 查询中抛出错误?的主要内容,如果未能解决你的问题,请参考以下文章

为啥子查询解决方法中的这个 ORDER BY 不能始终如一地工作?

SwiftUI 组嵌套在 VStack 中抛出错误

当查询具有 GROUP BY 子句时,Hive 查询抛出“来自 org.apache.hadoop.hive.ql.exec.tez.TezTask 的代码 2”异常

Laravel GroupBy 在 mysql 中工作,但在 MariaDB 中抛出错误

为啥 requests-mock 装饰器模式会在 pytest 中抛出“fixture 'm' not found”错误?

BigQuery GROUP BY/GROUP EACH BY 资源超出错误,但查询不包含 GROUP BY 运算符