查询不按我想要的分组

时间:2014-05-19 17:19:28

标签: sql oracle

我在查询时遇到了一些问题:

select 
case 
    when d.deal_numadm!=0 and d.deal_numadm is not null 
        then 'MA' || d.deal_nummas || '_' || d.ntt_id 
    when d.deal_numadm=0 or d.deal_numadm is null 
        then 'Master' || d.deal_nummas || '_' || d.ntt_id 
end as node_id, 
sum(cva.cva), sum(dva.dva), d.deal_numadm, d.deal_nummas
from acc_processed_cva cva 
left outer join acc_processed_dva dva on dva.deal_id = cva.deal_id 
join acc_deals d on d.deal_id = cva.deal_id and d.deal_scope='Y' 
join dt_runs r on r.run_id = cva.run_id and r.deal_cnt_id = d.deal_cnt_id 
where r.run_id=57 and d.deal_nummas=10700 and d.ntt_id = 1731413
group by d.ntt_id, d.deal_nummas, d.deal_numadm, d.deal_cnt_id, d.ntt_id;

返回以下行:

NODE_ID             SUM(CVA.CVA)    SUM(DVA.DVA)    DEAL_NUMADM DEAL_NUMMAS
MA10700_1731413     953180          1373440         22012       10700
Master10700_1731413 4886330         4318999         0           10700
MA10700_1731413     1654            1192            12054       10700
MA10700_1731413     4               (null)          83442       10700
MA10700_1731413     14              1093            10775       10700
MA10700_1731413     963             (null)          10779       10700

但是,我想按DEAL_NUMMAS对这些行进行分组,所以我应该只有2行。

我已尝试删除d.deal_numadm表达式中的GROUP BY,但失败并出现此错误:

ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression"

澄清

上面,DEAL_NUMADM仅用于显示返回的每行不同。

我想要的是:

NODE_ID             SUM(CVA.CVA)    SUM(DVA.DVA)    DEAL_NUMMAS
MA10700_1731413     955815          1375725         10700
Master10700_1731413 4886330         4318999         10700

第一行是行1,3,4,5和6的总和。

如何达到我想要的效果?

1 个答案:

答案 0 :(得分:0)

好的,我明白了。执行我想要的查询:

select
case 
    when d.deal_numadm!=0 and d.deal_numadm is not null 
        then 'MA' || d.deal_nummas || '_' || d.ntt_id 
    when d.deal_numadm=0 or d.deal_numadm is null 
        then 'Master' || d.deal_nummas || '_' || d.ntt_id 
end as node_id, 
sum(cva.cva), sum(dva.dva)
from acc_processed_cva cva 
left outer join acc_processed_dva dva on dva.deal_id = cva.deal_id 
join acc_deals d on d.deal_id = cva.deal_id and d.deal_scope='Y' 
join dt_runs r on r.run_id = cva.run_id and r.deal_cnt_id = d.deal_cnt_id 
where r.run_id=57 and d.deal_nummas=10700 and d.ntt_id = 1731413
group by d.ntt_id, d.deal_nummas, case 
    when d.deal_numadm!=0 and d.deal_numadm is not null 
        then 'MA' || d.deal_nummas || '_' || d.ntt_id 
    when d.deal_numadm=0 or d.deal_numadm is null 
        then 'Master' || d.deal_nummas || '_' || d.ntt_id 
end;