ORA-00979:使用子查询查询视图时不是GROUP BY表达式

时间:2013-05-29 14:43:08

标签: oracle

我有一个视图如下 - 它编译得很好但是当我尝试从中选择时,我得到一个ORA-00979:不是GROUP BY表达式错误。如果我从视图中删除子查询(第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 

2 个答案:

答案 0 :(得分:1)

如果您需要使用group by(如果添加聚合函数,则会出现这种情况,但当前编写的查询不是这样),您还需要在group by中包含子查询。您可以通过在主要查询之外添加SELECT并在该级别对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

答案 1 :(得分:0)

尝试删除...

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

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

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