SQL group by cases语句崩溃

时间:2017-07-27 14:27:13

标签: sql db2

我有一个很好的SQL查询,直到我尝试实现group by子句。目前它正在获取我要求的所有行但是他们需要进行其他明智的分组我有太多的行。但是一旦我添加了一个组,即使它只是按一列分组,sql也会完全崩溃。 任何帮助将不胜感激。

SQL的当前错误消息为:

执行数据库查询时出错。

[Macromedia] [SequeLink JDBC驱动程序] [ODBC套接字] [IBM] [iSeries Access ODBC驱动程序] [DB2 UDB] SQL0122 - 列QDSHDT或SELECT列表中的表达式无效。

就像我在没有group by子句时提到的那样,这个错误完全消失了,查询运行正常。

SELECT  Distinct 'SJ' as JOURNAL, 
        BLNDTA.bln525l1.bclnt,
        (substr(digits(dtalib.odmastl1.QDSHDT),1,2) || substr(digits(dtalib.odmastl1.QDSHDT),3,2)) as YEAR,
        substr(digits(dtalib.odmastl1.QDSHDT),5,2) as shpmm,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN QDAMTBL ELSE 0.0 END as qdamtbl,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDAWP + dtalib.odmastl1.QDPFEE + dtalib.odmastl1.QDSHIP3 ELSE 0.0 END) + qdadjamt ELSE 0.0 END as NETSALE,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDCOPAY+dtalib.odmastl1.QDCPAY2 + dtalib.odmastl1.QDDEDCT ELSE 0.0 END as PATRESP,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDAWP + dtalib.odmastl1.QDPFEE + dtalib.odmastl1.QDSHIP3 ELSE 0.0 END) + qdadjamt ELSE 0.0 END) + QDSLTAX - (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDCOPAY+dtalib.odmastl1.QDCPAY2 + dtalib.odmastl1.QDDEDCT ELSE 0.0 END) ELSE 0.0 END as prxtot,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN qdamtpd ELSE 0.0 END as qdamtpd,
       (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDAWP + dtalib.odmastl1.QDPFEE + dtalib.odmastl1.QDSHIP3 ELSE 0.0 END) + qdadjamt ELSE 0.0 END)+qdsltax-(CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDCOPAY+dtalib.odmastl1.QDCPAY2 + dtalib.odmastl1.QDDEDCT ELSE 0.0 END)-qdamtpd as CLTBALDUE,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN qdcost ELSE 0.0 END as qdcost,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDAWP + dtalib.odmastl1.QDPFEE + dtalib.odmastl1.QDSHIP3 ELSE 0.0 END) + qdadjamt - qdcost ELSE 0.0 END as gp,
        CASE WHEN dtalib.odmastl1.qdbatch=bln525l1.bbatch THEN 1 ELSE 0.0 END as OrderCnt,
        CASE WHEN bln525l1.brjrsn <> '   ' THEN 1.0 ELSE 0.0 END as DenialCnt, 
        CASE WHEN bln525l1.brjrsn <> '   ' THEN 1.0 ELSE 0.0 END as Reshipcnt,
        CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch and (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDAWP + dtalib.odmastl1.QDPFEE + dtalib.odmastl1.QDSHIP3 ELSE 0.0 END) + qdadjamt ELSE 0.0 END) + QDSLTAX - (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDCOPAY+dtalib.odmastl1.QDCPAY2 + dtalib.odmastl1.QDDEDCT ELSE 0.0 END) ELSE 0.0 END) <> 0 THEN 1.0 
             WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch and (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDAWP + dtalib.odmastl1.QDPFEE + dtalib.odmastl1.QDSHIP3 ELSE 0.0 END) + qdadjamt ELSE 0.0 END) + QDSLTAX - (CASE WHEN dtalib.odmastl1.qdbatch = bln525l1.bbatch THEN dtalib.odmastl1.QDCOPAY+dtalib.odmastl1.QDCPAY2 + dtalib.odmastl1.QDDEDCT ELSE 0.0 END) ELSE 0.0 END) = 0 THEN 0.0 ELSE NULL END as Shortpd 



FROM        dtalib.odmastl1



LEFT JOIN DTALIB.INV105L1 on (

        dtalib.odmastl1.qdndc5=DTALIB.INV105L1.indc5 
        and dtalib.odmastl1.qdndc4=DTALIB.INV105L1.indc4
        and dtalib.odmastl1.qdndc2=DTALIB.INV105L1.indc2
        and dtalib.odmastl1.qdgener=DTALIB.INV105L1.igener )

LEFT JOIN blndta.gpi205l1 on (

        DTALIB.INV105L1.igpi=gpi205l1.ggpi )

LEFT JOIN blndta.bln525l1 on (

        dtalib.odmastl1.qdordno=bln525l1.brxnum )    

LEFT JOIN dtalib.odmast4l1 on (

        dtalib.odmastl1.qdcocd=dtalib.odmast4l1.q4cocd 
        and dtalib.odmastl1.qdbrcd=dtalib.odmast4l1.q4brcd 
        and dtalib.odmastl1.qddvcd=dtalib.odmast4l1.q4dvcd
        and dtalib.odmastl1.qdtrans=dtalib.odmast4l1.q4trans
        and dtalib.odmastl1.qdline=dtalib.odmast4l1.q4line
        and dtalib.odmastl1.qdprcgrp=dtalib.odmast4l1.q4group
        and dtalib.odmastl1.qdprcclnt=dtalib.odmast4l1.q4clnt 
        and dtalib.odmastl1.qdfill##=dtalib.odmast4l1.Q4FILL## )            

LEFT JOIN dtalib.ohmast on (

        dtalib.odmastl1.qdcocd=dtalib.ohmast.qhcocd 
        and dtalib.odmastl1.qdbrcd=dtalib.ohmast.qhbrcd 
        and dtalib.odmastl1.qddvcd=dtalib.ohmast.qhdvcd 
        and dtalib.odmastl1.qdtrans=dtalib.ohmast.qhtrans ) 


LEFT JOIN blndta.cst115l1 on (

        dtalib.ohmast.qhacct=cst115l1.acct 
        and dtalib.ohmast.qhadnum=cst115l1.adnum )

WHERE       1=1 

        AND dtalib.odmastl1.QDSTATS IN('R3','70') 
        AND SUBSTR(DTALIB.INV105L1.igpi,1,4) NOT IN('9991') 
        AND QDMEDCD NOT IN('CASH','EDUCA','SRVC') 
        AND dtalib.odmastl1.QDSHDT BETWEEN '20170101' and '20991231' 
        AND dtalib.odmastl1.QDRSDT=0

GROUP BY BLNDTA.bln525l1.bclnt, (substr(digits(dtalib.odmastl1.QDSHDT),1,2) || substr(digits(dtalib.odmastl1.QDSHDT),3,2)), substr(digits(dtalib.odmastl1.QDSHDT),5,2) 

1 个答案:

答案 0 :(得分:0)

组中的所有简单选择列都是吗?我看不出来。