我有一个查询结果集,可以返回如下所示的结果:
VISIT ID | MRN | ORDER NUMBER | ORDER STATUS | STATUS DATETIME
12345678 | 123 | 987654321 | ACITVE | 2013-06-01 11:00:01.000
12345678 | 123 | 987654321 | DISCONTINUE | 2013-06-01 11:05:00.000
12345678 | 123 | 987654320 | ACTIVE | 2013-06-01 11:10:00.000
12345678 | 123 | 987654320 | IN PROGRESS | 2013-06-01 11:15:00.000
12345678 | 123 | 987654320 | COMPLETE | 2013-06-01 11:25:00.000
我想要做的是摆脱所有订单状态为 Discontinue 的订单号并获得以下输出:
VISIT ID | MRN | ORDER NUMBER | ORDER STATUS | STATUS DATETIME
12345678 | 123 | 987654320 | ACTIVE | 2013-06-01 11:10:00.000
12345678 | 123 | 987654320 | IN PROGRESS | 2013-06-01 11:15:00.000
12345678 | 123 | 987654320 | COMPLETE | 2013-06-01 11:25:00.000
我能想到的唯一方法似乎过于复杂。到目前为止,这是我的代码:
DECLARE @SD DATETIME
DECLARE @ED DATETIME
SET @SD = '2013-06-01';
SET @ED = '2013-06-30';
-- COLUMN SELECTION
SELECT PV.PtNo_Num AS 'VISIT ID'
, PV.Med_Rec_No AS 'MRN'
, PV.Adm_Date AS 'ADMIT'
, PV.Dsch_Date AS 'DISC'
, PV.Days_Stay AS 'LOS'
, PV.pt_type AS 'PT TYPE'
, PV.hosp_svc AS 'HOSP SVC'
, SO.ord_no AS 'ORDER NUMBER'
, SO.ent_dtime AS 'ORDER ENTRY TIME'
, SO.svc_desc AS 'ORDER DESCRIPTION'
, OSM.ord_sts AS 'ORDER STATUS'
, SOS.prcs_dtime AS 'ORDER STATUS TIME'
-- DB(S) USED
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
-- FILTER(S)
WHERE Adm_Date BETWEEN @SD AND @ED
--AND OSM.ord_sts IN ( <--COMMENTED OUT
--'ACTIVE', <--COMMENTED OUT
--'IN PROGRESS', <--COMMENTED OUT
--'COMPLETE' <--COMMENTED OUT
--) <--COMMENTED OUT
AND pv.drg_no IN (067,068,069)
AND SO.svc_cd IN (SVC CODES LIST)
ORDER BY PV.PtNo_Num, SO.ord_no, SOS.prcs_dtime
欢迎任何建议。
谢谢,