0

我有一个查询结果集,可以返回如下所示的结果:

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

欢迎任何建议。

谢谢,

4

1 回答 1

1

我将创建一个 Ordernumbers 列表,然后使用类似于以下的 where 子句从结果集中删除它们:

WHERE OrderNumber NOT IN
(
   SELECT OrderNumber From smsmir.ord_sts_modf_mstr
   WHERE OrderStatus = 'Discontinue'
)

为了加快速度,您可以首先对子查询的主查询应用任何约束 - 根据您的结果集大小和性能预期,甚至可能值得在子查询中加入和过滤以消除大量 OrderNumber(如果它们是)不需要。此外,DISTINCT声明可能很有用。虽然它必须进行排序和过滤,但它可能比臃肿的NOT IN语句更有效。您也可以颠倒该声明...我假设您的 Discontinued 比 active 少,但情况可能并非如此。IN带有 OrderStatus <> 'Discontinue'的语句可能会更快。如果存在停止状态的非字符串指示,则比字符串值更适合检查。

于 2013-07-08T19:17:17.043 回答