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
ANDFROM
句は次のとおりです。
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