怪物编辑:查询现在将运行,但将返回不正确的答案。 增加了一个粗略的架构。 PatientID不在tblPatientVisits表的主键,因为相同的patientID可以存在多于一次。
每个县名称上市,但每个计数(s.countyName)和计数(t.countyname)为1
PatientVisits
PatientID int
PatientState varchar(2)
patientCounty varchar(3)
visitNumber int - PK
tblStateCounties
CPK stateCode varchar(2)
CPK countyCode varchar(3)
countyName varchar(25)
SELECT t.countyName,
count(t.countyName) as reAdmits ,count(s.countyName) as totalVisits
FROM (
SELECT countyName,count(countyName) AS readmitCounts
FROM (
SELECT tblPatient.patientID
,tblStateCounties.countyName
FROM tblPatient
INNER JOIN tblPatientVisits
ON tblPatient.patientID = tblPatientVisits.patientID
INNER JOIN tblStateCounties
ON tblPatientVisits.patientState = tblStateCounties.stateCode
AND tblPatientVisits.patientCounty = tblStateCounties.countyCode
GROUP BY tblPatient.patientID
,tblStateCounties.stateCode
,tblStateCounties.countyName
HAVING (COUNT(tblPatient.patientID) > 1)
AND (tblStateCounties.stateCode = '21')
) t
GROUP BY countyname
) t
INNER JOIN (
SELECT countyName
FROM (
SELECT tblStateCounties.countyName
,COUNT(tblStateCounties.countyName) AS counts
FROM tblPatient
INNER JOIN tblPatientVisits
ON tblPatient.patientID = tblPatientVisits.patientID
INNER JOIN tblStateCounties
ON tblPatientVisits.patientState = tblStateCounties.stateCode
AND tblPatientVisits.patientCounty = tblStateCounties.countyCode
WHERE (tblStateCounties.stateCode = '21')
GROUP BY tblStateCounties.countyName
) z
) s
ON s.countyName = t.countyName
group by s.countyname, t.countyname
编辑:我现在运行一个查询,但它返回
很难说没有样本数据和预期的结果,但也许这是你所追求的?
;WITH x AS
(
SELECT c.CountyName, v.patientCounty, v.patientState, p.patientID
FROM dbo.tblPatient AS p
INNER JOIN dbo.tblPatientVisits AS v
ON p.patientID = v.patientID
INNER JOIN dbo.tblStateCounties AS c
ON v.patientState = c.stateCode
AND v.patientCounty = c.countyCode
WHERE c.stateCode = '21'
),
y AS (SELECT CountyName, c = COUNT(*) FROM x GROUP BY CountyName),
z AS (SELECT CountyName, c = COUNT(PatientID) FROM x
GROUP BY CountyName, patientState, PatientID HAVING COUNT(*)>1)
SELECT y.countyName, reAdmits = MAX(COALESCE(z.c, 0)), totalVisits = MAX(y.c)
FROM y LEFT OUTER JOIN z
ON y.CountyName = z.CountyName
GROUP BY y.CountyName;
似乎有这个查询的几个问题。 我假定为“S”的子查询意图之前被替换为在查询的“s”。 你熟悉“与”语法? 这将是接近你是如何表达它。
在任何情况下,你的第一个子查询缺少“)” HAVING子句和GROUP BY之间。 此外, “S” 子查询有GROUP BY,但 “SELECT *”。
以下可能是你想表达什么:
select t.countyName, count(t.countyName), s.countyName, count(s.countyName)
from (select countyName, count(countyName) as readmitCounts
from (SELECT tblPatient.patientID, tblStateCounties.countyName
FROM tblPatient INNER JOIN
tblPatientVisits
ON tblPatient.patientID = tblPatientVisits.patientID INNER JOIN
tblStateCounties
ON tblPatientVisits.patientState = tblStateCounties.stateCode AND
tblPatientVisits.patientCounty = tblStateCounties.countyCode
GROUP BY tblPatient.patientID, tblStateCounties.stateCode, tblStateCounties.countyName
HAVING (COUNT(tblPatient.patientID) > 1) AND (tblStateCounties.stateCode = '21')
) t
group by countyname
) t inner join
(select tblStateCounties.countyName
from (SELECT tblStateCounties.countyName, COUNT(tblStateCounties.countyName) AS counts
FROM tblPatient INNER JOIN
tblPatientVisits
ON tblPatient.patientID = tblPatientVisits.patientID INNER JOIN
tblStateCounties
ON tblPatientVisits.patientState = tblStateCounties.stateCode AND
tblPatientVisits.patientCounty = tblStateCounties.countyCode
WHERE (tblStateCounties.stateCode = '21')
GROUP BY tblStateCounties.countyName
)
on s.countyName = t.countyName
我觉得你过于复杂的问题,并认为这可以在不糖化2个查询一起做到:
SELECT t.countyName,
SUM(Admissions) AS TotalAdmissions,
COUNT(*) AS TotalPatients,
COUNT(CASE WHEN Admissions > 1 THEN PatientID END) AS TotalPatientsReadmitted
SUM(Admissions - 1) AS TotalReadmissions
FROM ( SELECT tblPatient.PatientID,
tblStateCounties.countyName,
COUNT(*) AS Admissions
FROM tblPatient
INNER JOIN tblPatientVisits
ON tblPatient.patientID = tblPatientVisits.patientID
INNER JOIN tblStateCounties
ON tblPatientVisits.patientState = tblStateCounties.stateCode
AND tblPatientVisits.patientCounty = tblStateCounties.countyCode
WHERE tblStateCounties.stateCode = '21'
GROUP BY tblPatient.PatientID, tblStateCounties.countyName
) AS t
GROUP BY CountyName
我刚拍摄的FROM
和JOINS
让我觉得什么是你所需要的(多一点)的数据。 我希望能够给列明智足够的名字为它是什么显而易见的每一个应该显示。