0

我正在使用 SQL Server 2008。我想将三个不同的查询合并在一起,但由于查询的结构不同,我不确定如何正确执行。我的第一个查询创建了两个表来插入记录以进行比较,其中MAX返回访问列的 。前任:

询问:

SET ANSI_NULLS OFF
GO

-- VARIABLE DECLARATION
DECLARE @STARTDATE DATETIME
DECLARE @ENDDATE DATETIME

-- VARIABLE INITIALIZATION
SET @STARTDATE = '6/1/12';
SET @ENDDATE = '1/1/13';

--#############################################################################################
-- TABLE DECLARATION WHICH WILL BE USED TO GET THE MAX ORDERED DATE                           #
--                                                                                            #
DECLARE @T1 TABLE (ENCOUNTER VARCHAR(200), PT_NAME VARCHAR(500), MRN VARCHAR(200),          --#
                   LOS VARCHAR(200), PT_LOC VARCHAR(500), PT_DISPO VARCHAR(500),            --# 
                   LAB_NAME VARCHAR(500), LAB_VALUE VARCHAR(40),LOWER_LIMIT VARCHAR(30),    --# 
                   UPPER_LIMIT VARCHAR(30), HISTORY VARCHAR(10), HAS_HISTORY VARCHAR(10),   --#
                   AB_CODE VARCHAR(30), ORDER_ENTERED VARCHAR(500))                         --#
DECLARE @T2 TABLE (ENCOUNTER2 VARCHAR(200), PT_NAME2 VARCHAR(500), MRN2 VARCHAR(200),       --# 
                   LOS2 VARCHAR(200), PT_LOC2 VARCHAR(500), PT_DISPO2 VARCHAR(500),         --#
                   LAB_NAME2 VARCHAR(500), LAB_VALUE2 VARCHAR(40),LOWER_LIMIT2 VARCHAR(30), --#
                   UPPER_LIMIT2 VARCHAR(30), HISTORY2 VARCHAR(10), HAS_HISTORY2 VARCHAR(10),--# 
                   AB_CODE2 VARCHAR(30), ORDER_ENTERED2 VARCHAR(500))                       --#
--                                                                                            #
--#############################################################################################

--##       TABLE INSERTIONS     ##
--###########################################################################################
--## WHAT GETS PUT INTO TABLE 1 ##
INSERT INTO @T1
SELECT
A.VisitIDCode,
A.ClientDisplayName,
A.IDCode,
A.LOS,
A.CurrentLocation,
A.DischargeDisposition,
A.ItemName,
A.Value,
A.ReferenceLowerLimit,
A.ReferenceUpperLimit,
A.IsHistory,
A.HasHistory,
A.AbnormalityCode,
A.Entered

FROM
(
-- COLUMN SELECTION
SELECT CV.VisitIDCode, CV.ClientDisplayName, CV.IDCode,DATEDIFF(DD,CV.ADMITDTM,CV.DISCHARGEDTM)AS 'LOS',
CV.CurrentLocation, CV.DischargeDisposition, BO.ItemName, BO.Value,
BO.ReferenceLowerLimit, BO.ReferenceUpperLimit, BO.IsHistory, BO.HasHistory, BO.AbnormalityCode,
BO.Entered

-- DB USED: SCM
FROM CV3ClientVisit CV
JOIN CV3BasicObservation BO
ON CV.GUID = BO.ClientVisitGUID

WHERE CV.AdmitDtm BETWEEN @STARTDATE AND @ENDDATE
AND CV.TypeCode = 'INPATIENT'
AND BO.Value IS NOT NULL
AND (BO.ItemName LIKE '%SODIUM LEVEL%'
OR BO.ITEMNAME LIKE '%HEMOG%')
)A 

