3

怪物编辑:查询现在将运行但返回不正确的答案。添加了粗略的架构。PatientID 不是 tblPatientVisits 表中的主键,因为同一个患者 ID 可以多次出现。

列出了每个县名,但每个 count(s.countyName) 和 count(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

编辑:我有一个现在运行的查询,但它返回

4

3 回答 3

10

没有样本数据和期望的结果很难说,但也许这就是你所追求的?

;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;
于 2012-06-18T15:36:04.363 回答
1

这个查询似乎有几个问题。我假设“s”的子查询旨在替换之前查询中的“s”。你熟悉“with”语法吗?这将接近您的表达方式。

无论如何,您的第一个子查询在 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
于 2012-06-18T14:39:54.527 回答
0

我认为您的问题过于复杂,并且可以在不将 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

我刚刚拿走了你的FROMJOINS得到了我认为你需要的所有数据(还有更多)。我希望为这些列提供足够合理的名称,以便清楚地显示每个列应该显示的内容。

于 2012-06-18T15:36:28.650 回答