3

我的观点如下 - 它编译得很好,但是当我尝试从中选择时,我得到一个 ORA-00979: not a GROUP BY expression 错误。如果我从视图中消除子查询(第 4 列),一切正常。任何想法将不胜感激!谢谢!!

select
    l.LAB_GROUP,
    l.NAME as LAB,
    b.NAME as BENCH,
    (select count(distinct s2.SAMPLE_NUMBER)
    from SAMPLE s2 inner join TEST t2 on s2.SAMPLE_NUMBER = t2.SAMPLE_NUMBER and t2.STATUS            in ('C', 'R') and s2.TEMPLATE <> 'QC_SAMPLE'
    inner join LABORATORY_ENTRY le2 on t2.ANALYSIS = le2.ANALYSIS
    where s2.LAB_GROUP = l.LAB_GROUP and le2.NAME = l.NAME and t2.X_BENCH = b.NAME and
    ((select count(t1.TEST_NUMBER)
    from TEST t1
    where t1.SAMPLE_NUMBER = t2.SAMPLE_NUMBER and t1.ANALYSIS = t2.ANALYSIS and t1.STATUS   <> 'R') = 0)) as RFR
from LABORATORY l
inner join LABORATORY_ENTRY le on le.NAME = l.NAME 
inner join X_BENCH b on b.NAME = le.X_BENCH
left join (SAMPLE s inner join TEST t on s.SAMPLE_NUMBER = t.SAMPLE_NUMBER and s.STATUS <> 'U'
and s.TEMPLATE <> 'QC_SAMPLE' and t.STATUS in ('I', 'P')) on t.ANALYSIS = le.ANALYSIS and s.LAB_GROUP = l.LAB_GROUP
left join V_LOC_DEPT_FAC ldf on ldf.LOCATION_NUMBER = s.STORAGE_LOC_NO 
group by l.LAB_GROUP, l.NAME, b.NAME 
4

2 回答 2

3

如果您需要使用 group by(如果您添加聚合函数就是这种情况,但不是当前编写的查询),您还需要在其中包含子查询group bySELECT您可以通过在主查询之外添加一个最简单的方法并GROUP BY在该级别应用:

select lab_group, lab, bench, rfr
from 
(
   select
      l.LAB_GROUP as lab_group,
      l.NAME as LAB,
      b.NAME as BENCH,
      (select .....) as RFR
   from LABORATORY l
      inner join LABORATORY_ENTRY le on le.NAME = l.NAME 
      inner join X_BENCH b on b.NAME = le.X_BENCH
      left join (SAMPLE s inner join TEST t on ...) on 
          t.ANALYSIS = le.ANALYSIS and 
          s.LAB_GROUP = l.LAB_GROUP
      left join V_LOC_DEPT_FAC ldf on ldf.LOCATION_NUMBER = s.STORAGE_LOC_NO 
) x
group by lab_group, lab, bench, rfr
于 2013-05-29T14:47:35.540 回答
0

尝试删除...

group by l.LAB_GROUP, l.NAME, b.NAME

因为如果您在子查询中进行计数,我认为这是不必要的。

但是,出于性能原因,我建议重写您的查询,以便将子查询移动到 FROM 部分而不是 SELECT 部分。

于 2013-05-29T14:49:50.430 回答