我之前曾询问过有关此查询的信息,但我现在提出的问题与之前的问题完全不同。如果需要,请参阅此处了解上一个问题。
下面的代码应该为一个人计算一个数字,它应该使用以下信息:
获取停留时间并给每个范围一个分数,<-这有效
敏锐度评分,<-- 这行得通
共病评分,<-- 这行得通
ER 访问分数。<-- 这不起作用
我将以我自己为例:
假设我于 2013 年 6 月 18 日来到急诊室并入院。出院后,我将有一段时间的住院时间,这是根据 中的案例陈述评分的@T1
,Acuity 也在那里计算。
在第二个查询中,我应该根据 2013 年 6 月 18 日的日期计算过去 6 个月内我来急诊室的次数。这是给我最大的悲痛点,也是我整个查询失败的点。对我来说棘手的是,每次有人进来时,他们都会得到一个访问 ID,但这个人只有一个 MRN 作为他们的唯一标识符。所以我必须根据每次就诊的出院日期计算过去 180 天内有人来过多少次。例如:
6-18-2013MCP
来到急诊室并被录取
6-20-2013MCP
出院
逗留时间 = 2
敏锐度 = 3
MCP
2013 年 6 月 18 日(不计此)的 ER就诊次数 2013 年 1 月 1 日(计此)、2013 年 5 月 15 日(计此) 2013 年 2 月 19 日(计此) 2013 年 1 月 1 日(计)不计算这个)ER 访问 = 3 而不是 5
重要的是要注意对一个人的访问进行评分,而不是对个人本身进行评分,这是由于 ER 访问的日期范围限制。因此,我可以在 6 月 18 日的访问中获得分数,而在 7 月 30 日的访问中获得不同的分数。
到目前为止,我已经完整地给出了查询,以便可以评估我从头到尾的逻辑。我正在使用 SQL Server 2008。
LOS 和 Acute IP 评分查询
-- VARIABLE DECLARATION AND INITIALIZATION
SET ANSI_NULLS OFF
GO
DECLARE @SD DATETIME
DECLARE @ED DATETIME
-- QUERY ONE THAT GETS THE SCORING FOR THE LOS AND ACUITY --########--]
-- THESE ARE PATIENT ADMIT DATES
SET @SD = '2013-01-01'
SET @ED = '2013-01-31'
-- @T1 --------------------------------------------------------------]
-- TABLE DECLARATION WHERE ALL RESULTS WILL GET DEPOSITED OF THE FIRST
-- QUERY WILL GET DEPOSITED. THIS TABLE WILL GET USED IN CONJUNCTION
-- WITH TWO OTHER TABLES IN ORDER TO COMPUTE THE FINAL
DECLARE @T1 TABLE (
ENCOUNTER_ID VARCHAR(200)
, MRN VARCHAR(200)
, [PT AGE] VARCHAR(200)
, [PT NAME] VARCHAR(500)
, [DAYS STAY] VARCHAR(200)
, [LACE DAYS SCORE] INT
, [ACUTE ADMIT SCORE] VARCHAR(100)
, ARRIVAL DATETIME
)
----------------------------------------------------------------------]
-- @T1 RECORD INSERTIONS #############################################]
INSERT INTO @T1
SELECT
A.PT_NO
, A.MED_REC_NO
, A.PT_AGE
, A.PT_NAME
, A.DAYS_STAY
, A.LACE_DAYS_SCORE
, A.ACUTE_ADMIT_LACE_SCORE
, A.ADM_DATE
--####################################################################]
-- DAYS STAY, ACUTE ADMIT AND RELATED SCORING -------------------------
FROM
(SELECT PT_NO
, Med_Rec_No
, Pt_Age
, Pt_Name
, Days_Stay
, CASE
WHEN Days_Stay < 1 THEN 0
WHEN Days_Stay = 1 THEN 1
WHEN Days_Stay = 2 THEN 2
WHEN Days_Stay = 3 THEN 3
WHEN Days_Stay BETWEEN 4 AND 6 THEN 4
WHEN Days_Stay BETWEEN 7 AND 13 THEN 5
WHEN Days_Stay >= 14 THEN 6
END AS LACE_DAYS_SCORE
, CASE
WHEN PLM_PT_ACCT_TYPE = 'I' THEN 3
ELSE 0
END AS ACUTE_ADMIT_LACE_SCORE
, ADM_DATE
FROM SMSDSS.BMH_PLM_PTACCT_V
WHERE DSCH_DATE BETWEEN @SD AND @ED
AND Plm_Pt_Acct_Type = 'I'
)A
--SELECT * FROM @T1
上面的结果应该是这样的:
ENCOUNTER_ID| MRN | AGE | PT NAME | DAYS STAY | LACE DAYS STAY |ACUTE SCR | ARRIVAL
123456789 | 123 | 65 | MCP | 5 | 4 | 3 | 6/18/2013
@JoaoLeal 修复的 ER 计数查询:
-- ER VISITS QUERY
DECLARE @CNT TABLE (
MRN VARCHAR(100)
, VISIT_ID VARCHAR(100)
, VISIT_DATE DATETIME
, VISIT_COUNT INT
)
INSERT INTO @CNT
SELECT
A.MRN
, A.VISIT_ID
, A.VISIT_DATE
, COUNT(B.VISIT_ID) AS VISIT_COUNT
FROM
(SELECT MED_REC_NO AS MRN, VST_START_DTIME AS VISIT_DATE, PT_NO AS VISIT_ID
FROM smsdss.BMH_PLM_PtAcct_V
WHERE
((
PLM_PT_ACCT_TYPE = 'I'
AND ADM_SOURCE NOT IN
('RA',
'RP'
)
)
OR PT_TYPE = 'E')
AND vst_start_dtime BETWEEN @SD AND @ED)A
LEFT JOIN
(SELECT MED_REC_NO AS MRN, VST_START_DTIME AS VISIT_DATE, PT_NO AS VISIT_ID
FROM smsdss.BMH_PLM_PtAcct_V
WHERE
((
PLM_PT_ACCT_TYPE = 'I'
AND ADM_SOURCE NOT IN
('RA',
'RP'
)
)
OR PT_TYPE = 'E')
AND vst_start_dtime BETWEEN @SD AND @ED)B
ON A.MRN = B.MRN
AND A.VISIT_DATE > B.VISIT_DATE AND A.VISIT_DATE-180 <B.VISIT_DATE
GROUP BY A.MRN, A.VISIT_ID, A.VISIT_DATE
ORDER BY A.MRN
--SELECT * FROM @CNT
期望的输出是这样的:
MRN | Visit_ID | Visit_Date | Visit_Count
123 | 12345678 | 6/18/2013 | 1
合并症查询
-- CO MORBIDITY QUERY
DECLARE @CM TABLE (
ENCOUNTER_ID VARCHAR(200)
, [MRN CM] VARCHAR(200)
, NAME VARCHAR(500)
, [CC GRP ONE SCORE] VARCHAR(20)
, [CC GRP TWO SCORE] VARCHAR(20)
, [CC GRP THREE SCORE] VARCHAR(20)
, [CC GRP FOUR SCORE] VARCHAR(20)
, [CC GRP FIVE SCORE] VARCHAR(20)
, [CC LACE SCORE] INT
)
--#####################################################################
INSERT INTO @CM
SELECT
C.PT_NO
, C.MED_REC_NO
, C.PT_NAME
, C.PRIN_DX_CD_1
, C.PRIN_DX_CD_2
, C.PRIN_DX_CD_3
, C.PRIN_DX_CD_4
, C.PRIN_DX_CD_5
, CASE
WHEN (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5) = 0 THEN 0
WHEN (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5) = 1 THEN 1
WHEN (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5) = 2 THEN 2
WHEN (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5) = 3 THEN 3
WHEN (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5) = 4 THEN 4
WHEN (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5) = 5 THEN 5
WHEN (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5) >= 6 THEN 6
END AS CC_LACE_SCORE
FROM (
SELECT distinct PT_NO
, MED_REC_NO
, PT_NAME
, CASE
WHEN PRIN_DX_CD IN (
List of Codes
)
THEN 1
ELSE 0
END AS PRIN_DX_CD_1
, CASE
WHEN PRIN_DX_CD IN (
List of Codes
)
THEN 2
ELSE 0
END AS PRIN_DX_CD_2
, CASE
WHEN PRIN_DX_CD IN (
List of Codes
)
THEN 3
ELSE 0
END AS PRIN_DX_CD_3
, CASE
WHEN PRIN_DX_CD IN (
List of Codes
)
THEN 4
ELSE 0
END AS PRIN_DX_CD_4
, CASE
WHEN PRIN_DX_CD IN (
List of Codes
)
THEN 6
ELSE 0
END AS PRIN_DX_CD_5
FROM smsdss.BMH_PLM_PtAcct_V
WHERE dsch_Date BETWEEN @SD AND @ED
)C
GROUP BY C.PT_NO
, C.MED_REC_NO
, C.PT_NAME
, C.PRIN_DX_CD_1
, C.PRIN_DX_CD_2
, C.PRIN_DX_CD_3
, C.PRIN_DX_CD_4
, C.PRIN_DX_CD_5
ORDER BY (C.PRIN_DX_CD_1+C.PRIN_DX_CD_2+C.PRIN_DX_CD_3+C.PRIN_DX_CD_4+C.PRIN_DX_CD_5)
--SELECT * FROM @CM
所需的输出如下:
Visit_ID | MRN | Name | Grp 1 | Grp 2 | Grp 3 | Grp 4 | Grp 5 | Total
1234567 | 123 | MCP | 0 | 2 | 3 | 4 | 0 | 6
总而言之:
DECLARE @LACE_MSTR TABLE (
MRN VARCHAR(200)
,ENCOUNTER VARCHAR(200)
, AGE VARCHAR(30)
, NAME VARCHAR (500)
, [LACE DAYS SCORE] INT
, [LACE ACUTE IP SCORE] INT
, [LACE ER SCORE] INT
, [LACE COMORBID SCORE] INT
)
INSERT INTO @LACE_MSTR
SELECT
Q1.MRN
, Q1.ENCOUNTER_ID
, Q1.[PT AGE]
, Q1.[PT NAME]
, Q1.[LACE DAYS SCORE]
, Q1.[ACUTE ADMIT SCORE]
, CASE
WHEN Q1.[MRN COUNT] IS NULL THEN 0
WHEN Q1.[MRN COUNT] = 1 THEN 1
WHEN Q1.[MRN COUNT] = 2 THEN 2
WHEN Q1.[MRN COUNT] = 3 THEN 3
WHEN Q1.[MRN COUNT] >= 4 THEN 4
ELSE 0
END AS [LACE ER SCORE]
, Q1.[CC LACE SCORE]
FROM
(
SELECT
DISTINCT T1.ENCOUNTER_ID
, T1.MRN
, T1.[PT AGE]
, T1.[PT NAME]
, T1.[LACE DAYS SCORE]
, T1.[ACUTE ADMIT SCORE]
, CNT.[MRN COUNT]
, CM.[CC LACE SCORE]
FROM
@T1 T1
LEFT OUTER JOIN @CNT CNT
ON T1.MRN = CNT.MRN
JOIN @CM CM
ON CM.ENCOUNTER_ID = T1.ENCOUNTER_ID
)Q1
SELECT DISTINCT ENCOUNTER
, MRN
, AGE
, NAME
, [LACE DAYS SCORE]
, [LACE ACUTE IP SCORE]
, [LACE ER SCORE]
, [LACE COMORBID SCORE]
, [LACE DAYS SCORE]+[LACE ACUTE IP SCORE]+[LACE ER SCORE]+[LACE COMORBID SCORE] AS [TOTAL LACE]
FROM @LACE_MSTR
GROUP BY ENCOUNTER
, MRN
, AGE
, NAME
, [LACE DAYS SCORE]
, [LACE ACUTE IP SCORE]
, [LACE ER SCORE]
, [LACE COMORBID SCORE]
此查询对@CM ON @T1
表的遭遇号进行连接,并通过 MRN 对 ER 访问计数和 T1 表进行连接。由于一些非常奇怪的原因,当我像@CM on @T1
MRN 那样进行最终连接时,我将获得单个 Encounter 编号的多条记录:
ENCOUNTER | MRN | CM SCORE
12345 | 99999 | 9
12345 | 99999 | 11