我正在使用 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我希望这一切都清楚