当前位置: 代码迷 >> Sql Server >> union 语句, 急该如何处理
  详细解决方案

union 语句, 急该如何处理

热度:57   发布时间:2016-04-27 18:42:48.0
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 colfrom 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 awhere 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)