日期:2014-05-18  浏览次数:20667 次

合并SQL语句
(1)select co_code,sum(case when receive_date between <%param("qsrq")%> and <%param("jsrq")%> then revenue_num else 0 end)*100 benyue,sum(revenue_num)*100 leijishu 
from v_nt_bill_baobiaoqushu
where fiscal=<%substring(param('jsrq'),1,4) %>
and receive_date<=<%param("jsrq")%>
and co_code in (<%getcode(param('gsdm'))%>)
group by co_code
order by co_code

(2)select co_code,sum(case when receive_date between <%param("sqsrq")%> and <%param("sjsrq")%> then revenue_num else 0 end)*100 sbenyue,sum(revenue_num)*100 sleijishu
from v_nt_bill_baobiaoqushu
where fiscal=<%substring(param('sjsrq'),1,4) %>
and receive_date<=<%param("sjsrq")%>
and co_code in (<%getcode(param('gsdm'))%>)
group by co_code
order by co_code

请教如何将两个语句合并在一个语句里面,谢谢,指导

------解决方案--------------------
select co_code,sum(case when receive_date between <%param("qsrq")%> and <%param("jsrq")%> then revenue_num else 0 end)*100 benyue,sum(revenue_num)*100 leijishu
from v_nt_bill_baobiaoqushu
where fiscal=<%substring(param('jsrq'),1,4) %>
and receive_date<=<%param("jsrq")%>
and co_code in (<%getcode(param('gsdm'))%>)
group by co_code
union all
select co_code,sum(case when receive_date between <%param("sqsrq")%> and <%param("sjsrq")%> then revenue_num else 0 end)*100 sbenyue,sum(revenue_num)*100 sleijishu
from v_nt_bill_baobiaoqushu
where fiscal=<%substring(param('sjsrq'),1,4) %>
and receive_date<=<%param("sjsrq")%>
and co_code in (<%getcode(param('gsdm'))%>)
group by co_code

------解决方案--------------------
SQL code

select co_code,sum(case when receive_date between <%param("qsrq")%> and <%param("jsrq")%> then revenue_num else 0 end)*100 benyue,sum(revenue_num)*100 leijishu  
from v_nt_bill_baobiaoqushu
where fiscal=<%substring(param('jsrq'),1,4) %>
and receive_date<=<%param("jsrq")%>
and co_code in (<%getcode(param('gsdm'))%>)
group by co_code
order by co_code
union all  --用union all
select co_code,sum(case when receive_date between <%param("sqsrq")%> and <%param("sjsrq")%> then revenue_num else 0 end)*100 sbenyue,sum(revenue_num)*100 sleijishu
from v_nt_bill_baobiaoqushu
where fiscal=<%substring(param('sjsrq'),1,4) %>
and receive_date<=<%param("sjsrq")%>
and co_code in (<%getcode(param('gsdm'))%>)
group by co_code
order by co_code

------解决方案--------------------
select ...,
 sum(case when receive_date<=date1 and 原条件 then ... else 0 end)*100 benyue,
 sum(case when receive_date<=date2 and 原条件 then ... else 0 end)*100 sbenyue,
 sum(case when receive_date<=date1 then receive_num else 0 end) leijishu,
 sum(case when receive_date<=date2 then receive_num else 0 end) sleijishu
from ...
where ...

这里的where条件中没有receive_date<=条件。
------解决方案--------------------
SQL code
select co_code,sum(case when receive_date between <%param("qsrq")%> and <%param("jsrq")%> then revenue_num else 0 end)*100 benyue,sum(revenue_num)*100 leijishu   
from v_nt_bill_baobiaoqushu
where fiscal=<%substring(param('jsrq'),1,4) %>
and receive_date<=<%param("jsrq")%>
and co_code in (<%getcode(param('gsdm'))%>)
group by co_code
union all
select co_code,sum(case when receive_date between <%param("sqsrq")%> and <%param(&quo