繁体   English   中英

使用子查询SQL Server 2008连接两个子查询或查询

[英]Joining two subqueries or a query with a subquery SQL Server 2008

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

列出每个县名称,但每个县名称(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

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

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

;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”。 你熟悉“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

我认为你的问题过于复杂,而且这可以在不将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来获得我认为你需要的所有数据(以及更多)。 我希望列中有足够明智的名称,以便明白每个人应该展示什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM