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

union 语句, 急
select day(ghrq),count(djh) as sl,max('初诊')as xm from gh_sfdj where ctbz='0' and cf='0' group by day(ghrq)
union select day(rq),count(id) as sl,max('咨询') as xm from t_czdhlyb where bz='0' group by day(rq)
union select day(rq),count(id) as sl,max('留诊')as xm from t_czdhlyb where bz='0' and sflz='0' group by day(rq)


这个语句中 我想加个 留诊数量合计/咨询数量合计的 列,如何加,谢谢,也就是加个比率列

------解决方案--------------------
SQL code
select day(ghrq),count(djh) as sl,max('初诊')as xm from gh_sfdj where ctbz='0' and cf='0' group by day(ghrq) 
union select day(rq),count(id) as sl,max('咨询') as xm from t_czdhlyb where bz='0' group by day(rq) 
union select day(rq),count(id) as sl,max('留诊')as xm from t_czdhlyb where bz='0' and sflz='0' group by day(rq) 
union select day(rq),sum(case sflz='0' then 1 else 0 end)*100/count(id) as sl,max('比例')as xm 
from t_czdhlyb where bz='0' group by day(rq)

------解决方案--------------------
SQL code
select day(ghrq),count(djh) as sl,max('初诊')as xm, 0 as col
from gh_sfdj 
where ctbz='0' and cf='0' 
group by day(ghrq) 
union 
select day(rq),count(id) as sl,max('咨询') as xm ,count(1) * 100/(select count(id) from t_czdhlyb where rq = a.rq and bz='0' and sflz='0')
from t_czdhlyb a
where bz='0' 
group by day(rq) 
union 
select day(rq),count(id) as sl,max('留诊')as xm ,count(1) * 100/(select count(id) from t_czdhlyb where rq = a.rq and bz='0' )
from t_czdhlyb 
where bz='0' and sflz='0' 
group by day(rq)