SQL,值太多,结果合并

时间:2013-01-28 13:26:11

标签: sql oracle optimization profiling ora-00913

我的SQL查询有问题,事实上当我尝试执行此操作时,我有这样的回复: 我想要做的是计算总和和计数

你能帮帮我吗?非常感谢你

ORA-00913:值太多了 00913. 00000 - “太多的价值观” *原因:
*行动: Erreuràlaligne 2,colonne 5


select distinct 
(select sum(che1.mon), count(che1.typ)
from awbeprod_fr.CHE_BKFRACOMC che1 
where che.ncp = che1.ncp and che.pie=che1.pie and che.typ in ('T','C')),

(select sum(che1.mon) 
from awbeprod_fr.CHE_BKFRACOMC che1 
where che.ncp = che1.ncp and che.typ = che1.typ and che.typ ='F'),

che.ncp, 
m.cpro, m.age,  m.ribdec,  m.dev,
p.lib, cli.cli, cli.pre, cli.nom, 
adcli.adr1, adcli.adr2, adcli.adr3, adcli.cpos, adcli.ville
from 
awbeprod_fr.CHE_BKFRACOMC che, awbeprod_fr.bkcom m, 
awbeprod_fr.bkprod p, awbeprod_fr.bkcli cli, 
awbeprod_fr.bkadcli adcli
where che.ncp = m.ncp 
and che.cli = m.cli and m.cli = cli.cli
and cli.cli = adcli.cli and m.cpro = p.cpro
and che.ncp = '11000003415'
order by che.ncp

2 个答案:

答案 0 :(得分:2)

ANSI JOIN的使用会使您的请求更加清晰,使用GROUP BY可以帮助您“一体化”。

SELECT 
SUM (CASE WHEN che.typ in ('T', 'C') THEN che.mon ELSE 0 END) sumMonTC,
SUM (CASE WHEN che.typ in ('T', 'C') THEN 1       ELSE 0 END) cntMonTC,
SUM (CASE WHEN che.typ ='F'          THEN che.mon ELSE 0 END) sumMonF,
che.ncp, 
m.cpro, m.age,  m.ribdec,  m.dev,
p.lib, cli.cli, cli.pre, cli.nom, 
adcli.adr1, adcli.adr2, adcli.adr3, adcli.cpos, adcli.ville

FROM awbeprod_fr.CHE_BKFRACOMC che
    INNER JOIN awbeprod_fr.bkcom m ON che.ncp = m.ncp AND che.cli = m.cli
    INNER JOIN awbeprod_fr.bkprod p ON m.cpro = p.cpro
    INNER JOIN awbeprod_fr.bkcli cli ON m.cli = cli.cli
    INNER JOIN awbeprod_fr.bkadcli adcli on cli.cli = adcli.cli
WHERE che.ncp = '11000003415'
GROUP BY che.ncp, 
m.cpro, m.age,  m.ribdec,  m.dev,
p.lib, cli.cli, cli.pre, cli.nom, 
adcli.adr1, adcli.adr2, adcli.adr3, adcli.cpos, adcli.ville
ORDER BY che.ncp

答案 1 :(得分:0)

如果使用子查询,则必须返回一个不是两个或更多的值。所以只需将你的第一个子查询分成两个:

select distinct 
(select sum(che1.mon)
from awbeprod_fr.CHE_BKFRACOMC che1 
where che.ncp = che1.ncp and che.pie=che1.pie and che.typ in ('T','C')),

(select count(che1.typ)
from awbeprod_fr.CHE_BKFRACOMC che1 
where che.ncp = che1.ncp and che.pie=che1.pie and che.typ in ('T','C')),

(select sum(che1.mon) 
from awbeprod_fr.CHE_BKFRACOMC che1 
where che.ncp = che1.ncp and che.typ = che1.typ and che.typ ='F'),

che.ncp, 
m.cpro, m.age,  m.ribdec,  m.dev,
p.lib, cli.cli, cli.pre, cli.nom, 
adcli.adr1, adcli.adr2, adcli.adr3, adcli.cpos, adcli.ville
from 
awbeprod_fr.CHE_BKFRACOMC che, awbeprod_fr.bkcom m, 
awbeprod_fr.bkprod p, awbeprod_fr.bkcli cli, 
awbeprod_fr.bkadcli adcli
where che.ncp = m.ncp 
and che.cli = m.cli and m.cli = cli.cli
and cli.cli = adcli.cli and m.cpro = p.cpro
and che.ncp = '11000003415'
order by che.ncp