我有一个查询结果集可以返回如下所示的结果:
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
欢迎任何建议。
谢谢,
答案 0 :(得分:1)
我创建一个Ordernumbers列表,然后使用类似于的where子句从结果集中删除它们:
WHERE OrderNumber NOT IN
(
SELECT OrderNumber From smsmir.ord_sts_modf_mstr
WHERE OrderStatus = 'Discontinue'
)
为了加快速度,您可以先对子查询的主查询应用任何约束 - 根据您的结果集大小和性能预期,甚至可能值得在子查询中加入和过滤以消除大量的OrderNumbers如果他们不需要。此外,DISTINCT
语句可能很有用。虽然它必须进行排序和过滤,但它可能比膨胀的NOT IN
语句更有效。
您也许可以反转声明......我假设您的已停止活动数量比活动数量少,但情况可能并非如此。具有OrderStatus&lt;&gt;的IN
语句“停止”可能会更快。如果有一个非字符串指示中断状态,那么比字符串值更适合检查。