以下语句为何报错?RA-00979: not a GROUP BY expression
以下语句正常得到结果:
SELECT a.sljg,b.jgmc,substr(a.tdj,1,4) ,C.XSJMC ,count(1)
FROM TYJMX a,TJGB b,TXSJMCYB c
WHERE a.sljg=b.jgbh and C.YB=substr(a.tdj,1,4)
and a.sljg='31003800' and substr(a.tdj,1,4)<>'3100'
and a.yjzt<>'Z' and substr(a.pc,1,2)<>'CS'
group by a.sljg,b.jgmc,substr(a.tdj,1,4) ,C.XSJMC
;
但是: SELECT a.sljg,b.jgmc,substr(a.tdj,1,4) ,C.XSJMC ,count(1) ,
(SELECT COUNT(*)
FROM tyjmx d
WHERE d.sljg = a.sljg and d.tdj=a.TDJ
and d.sljg='31003800' and substr(d.tdj,1,4)<>'3100'
AND d.yjzl = 'B类邮件' )
FROM TYJMX a,TJGB b,TXSJMCYB c
WHERE a.sljg=b.jgbh and C.YB=substr(a.tdj,1,4)
and a.sljg='31003800' and substr(a.tdj,1,4)<>'3100'
and a.yjzt<>'Z' and substr(a.pc,1,2)<>'CS'
group by a.sljg,b.jgmc,substr(a.tdj,1,4) ,C.XSJMC
;
却报错:RA-00979: not a GROUP BY expression
其中d.yjzl = 'B类邮件' ) 是邮件种类中的一部分
oracle10g是支持这种写法的,求助
[解决办法]
(SELECT COUNT(*)
FROM tyjmx d
WHERE d.sljg = a.sljg and d.tdj=a.TDJ
and d.sljg='31003800' and substr(d.tdj,1,4)<>'3100'
AND d.yjzl = 'B类邮件' )
这个弄个列别名
然后写到group by字句里面
[解决办法]
SELECT a.sljg,b.jgmc,substr(a.tdj,1,4) ,C.XSJMC ,count(1) ,
(SELECT COUNT(*)
FROM tyjmx d
WHERE d.sljg = a.sljg and d.tdj=a.TDJ
and d.sljg='31003800' and substr(d.tdj,1,4)<>'3100'
AND d.yjzl = 'B类邮件'
group by a.sljg,b.jgmc,substr(a.tdj,1,4) ,C.XSJMC)
FROM TYJMX a,TJGB b,TXSJMCYB c
WHERE a.sljg=b.jgbh and C.YB=substr(a.tdj,1,4)
and a.sljg='31003800' and substr(a.tdj,1,4)<>'3100'
and a.yjzt<>'Z' and substr(a.pc,1,2)<>'CS'
group by a.sljg,b.jgmc,substr(a.tdj,1,4) ,C.XSJMC
加了红字部分,你试试。
[解决办法]
group by 语句的基本原则:
select 部分所选的字段,应该是 group by 引用的字段,如果不是,则字段名称必须用于 sum(),count()等函数的参数,否则会报 ora-00979;道理很简单,仔细想想就理解了。