我有一个我正在处理的查询,我想更改一个人如何被选中的标准,而不是被一个人获得订单的医生类型选中,我希望看到结果回来如果某人有某种类型的医生进行过活动。
在某些情况下,相同的订单会显示1000次或更多次。
以下是我的查询的一些输出示例:
ROW_NUM | VISIT ID | ORD DESC | ORD NUM | ORD STS | ORD DTIME
1 | 123456 | INSERT F | 987654 | ACTIVE | 2013-01-01 10:01:00.000
2 | 123456 | INSERT F | 987654 | ACTIVE | 2013-01-01 10:01:00.000
3 | 123456 | INSERT F | 987654 | ACTIVE | 2013-01-01 10:01:00.000
.
.
.
568 | 123456 | INSERT F | 987654 | ACTIVE | 2013-01-01 10:01:00.000
如图所示,订单号是相同的,所以我只需要它的一个实例,它是Active,In Progress,Complete等。这个查询非常类似于另一个,唯一的区别在于如何我在问这个问题。我觉得这个更新的版本会更精确,只要我开始工作。以下是提供上述结果的查询:
DECLARE @SD DATETIME
DECLARE @ED DATETIME
SET @SD = '2013-06-01';
SET @ED = '2013-06-30';
WITH [SX FLAG] AS (
SELECT
PV.PtNo_Num AS [VISIT ID]
, PV.Med_Rec_No AS MRN
, PV.vst_start_dtime AS ADM
, PV.vst_end_dtime AS DISCH
, PV.Days_Stay AS LOS
, PV.pt_type AS [PT TYPE]
, PV.hosp_svc AS [HOSP SVC]
, SO.ord_no AS [ORD NUM]
, X.[ORD DESC]
, SO.pty_name AS [PARTY NAME]
, OSM.ord_sts AS [ORD STS]
, SOS.prcs_dtime AS [ORD STS TIME]
, DATEDIFF(DAY,PV.vst_start_dtime,SOS.prcs_dtime) AS [ADM TO ORD STS IN DAYS]
, MAX(CASE WHEN [ORD DESC] IN ('INSERT FOLEY', 'REMOVE FOLEY') THEN 1 ELSE 0 END)
OVER (PARTITION BY PV.PTNO_NUM) AS HasInsertRemoveFoley
, MAX(CASE WHEN ACDV.actv_group = 'OR' THEN 1 ELSE 0 END)
OVER (PARTITION BY PV.PTNO_NUM) AS HasORTime
FROM smsdss.BMH_PLM_PtAcct_V PV
JOIN smsmir.sr_ord SO
ON PV.PtNo_Num = SO.episode_no
JOIN smsmir.sr_ord_sts_hist SOS
ON SO.ord_no = SOS.ord_no
JOIN smsmir.ord_sts_modf_mstr OSM
ON SOS.hist_sts = OSM.ord_sts_modf_cd
JOIN smsdss.actv_fct_v AFV
ON PV.Pt_No = AFV.pt_id
JOIN smsdss.actv_cd_dim_v ACDV
ON AFV.actv_cd = ACDV.actv_cd
CROSS APPLY (
SELECT
CASE
WHEN SO.svc_desc = 'INSERT FOLEY CATHETER' THEN 'INSERT FOLEY'
WHEN SO.svc_desc = 'INSERT INDWELLING URINARY CATHETER TO GRAVITY DRAINAGE' THEN 'INSERT FOLEY'
WHEN SO.svc_desc = 'REMOVE INDWELLING URINARY CATHETER' THEN 'REMOVE FOLEY'
ELSE SO.svc_desc
END AS [ORD DESC]
) X
WHERE PV.Adm_Date BETWEEN @SD AND @ED
AND SO.svc_cd IN ('PCO_REMFOLEY' -- <-- The orders I am looking for
,'PCO_INSRTFOLEY' -- <-- for patients who have had a
,'PCO_INSTFOLEY' -- <-- surgical procedure
,'PCO_URIMETER'
)
-- I don't want patients who fall into these pt types
AND PV.hosp_svc NOT IN (
'DIA'
,'DMS'
,'EME'
)
-- This is supposed to kick out orders that were 'Discontinued'
-- or orders that were 'Canceled'
AND SO.ord_no NOT IN (
SELECT SO.ord_no
FROM smsdss.BMH_PLM_PtAcct_V PV
JOIN smsmir.sr_ord SO
ON PV.PtNo_Num = SO.episode_no
JOIN smsmir.sr_ord_sts_hist SOS
ON SO.ord_no = SOS.ord_no
JOIN smsmir.ord_sts_modf_mstr OSM
ON SOS.hist_sts = OSM.ord_sts_modf_cd
JOIN smsdss.actv_fct_v AFV -- <-- gets pt activity
ON PV.Pt_No = AFV.pt_id
JOIN smsdss.actv_cd_dim_v ACDV -- <-- tells me if pt had OR Time
ON AFV.actv_cd = ACDV.actv_cd
WHERE OSM.ord_sts = 'DISCONTINUE' -- <-- don't want these orders
AND SO.svc_cd IN ('PCO_REMFOLEY' -- <-- to show if they were
,'PCO_INSRTFOLEY' -- <-- canned / discontinued
,'PCO_INSTFOLEY'
,'PCO_URIMETER'
)
)
)
SELECT *
FROM [SX FLAG]
WHERE HasInsertRemoveFoley = 1
AND HasORTime = 1
谢谢,
答案 0 :(得分:0)
由您决定您的查询是否产生正确或不正确的结果。我们不能为你做那件事。
如果结果的唯一问题是在结果表中多次出现同一行,则有一种简单的方法可以检查:使用“SELECT DISTINCT”而不是“SELECT”。这可能会减慢您的查询速度,但它可能会帮助您调试它。