暂无图片
奇怪的 ORA-00979: 不是Group BY表达式问题
我来答
分享
暂无图片 匿名用户
奇怪的 ORA-00979: 不是Group BY表达式问题
暂无图片 10M

今天碰到个奇怪的 ORA-00979: 不是Group BY表达式问题, 主要代码如下:


insert into r_ind_crcard_inf
(data_dt,
  inst_id,
  cancl_code,
  th_mnth_iss_ind,
  crdt_card_cnt,
  crdt_acct_cnt)
select 
data_dt,
nvl(t1.open_inst_id,'@') as inst_id,
nvl(t1.cancl_code,'@') as cancl_code,
decode(to_char(t1.issue_day,'yyyymm'),substr(i_etl_date,1,6),'1','0') as th_mnth_iss_ind,
count(*) as crdt_card_cnt,
count(distinct t1.acct_no) as crdt_acct_cnt
from g_ind_crcard_inf t1
where t1.data_dt=v_etl_date
group by 
nvl(t1.open_inst_id,'@'),
nvl(t1.cancl_code,'@'),
decode(to_char(t1.issue_day,'yyyymm'),substr(i_etl_date,1,6),'1','0');
复制

这段代码直接在plsql develop中执行,把日期变量改成具体日期是可以正常运行的, 但是放进存储过程中,去跑存储过程就是报ORA-00979: 不是Group BY表达式问题,  但如果把  count(distinct t1.acct_no) as crdt_acct_cnt中的distinct 去掉,在存储过程中是可以运行的,为啥加了个distinct就会报错呢 ?  更理解不了的是,  即使不去掉distinct, 放在plsql develop中直接执行sql语句也不会报错,  真是百思不得其解 ?

我来答
添加附件
收藏
分享
问题补充
3条回答
默认
最新
cqiwen

Apply Patch 18749211, if available for your version and platform.

暂无图片 评论
暂无图片 有用 1
打赏 1
wclluck
2022-02-08
之前我百度过,像这个贴子 https://blog.csdn.net/tpc4289/article/details/112603669, 所以我也猜测是版本问题,我看了下我的oracle版本是11.2.0.4, 我想不通的是,为何直接运行sql语句是可以的,而把sql语句放进存储过程,去跑存储过程就会报错 ?
wclluck
2022-02-08
看了很多贴子,有点明白可能的原因,因为 i_etl_date 是跑存储过程时传进来的变量, 放到group by后面时, 聚合函数不能带distinct, 直接sql运行正常是因为把i_etl_date改成了具体的值,明天在公司电脑再验证下
Thomas

你说的日期变量是DATE_DT吗?

在存储过程里跑,DATE_DT出现在SELECT里,是指表T1的具体字段。那么,在GROUP BY 里也应该出现DATE_DT,所以得将group by改为:group by date_dt, nvl(t1.open_inst_id,'@'), nvl(t1.cancl_code,'@'), decode(to_char(t1.issue_day,'yyyymm'),substr(i_etl_date,1,6),'1','0');    

应该就没错了。尽管你认为加了WHERE条件data_dt=v_etl_date,已可以确保输出的DATE_DT只有一个值,但计算机判断问题没那么灵活,凡是在SELECT里出现的单个字段(不是指那种用聚合函数包括的字段,比如count(distinct acct_no),必须也出现在GROUP BY里。


暂无图片 评论
暂无图片 有用 0
打赏 1
wclluck
2022-02-08
谢谢回复, 我试过了不是这个问题, count(diistinct acct_no)中把distinct去掉后就能正常运行,所以我觉得与group by后面的字段没关系
DarkAthena

把select的第一个字段改一下,用那个变量。

insert into r_ind_crcard_inf (data_dt, inst_id, cancl_code, th_mnth_iss_ind, crdt_card_cnt, crdt_acct_cnt) select v_etl_date, nvl(t1.open_inst_id,'@') as inst_id, nvl(t1.cancl_code,'@') as cancl_code, decode(to_char(t1.issue_day,'yyyymm'),substr(i_etl_date,1,6),'1','0') as th_mnth_iss_ind, count(*) as crdt_card_cnt, count(distinct t1.acct_no) as crdt_acct_cnt from g_ind_crcard_inf t1 where t1.data_dt=v_etl_date group by nvl(t1.open_inst_id,'@'), nvl(t1.cancl_code,'@'), decode(to_char(t1.issue_day,'yyyymm'),substr(i_etl_date,1,6),'1','0');
复制

在sql聚合语法中,对于没有聚合的字段,全部要放到group by后面去,除了变量或常量

暂无图片 评论
暂无图片 有用 0
打赏 1
wclluck
2022-02-08
谢谢回复, 我试过了不是这个问题, count(diistinct acct_no)中把distinct去掉后就能正常运行,所以我觉得与group by后面的字段没关系
回答交流
Markdown


请输入正文
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
暂无图片墨值悬赏