使用子查询选择组函数

时间:2015-10-27 16:33:15

标签: sql oracle group-by

我有以下选择:

SELECT DISTINCT
                   max(tctc_cntipcli) as "TypeOfContract" ,
                   max(texe_cncclipu) as "ContractNumber",
                   max(tctc_cndocidc) as "ClientName",
                   max(tsrv_cndesser) as "ServiceName",
                   max(texe_cnfuncid) as "ServiceNumber",
                   tsrs_cnsubsdc as "SubserviceName",
                   texe_cnsubser as "SubserviceNumber",
                   tmap_cndesc   as "Map",
        (
        SELECT DECODE(to_char(count(tlof_cnlofrid)), '0', 'NA', COUNT(TLOF_CNLOFRID))
        from service.kndtlof
        where tlof_cncclipu = tctc_cncclipu
        and tlof_cnservic = texe_cnfuncid
        and tlof_cnsubser = texe_cnsubser
        and tlof_cnfhalta > trunc (sysdate, 'mm')
       ) as "VolumeOffilesMessages"
                          from service.kndtctc, service.kndtexe, service.kndtscm, service.kndtsrv, service.kndtsrs, service.kndtmap
                          where tctc_cncclipu = texe_cncclipu
                          and texe_cnfuncid = tsrv_cncveser
                          and texe_cnfuncid = tsrs_cncveser
                          and texe_cnsubser = tsrs_cnsubser
                          and texe_cncclipu = tscm_cncontra
                          and tscm_cnmapco = tmap_cnmapco
                          and tscm_cnservic = tsrv_cncveser
                          and tscm_cnsubser = tsrs_cnsubser
                          and tctc_cnestado in ('01', '03')
                          and texe_cnestado in ('01', '03')
                          and tsrv_cnestado in ('01', '03')
                          and tsrs_cnestado in ('01', '03')
                          and tscm_cnestado in ('01', '03')
                          and tmap_cnestado in ('01', '03')
                          group by tsrs_cnsubsdc, texe_cnsubser, tmap_cndesc
                          order by texe_cncclipu;
输出的

    C|50000614|Lindsey|InformationReporting|3050|940PreviousDay|I1|BAISameDayWire|N                                  
C|50000614|Lindsey|WirePayments|3001|WireStatus|S1|MT101SWIFTStatus|NA                                     
C|50000614|Lindsey|WirePayments|3001|WirePayments|W1|Sungard820Payment|NA

我需要在第1,2,3列使用max,并在第4和第5列重复使输出看起来如下:

C|50000614|Lindsey|InformationRep|3050|PreviousDay |I1|BAISameDayWire   |NA
                   WirePayments  |3001|WireStatus  |S1|MT101SWIFTStatus |NA                                     
                                       WirePayments|W1|Sungard820Payment|NA

我在外部查询中的所选列上尝试了最大值并继续获取" ora 0037:不是单组组功能"错误。感谢。

1 个答案:

答案 0 :(得分:0)

MAX是一个分析函数。这意味着它根据一组行计算单个值。请查看this页面以获取有关函数本身的文档,并查看this页面以获取有关分析函数的信息。您将不得不使用GROUP BY语句根据您的规范对数据进行分组,否则您将遇到错误ORA-00937,就像以前一样。