“来自关键字未找到”,在子查询中使用distinct

时间:2016-12-28 14:58:02

标签: sql oracle

基本上我想要做的是插入一个名为tempRecargaBIFINAL的表NUM_ORIGEN,以及大小写的总和。

当我执行只有NUM_ORIGEN运行完美时,但是当我添加SUM时,它会显示标题中列出的错误。现在显示错误ORA-00937:不是单组组功能

Insert into SAC_IND.tempRecargaBIFINAL(NUM_ORIGEN,REC_A)
WITH
data AS
    (
    SELECT DISTINCT( NUM_ORIGEN ),
       sum(
           case
               when IDPERIODO = (2016010-5)
               then CTA_PRINCIPAL_DELTA else 0 
           end) rec_a x
    from FT_RECARGA_BI
    Where IDPERIODO BETWEEN 201604 and 2016010
    )

Select x from data
Group By x;

按照建议我现在更改查询

Insert into SAC_IND.tempRecargaBIFINAL(NUM_ORIGEN,REC_A)
WITH
data AS
(
SELECT DISTINCT( NUM_ORIGEN )x,
sum(case when IDPERIODO = (2016010-5) then CTA_PRINCIPAL_DELTA else 0 end)rec_a from FT_RECARGA_BI
Where IDPERIODO BETWEEN 201604 and 2016010
)
Select x,rec_a from data
Group By x,rec_a;

最后查询工作,我几乎哭了但是伯爵给了0,但是嘿!最后脚本工作:D

Insert into SAC_IND.tempRecargaBIFINAL(NUM_ORIGEN,REC_A)
WITH
data AS
(
SELECT DISTINCT( NUM_ORIGEN ) x,
sum(case when IDPERIODO = (2016010-5) then CTA_PRINCIPAL_DELTA else 0  end)rec_a from FT_RECARGA_BI
Where IDPERIODO BETWEEN 201604 and 2016010
Group by IDPERIODO,NUM_ORIGEN
)
Select x,rec_a from data
Group By x,rec_a;

1 个答案:

答案 0 :(得分:0)

当你在聚合函数和中使用它时,你已经将IDPERIODO放在group by子句中。那么它会为每个IDPERIODO和NUM_ORIGEN总结CTA_PRINCIPAL_DELTA会发生什么。

所以查询可以有两种方式

1 ..

Insert into SAC_IND.tempRecargaBIFINAL(NUM_ORIGEN,REC_A)
WITH
data AS
(
SELECT distinct  NUM_ORIGEN  x, IDPERIODO ,
sum (case when IDPERIODO = (2016010-5) then CTA_PRINCIPAL_DELTA else 0  end) rec_a from FT_RECARGA_BI
Where IDPERIODO BETWEEN 201604 and 2016010
Group by IDPERIODO ,NUM_ORIGEN
)
Select x,rec_a from data
Group By x,rec_a;

或 2.

Insert into SAC_IND.tempRecargaBIFINAL(NUM_ORIGEN,REC_A)
WITH
data AS
(
SELECT distinct NUM_ORIGEN x, 
sum (case when IDPERIODO = (2016010-5) then CTA_PRINCIPAL_DELTA else 0  end) rec_a from FT_RECARGA_BI
Where IDPERIODO BETWEEN 201604 and 2016010
Group by NUM_ORIGEN
)
Select x,rec_a from data
Group By x,rec_a;