querysql
.append("select o.orgid,date_format(o.orderdate,'%Y'),count(*),sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)" +
"as totalmoney,sum(case when o.payamount is not NULL then o.payamount else '0.00' end) as paymoney," +
" (sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)-sum(case when o.payamount is not NULL and (case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)!='0.00' then o.payamount else '0.00' end)) " +
"as nopaymoney  from htglview o where 1=1 and o.contractState in (5,8,9,10,11) ");

解决方案 »

  1.   

    querysql
    .append("select o.orgid,date_format(o.orderdate,'%Y'),count(*),sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)" +
    "as totalmoney,sum(case when o.payamount is not NULL then o.payamount else '0.00' end) as paymoney," +
    " (sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)-sum(case when o.payamount is not NULL and (case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)!='0.00' then o.payamount else '0.00' end)) " +
    "as nopaymoney  from htglview o where 1=1 and o.contractState in (5,8,9,10,11) ");
      

  2.   

    sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)
    判断 o.finalamount!='0.00'  成立 则累加 o.finalamount 字段  否则 累加o.contractamount 
    其他的类似 
      

  3.   

    晕啊,就一个SQL语句,这样看不明白,就把它格式化出来,发这么长,一看乱七八糟啊
      

  4.   

    select o.orgid,查询 orgid   
    date_format(o.orderdate,'%Y'), 输出 orderdate 年的数字
    count(*) 统计结果集
    sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)" +
    "as totalmoney 已经解释过累加规则了 以别名 totalmoney  输出结果
    sum(case when o.payamount is not NULL then o.payamount else '0.00' end) as paymoney同理
    sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)-sum(case when o.payamount is not NULL and (case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)!='0.00' then o.payamount else '0.00' end)) " +
    "as nopaymoney
    nopaymoney 是把 里面2个SUM结果集想减   case when  then  这个就不解释 同理
     from htglview o where 1=1 and o.contractState in (5,8,9,10,11) 这个我就不用解释了吧
      

  5.   

    你这能debug吧,断点设在这句下面,看看querysql的值,就是完整的SQL语句了
    复制到PLSQL里,格式化的功能。