当前位置: 代码迷 >> Oracle管理 >> 执行子查询出错 not a GROUP BY expression
  详细解决方案

执行子查询出错 not a GROUP BY expression

热度:114   发布时间:2016-04-24 04:12:57.0
执行子查询报错 not a GROUP BY expression
本人想输出四个字段的信息,STAT_TIME,CITY_CODE,AMOUNT,BANK_AMOUNT
现在执行红色部分有问题,总是报错not a GROUP BY expression,注释掉就没问题了,现在想打印输出子查询表中的fee,应该如何修改?谢谢,请指点。

select 
a.STAT_TIME,
b.F_CITY_CODE as CITY_CODE,
sum(a.pay_fee)as AMOUNT,
d.fee as BANK_AMOUNT
from  brpt_bf_n_8035_temp_t a,
om_area_t b,
(
select 
to_char(op_day,'yyyymmdd'), 
city_code, 
sum(t.fee3_crm) fee 
from rpt_user.brpt_reserve_deposit_month_t@bill2crm t ,om_area_t c
where  c.F_CITY_CODE=t.city_code
and c.f_area_level='3'   
and t.op_day >= to_date('2014101','yyyymmdd')
AND t.op_day < to_date('20141002','yyyymmdd')
group by op_day, city_code
) d
where a.city_code=b.f_area_code 
and b.F_CITY_CODE=d.city_code
and b.f_area_level='3'  
and a.STAT_TIME='20141001'
group by STAT_TIME, ( b.F_CITY_CODE) order by ( b.F_CITY_CODE);
------解决思路----------------------
引用:



select a.STAT_TIME,
       b.F_CITY_CODE as CITY_CODE,
       sum(a.pay_fee) as AMOUNT,
       d.fee as BANK_AMOUNT
  from brpt_bf_n_8035_temp_t a,
       om_area_t b,
       (select to_char(op_day, 'yyyymmdd'), city_code, sum(t.fee3_crm) fee
          from rpt_user.brpt_reserve_deposit_month_t@bill2crm t, om_area_t c
         where c.F_CITY_CODE = t.city_code
           and c.f_area_level = '3'
           and t.op_day >= to_date('2014101', 'yyyymmdd')
           AND t.op_day < to_date('20141002', 'yyyymmdd')
         group by op_day, city_code) d
 where a.city_code = b.f_area_code
   and b.F_CITY_CODE = d.city_code
   and b.f_area_level = '3'
   and a.STAT_TIME = '20141001'
 group by STAT_TIME, b.F_CITY_CODE, d.fee
 order by b.F_CITY_CODE;

------解决思路----------------------
如果查询语句中存在group子句
则select子句中只能是group by 子句中的字段,或是能通过这些字段生成的公式,或是组函数,或是常数
d.fee as BANK_AMOUNT在group子句中没有,因此在这里写会报错可以改为max(d.fee) as BANK_AMOUNT
------解决思路----------------------
d.fee 不在组合 group by STAT_TIME, ( b.F_CITY_CODE)  范围内的,系统无法给出查询结果~ 
------解决思路----------------------
以下两句的字段列表要一致,除非使用了 max() , min() , avg() , count() 等函数。

a.STAT_TIME,
b.F_CITY_CODE as CITY_CODE,
sum(a.pay_fee)as AMOUNT,
d.fee as BANK_AMOUNT




group by STAT_TIME, ( b.F_CITY_CODE) 


------解决思路----------------------
没有用聚合函数的列,应该出现在GROUP BY 子句中
------解决思路----------------------
还有一种可能是,它输出的字段中只有日期和城市是个key,paid fee 显示个收到的总和,而 fee 却显示明细,用 OLAP Window 函数能做到,从它的表结构猜想  a 和 b 应该是个静态数据(例如外出出报表的机构层次范围),不会有重复记录:


select a.STAT_TIME,
       b.F_CITY_CODE as CITY_CODE,
       sum(a.pay_fee) over (partition by  STAT_TIME, b.F_CITY_CODE) as AMOUNT,
       d.fee as BANK_AMOUNT
  from brpt_bf_n_8035_temp_t a,
       om_area_t b,
       (select to_char(op_day, 'yyyymmdd'), city_code, sum(t.fee3_crm) fee
          from rpt_user.brpt_reserve_deposit_month_t@bill2crm t, om_area_t c
         where c.F_CITY_CODE = t.city_code
           and c.f_area_level = '3'
           and t.op_day >= to_date('2014101', 'yyyymmdd')
           AND t.op_day < to_date('20141002', 'yyyymmdd')
         group by op_day, city_code) d
 where a.city_code = b.f_area_code
   and b.F_CITY_CODE = d.city_code
   and b.f_area_level = '3'
   and a.STAT_TIME = '20141001'
  相关解决方案