--##       TABLE INSERTIONS     ##
--###########################################################################################
--## WHAT GETS PUT INTO TABLE 2 ##
INSERT INTO @T2
SELECT
B.VisitIDCode,
B.ClientDisplayName,
B.IDCode,
B.LOS,
B.CurrentLocation,
B.DischargeDisposition,
B.ItemName,
B.Value,
B.ReferenceLowerLimit,
B.ReferenceUpperLimit,
B.IsHistory,
B.HasHistory,
B.AbnormalityCode,
B.Entered

FROM
(
-- COLUMN SELECTION
SELECT CV.VisitIDCode, CV.ClientDisplayName, CV.IDCode,DATEDIFF(DD,CV.ADMITDTM,CV.DISCHARGEDTM) AS 'LOS', 
CV.CurrentLocation,CV.DischargeDisposition, BO.ItemName, BO.Value,BO.ReferenceLowerLimit, BO.ReferenceUpperLimit,   
BO.IsHistory, BO.HasHistory, BO.AbnormalityCode,BO.Entered

-- DB USED: SCM
FROM CV3ClientVisit CV
JOIN CV3BasicObservation BO
ON CV.GUID = BO.ClientVisitGUID


WHERE CV.AdmitDtm BETWEEN @STARTDATE AND @ENDDATE
AND CV.TypeCode = 'INPATIENT'
AND BO.Value IS NOT NULL
AND (BO.ItemName LIKE '%SODIUM LEVEL%'
OR BO.ITEMNAME LIKE '%HEMOG%')
)B

--###########################################################################################
--## HERE IS WHERE WE DO TABLE COMPARISONS ##

SELECT
DISTINCT T1.ENCOUNTER,
T1.PT_NAME AS 'PT NAME', T1.MRN AS 'MRN', T1.LOS AS 'LOS', T1.PT_LOC AS 'PT LOC',
T1.PT_DISPO AS 'PT DISPO', T2.LAB_NAME2 AS 'LAB NAME', T2.LAB_VALUE2, T2.LOWER_LIMIT2 AS 'LOWER LIMIT',
T2.UPPER_LIMIT2 AS 'UPPER LIMIT', T2.AB_CODE2 AS 'AB CODE', T2.ORDER_ENTERED2

FROM @T1 T1
JOIN @T2 T2
ON T1.MRN = T2.MRN2

WHERE
T1.ENCOUNTER = T2.ENCOUNTER2
AND T1.ORDER_ENTERED < T2.ORDER_ENTERED2
AND T2.ORDER_ENTERED2 = (
                        SELECT MAX(TEMP.ORDER_ENTERED2)
                        FROM @T2 TEMP
                        WHERE T1.MRN = TEMP.MRN2
                        )

结果

CLIENT ID    VISIT ID     ARRIVE DATE     VALUE     DATE VALUE ORDERED
.......................................................................
1      |     1      |     1/1/13     |    5     |   1/1/13
1      |     1      |     1/1/13     |    6     |   1/2/13 <- returned row

此查询将返回日期值排序为 1/2/13 的行。

第二个查询计算了客户 ID 列中的客户在 12 个月的时间范围内出现了多少次。因此,对于上述内容,它将返回类似于查询 2 的内容:

-- VARIABLE DECLARATION
DECLARE @STARTDATE DATETIME
DECLARE @ENDDATE DATETIME

-- INITIALIZE VARIABLES
SET @STARTDATE = '6/1/12';
SET @ENDDATE = '1/1/13';

-- COLUMN SELECTION
SELECT DISTINCT CV.IDCode AS 'MRN', COUNT(CV.IDCODE) AS 'COUNT OF IP VISITS'

FROM CV3ClientVisit CV

WHERE CV.AdmitDtm BETWEEN @STARTDATE AND @ENDDATE
AND CV.TypeCode LIKE '%INPATIENT'
AND CV.VisitStatus IN(
'ADM',
'DSC'
)
GROUP BY CV.IDCode
ORDER BY COUNT(CV.IDCode) DESC

结果:客户 ID 访问次数 ................................................... 1 | 2

