读书人

大家帮小弟我看上这个SQL语句!

发布时间: 2012-10-24 14:15:58 作者: rapoo

大家帮我看下这个SQL语句!!

SQL code
select *,isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=1 and ca_ctime<=:etime) ,0) as totals,isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=0 and ca_ctime<=:etime) ,0) as totalc,(isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=1 and ca_ctime<=:etime),0)-isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=0 and ca_ctime<=:etime),0)) as totalb,isnull((select sum(sp_money) from cjf_salecardpay where sp_caccno=a.c_accno and sp_ctime<=:etime),0) as totalp,isnull((select sum(cp_money) from cjf_card_consumepay where cp_cardacc=a.c_accno and cp_ctime<=:etime),0) as totalu,(isnull((select sum(jf_money) from cjf_card_jifen where jf_cardacc=a.c_accno and jf_type=1 and jf_ctime<=:etime),0)  - isnull((select sum(jf_money) from cjf_card_jifen where jf_cardacc=a.c_accno and jf_type=0 and jf_ctime<=:etime),0) ) as totalbjf,[color=#FF0000](isnull((select sum(cq_breakfast) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='04' and cq_ctime<=:etime),0) -isnull((select sum(cq_breakfast) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='01' and cq_ctime<=:etime),0) - isnull((select sum(cq_breakfast) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='05' and cq_ctime<=:etime),0))  as totalbreakfast,(isnull((select sum(cq_lunch) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='04' and cq_ctime<=:etime),0) -isnull((select sum(cq_lunch) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='02' and cq_ctime<=:etime),0) -isnull((select sum(cq_lunch) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='05' and cq_ctime<=:etime),0)) as totallunch,(isnull((select sum(cq_dinner) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='04' and cq_ctime<=:etime),0) -isnull((select sum(cq_dinner) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='03' and cq_ctime<=:etime),0) -isnull((select sum(cq_dinner) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='05' and cq_ctime<=:etime),0))  as totaldinner[/color]from cjf_card awhere c_ctime<=:etime and (1=:allflag or c_flag=:c_flag)order by c_cardno

放在Query组件里的,D5,通不过!主要是用了连续的减法,要怎么改?

下面是通过的
SQL code
select *,isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=1 and ca_ctime<=:etime) ,0) as totals,isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=0 and ca_ctime<=:etime) ,0) as totalc,(isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=1 and ca_ctime<=:etime),0)-isnull((select sum(ca_money) from cjf_card_account where ca_cardacc=a.c_accno  and  ca_type=0 and ca_ctime<=:etime),0)) as totalb,isnull((select sum(sp_money) from cjf_salecardpay where sp_caccno=a.c_accno and sp_ctime<=:etime),0) as totalp,isnull((select sum(cp_money) from cjf_card_consumepay where cp_cardacc=a.c_accno and cp_ctime<=:etime),0) as totalu,(isnull((select sum(jf_money) from cjf_card_jifen where jf_cardacc=a.c_accno and jf_type=1 and jf_ctime<=:etime),0)  - isnull((select sum(jf_money) from cjf_card_jifen where jf_cardacc=a.c_accno and jf_type=0 and jf_ctime<=:etime),0) ) as totalbjf,[color=#FF0000](isnull((select sum(cq_breakfast) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='04' and cq_ctime<=:etime),0) -isnull((select sum(cq_breakfast) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='01' and cq_ctime<=:etime),0) as totalbreakfast,(isnull((select sum(cq_lunch) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='04' and cq_ctime<=:etime),0) -isnull((select sum(cq_lunch) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='02' and cq_ctime<=:etime),0)as totallunch,(isnull((select sum(cq_dinner) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='04' and cq_ctime<=:etime),0) -isnull((select sum(cq_dinner) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='03' and cq_ctime<=:etime),0))as totaldinner[/color]from cjf_card awhere c_ctime<=:etime and (1=:allflag or c_flag=:c_flag)order by c_cardno 



[解决办法]
对比通过的那个
只比上面少了
“isnull((select sum(cq_lunch) from cjf_card_dzcq where cq_cardno=a.c_incode and cq_type='05' and cq_ctime<=:etime),0)”

为什么非要减去两次啊? 分组汇总的条件都一样的
把 cq_type='01'改成
cq_type in ('01','05') 这样不就行了?

读书人网 >.NET

热点推荐