0
下面的查询是一种丑陋的一种,所以我希望我已经把它的间隔足够好,以使其可读。该查询查找来自某个特定医院的人员的百分比。例如,如果100个人住在X县,20个去A医院,80个去B医院查询输出。这种事情到底是怎么回事?让我知道是否需要记录查询或我可以做的任何事情以使其更清楚。获取较大组的百分比
hospital A 20
hospital B 80
下面的查询工作完全一样我想它,但它给我想:怎么会这样对我的表中每一个县办呢?
select hospitalname, round(cast(counts as float)/cast(fayettestrokepop as float)*100,2)as percentSeen
from
(
SELECT tblHospitals.hospitalname, COUNT(tblHospitals.hospitalname) AS counts, tblStateCounties_1.countyName,
(SELECT COUNT(*) AS Expr1
FROM Patient INNER JOIN
tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode
WHERE (tblStateCounties.stateCode = '21') AND (tblStateCounties.countyName = 'fayette')) AS fayetteStrokePop
FROM Patient AS Patient_1 INNER JOIN
tblHospitals ON Patient_1.hospitalnpi = tblHospitals.hospitalnpi INNER JOIN
tblStateCounties AS tblStateCounties_1 ON Patient_1.stateCode = tblStateCounties_1.stateCode AND Patient_1.countyCode = tblStateCounties_1.countyCode
WHERE (tblStateCounties_1.stateCode = '21') AND (tblStateCounties_1.countyName = 'fayette')
GROUP BY tblHospitals.hospitalname, tblStateCounties_1.countyName
) as t
order by percentSeen desc
编辑:样本数据 下面的样本数据是不带最外层查询(在as t order by
一部分)。
的countsInTheCounty列是(SELECT COUNT(*)..)部分 'tblStateCounties_1.countyName'
hospitalName hospitalCounts countyName countsInTheCounty
st. james 23 X 300
st. jude 40 X 300
现在,经过与外部查询,我们将得到
st james 0.076 (23/300)
st. jude 0.1333 (40/300)
“循环”在大多数情况下应该像黑板上的钉子。当然,有一种方法可以解决这个问题,使用县名作为额外的分组,你能显示几行样本数据,所以我们知道你是如何达到20/80的? – 2012-07-20 19:44:25
当然,编辑在主要查询块之下。 – wootscootinboogie 2012-07-20 19:51:38
我没有回答这个问题吗? http://stackoverflow.com/a/11086155/61305 – 2012-07-20 20:26:31