第三个也是最后一个查询告诉我他们做了多少次,例如

查询 3:

Declare @procedures Table (MRN varchar(20), Patient varchar(80), VisitID varchar(20), Admit datetime, Disch datetime, SurgProc varchar(200), ProcDesc varchar(200))
insert into @procedures


select cv.IDCode,cv.ClientDisplayName,cv.VisitIDCode,AdmitDtm,cv.DischargeDtm,ed.Description,ed.text from CV3ClientVisit cv
left join cV3ClientEventDeclaration ed
    on cv.GUID=ed.ClientVisitGUID
        where ed.typecode = 'Surgery'
        and cv.AdmitDtm > '6/30/12' and cv.AdmitDtm <='1/1/13'
        and Status = 'Active'

    select visitid, COUNT(visitid)as '#Surg Procs' from @procedures
        group by visitid

结果:

CLIENT ID     VISIT ID     ARRIVE DATE     DEPART DATE     COUNT OF PR VISITS
...............................................................................
1         |   1         |  1/1/13       |  1/3/13       |  3

所以我的问题是,我如何正确地JOIN将所有这些查询放在一起以获得一个结果?有时客户可能没有针对其中一个查询的结果。

谢谢你,

PS我希望这一切都清楚

4

2 回答 2

1

我怀疑你不想要 aunion而是想要join. 这会将所有字段放在一行中:

SELECT q1.CLIENT_ID, q1.VISIT_ID, q1.ARRIVE_DATE,
       q3.DEPART_DATE, q3.LENGTH_OF_STAY,
       q1.VALUE, q1.DATE_VALUE_ORDERED,
       q4.Count_of_Visits
FROM (<query1>
     ) q1 left outer join
     (<query2>
     ) q2
     on q1.client_id = q2.client_id left outer join
     (<query3>
     ) q3
     on q1.client_id = q3.client_id and q1.client_id = q3.visit_id left outer join
     (<query4>
     ) q4
     on q1.client_id = q4.client_id and q1.client_id = q4.visit_id

我还猜测您可以在一个更简单的查询中获取所有数据。也就是说,这四个子查询可能不是必需的。如果您有兴趣对此进行调查,您应该问另一个问题。

于 2013-03-29T21:02:56.910 回答
0

一种方法是识别在您要联合的所有查询中选择的所有列,然后将具有空值的适当命名和定位的列(查询中缺少相关列)添加到每个查询 - 即。就像是:

SELECT CLIENT_ID,
       VISIT_ID,
       ARRIVE_DATE,
       NULL DEPART_DATE,
       NULL LENGTH_OF_STAY,
       VALUE,
       DATE_VALUE_ORDERED,
       NULL Count_of_Visits,
       1 QUERY_ID
FROM   {Query 1}
UNION ALL
SELECT CLIENT_ID,
       NULL VISIT_ID,
       NULL ARRIVE_DATE,
       NULL DEPART_DATE,
       NULL LENGTH_OF_STAY,
       NULL VALUE,
       NULL DATE_VALUE_ORDERED,
       Count_of_Visits,
       2 QUERY_ID
FROM   {Query 2}
UNION ALL
SELECT CLIENT_ID,
       VISIT_ID,
       ARRIVE_DATE,
       DEPART_DATE,
       LENGTH_OF_STAY,
       NULL VALUE,
       NULL DATE_VALUE_ORDERED,
       NULL Count_of_Visits,
       3 QUERY_ID
FROM   {Query 3}
UNION ALL
SELECT CLIENT_ID,
       VISIT_ID,
       ARRIVE_DATE,
       DEPART_DATE,
       NULL LENGTH_OF_STAY,
       NULL VALUE,
       NULL DATE_VALUE_ORDERED,
       Count_of_PR_Visits Count_of_Visits,
       4 QUERY_ID
FROM   {Query 4}
于 2013-03-29T19:25:57.403 回答