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

union 语句, 急该如何处理

2012-03-21 
union 语句, 急select day(ghrq),count(djh) as sl,max(初诊)as xm from gh_sfdj where ctbz0 and cf

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) 

热点排行