1 left join O_CANCELORDER d on a.orderid=d.orderid and d.paystate<>3 where 1=1 @b ) s group by date_format(s.transtime,'%Y-%m-%d') ) r ]]> 0 then orderstatus when orderstatus=0 and transSum<>price then 0 else 1 end as dealState from ( select b.OPERATORNAME as channel,a.orderno,a.orderid,b.cardno,a.transactions, b.price*0.01 as price, (select sum(q.transactions) from P_BILL q where q.orderid=a.orderid and q.ordertype=a.ordertype group by q.orderid,q.ordertype) as transSum, a.ordertype, case when a.ordertype=1 and c.paystate is not null then '1' when a.ordertype=2 and d.paystate is not null then '2' else '' end payFlag, case when a.ordertype=1 then to_char(c.paystate) when a.ordertype=2 then to_char(d.paystate) else '0' end state, to_char(a.transtime,'yyyy-mm-dd hh:mm:ss') transTime,to_char(b.begin,'yyyy-mm-dd hh:mm:ss') beginDate,a.orderstatus, c.paystate,d.paystate from P_BILL a left join O_ORDER b on a.orderid=b.orderid left join O_PAYORDER c on a.orderid=c.orderid left join O_CANCELORDER d on a.orderid=d.orderid where 1=1 @p order by a.orderid,a.ordertype ) tt where 1=1 @d ]]>