2

我正在使用 SQL-Server 2008。

我有一个查询,我在其中提取了一个日期范围内的几列信息。我需要为特定的订单类型列表提取最早的 order_number。一个人可以很容易地为同一件事获得多个订单。我可以在脑海中看到逻辑,但很难将其转移到键盘上。首先,我将给出一个我目前正在返回的示例结果集。

示例结果集:

VISIT ID | MRN | ORDER NUM | ORDER DESC        | ORDER STATUS | ADM TO ORD STS HRS
123456   | 123 | 987654321 | CBC WITH WBC DIFF | ACTIVE       | -4
123456   | 123 | 987654321 | CBC WITH WBC DIFF | IN PROGRESS  | -4
123456   | 123 | 987654321 | CBC WITH WBC DIFF | COMPLETE     | -3
123456   | 123 | 999654321 | CBC WITH WBC DIFF | ACTIVE       | 123
123456   | 123 | 999654321 | CBC WITH WBC DIFF | IN PROGRESS  | 139
123456   | 123 | 999654321 | CBC WITH WBC DIFF | COMPLETE     | 146

我只关心第一个订单,在这种情况下,987654321因为它最早发生。所以我想要的输出是:

期望的输出:

VISIT ID | MRN | ORDER NUM | ORDER DESC        | ORDER STATUS | ADM TO ORD STS HRS
123456   | 123 | 987654321 | CBC WITH WBC DIFF | ACTIVE       | -4
123456   | 123 | 987654321 | CBC WITH WBC DIFF | IN PROGRESS  | -4
123456   | 123 | 987654321 | CBC WITH WBC DIFF | COMPLETE     | -3

这是SELECTANDFROM子句:

DECLARE @SD DATETIME
DECLARE @ED DATETIME
SET @SD = '2013-01-01';
SET @ED = '2013-07-08';

-- COLUMN SELECTION
SELECT DISTINCT PV.PtNo_Num AS 'VISIT ID'
, PV.Med_Rec_No AS 'MRN'
, PV.vst_start_dtime AS 'ADMIT'
, PV.vst_end_dtime 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'
--, DATEDIFF(HOUR,PV.vst_start_dtime,SO.ent_dtime) AS 'ADM TO ENTRY HOURS'
, SO.svc_desc AS 'ORDER DESCRIPTION'
, OSM.ord_sts AS 'ORDER STATUS'
, SOS.prcs_dtime AS 'ORDER STATUS TIME'
, DATEDIFF(HOUR,PV.vst_start_dtime,SOS.prcs_dtime) AS 'ADM TO ORD STS IN HOURS'

FROM smsdss.BMH_PLM_PtAcct_Clasf_Dx_V PDV
JOIN smsdss.BMH_PLM_PtAcct_V PV
ON PDV.PtNo_Num = PV.PtNo_Num
JOIN smsdss.dx_cd_dim_v DX
ON PV.prin_dx_cd = DX.dx_cd
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

这是WHERE查询中的子句:

WHERE PDV.ClasfCd IN (
    LIST OF CLASFCDS
)
AND PV.hosp_svc NOT IN (
    'DIA'
    ,'DMS'
    ,'EME'
    )
AND PV.Adm_Date BETWEEN @SD AND @ED
AND SO.svc_cd IN (
    LIST OF CODES
    )
-- THE FOLLOWING GETS RID OF ORDERS THAT WERE DISCONTINUED
-- ONLY FOR EKG, CHEST XRAY PORTABLE, LACTIC ACID, CBC W/DIFF
AND SO.ord_no NOT IN (
    SELECT SO.ord_no

    FROM smsdss.BMH_PLM_PtAcct_Clasf_Dx_V PDV
    JOIN smsdss.BMH_PLM_PtAcct_V PV
    ON PDV.PtNo_Num = PV.PtNo_Num
    JOIN smsdss.dx_cd_dim_v DX
    ON PV.prin_dx_cd = DX.dx_cd
    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

    WHERE OSM.ord_sts IN (
        'DISCONTINUE'
        ,'CANCEL'
        )
    AND PDV.ClasfCd IN (
        LIST OF CLASFCDS
        )
    AND PV.hosp_svc NOT IN (
        'DIA'
        ,'DMS'
        ,'EME'
        )
    AND SO.svc_cd IN (
        '00407304',
        '00507301',
        '00600015',
        '00402347'
        )
)
ORDER BY PV.PtNo_Num, SO.ord_no, SOS.prcs_dtime

所以我需要在子句中使用准确的方法WHERE,我想添加另一条SELECT FROM WHERE语句,让我比较和相等datetimeOrder_number地方。Order_DescriptionVisit_ID

4

1 回答 1

1

我建议开发一个独立的 SQL 语句,选择一组不同的 svc_desc 及其 min(prcs_dtime)。然后我会尝试加入该集合以过滤到所需的结果。HTH。

于 2013-07-12T19:25:33.777 回答