首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

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

2013-03-26 
大家帮我看下这个SQL语句!!select *,isnull((select sum(ca_money) from cjf_card_account where ca_carda

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


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,
(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 totaldinnerfrom cjf_card a
where c_ctime<=:etime and (1=:allflag or c_flag=:c_flag)
order by c_cardno

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

下面是通过的

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,
(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 totaldinnerfrom cjf_card a
where 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') 这样不就行了?

热点排行