我正在使用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
以下是SELECT
和FROM
条款:
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
语句,这样我就可以比较datetime
} Order_number
和Order_Description
相等的Visit_ID
。
答案 0 :(得分:1)
我建议开发一个独立的SQL语句,用它们的min(prcs_dtime)选择一组不同的svc_desc。然后我会尝试加入该集合以过滤到所需的结果。 HTH。