0
我有兩個表,我想加入到一起。一個是具有各種信息位的表格,另一個是維度視圖,各種定義表。與所需信息的表看起來像這樣加入兩列兩列SQL Server 2008 R2
VISIT ID | ATN_DR_NO | ADM_DR_NO |...
12345678 | 987654 | 123456 |...
凡ATN_DR_NO是主治醫師ID號和ADM_DR_NO是承認醫生ID。我有一個具有醫生ID號及其對應名稱的第二個表像這樣
src_prct_no | pract_rpt_name | ...
987654 | Dr. A | ...
123456 | Dr. B | ...
...
我試圖做到的是在一個表,我有以下
VISIT ID | ATN_DR_NO | ATN DR NAME | ADM_DR_NO | ADM DR NAME
12345678 | 987654 | DR. A | 123456 | DR. B
取得成果我不知道如何加入多欄。
這裏是我在整個工作查詢:
SET ANSI_NULLS OFF
GO
-- VARIABLE DECLARATION AND INITIALIZATION
DECLARE @SD DATETIME;
DECLARE @ED DATETIME;
SET @SD = '2014-01-01';
SET @ED = '2014-02-01';
/*
-----------------------------------------------------------------------
THIS QUERY WILL GET ALL THE FRONT END INFORMATION REQUIRED FOR HOSIM
AND INSERT IT INTO A TABLE THAT WILL GET MATCHED UP WITH THE DISCHARGE
ORDERS TABLE
-----------------------------------------------------------------------
START OF QUERY 1
-----------------------------------------------------------------------
*/
-- TABLE DECLARATION
DECLARE @T1 TABLE (
[ENCOUNTER ID] VARCHAR(200)
, /*NEW*/MRN VARCHAR(20)
, /*NEW*/DOB DATETIME
, /*NEW*/GENDER VARCHAR(10)
, FINANCIALCLASSORIG VARCHAR(200)
, [ADMIT DATE] DATE
, [ADMIT TIME] TIME
, [ADMIT FROM] VARCHAR(200)
, [DISCHARGE DATE] DATE
, [DISCHARGE TIME] TIME
, /*TEST*/[ADMITTING MD NO] VARCHAR(200)
, /*TEST*/[ADMITTING MD] VARCHAR(200)
, /*TEST*/[ATTENDING MD NO] VARCHAR(200)
, /*TEST*/[ADTTENDING MD] VARCHAR(200)
, [MS DRG] VARCHAR(200)
, LOS VARCHAR(20)
, [ADMIT PATIENT STATUS] VARCHAR (10)
, [DISCHARGE PATIENT STATUS] VARCHAR(10)
, [DISCHARGE DISPOSITION] VARCHAR (200)
, [DISCHARGE UNIT] VARCHAR (200)
)
-- WHAT GETS INSERTED INTO @T1
INSERT INTO @T1
SELECT
A.[VISIT ID]
, A.MRN
, A.DOB
, A.GENDER
, A.FINANCIALCLASSORIG
, A.[ADMIT DATE]
, A.[ADMIT TIME]
, A.[ADMIT FROM]
, A.[DISCHARGE DATE]
, A.[DISCHARGE TIME]
, A.[ADMITTING DR NO]
, A.[ADMITTING DR]
, A.[ATTENDING DR NO]
, A.[ATTENDING DR]
, A.[MS DRG]
, A.LOS
, A.[ADMIT PATIENT STATUS]
, A.[DISCHARGE PATIENT STATUS]
, A.[DISCHARGE DISPOSITION]
, A.[DISCHARGE UNIT]
-- END @T1 INSERT SELECTION
-- WHERE IT ALL COMES FROM
-- COLUMN SELECTION
FROM (
SELECT DISTINCT PAV.PtNo_Num AS [VISIT ID]
, /*NEW*/PAV.Med_Rec_No AS MRN
, /*NEW*/PAV.Pt_Birthdate AS DOB
, /*NEW*/PAV.Pt_Sex AS GENDER
, PD.pyr_name AS [FinancialClassOrig]
, CAST(PAV.Adm_Date AS DATE) AS [ADMIT DATE]
, CAST(PAV.vst_start_dtime AS TIME) AS [ADMIT TIME]
, PAV.Adm_Source AS [ADMIT FROM]
, CAST(PAV.Dsch_Date AS DATE) AS [DISCHARGE DATE]
, CAST(PAV.Dsch_DTime AS TIME) AS [DISCHARGE TIME]
, /*TEST*/PAV.Adm_Dr_No AS [ADMITTING DR NO]
, /*TEST*/PDV.pract_rpt_name AS [ADMITTING DR]
, /*TEST*/PAV.Atn_Dr_No AS [ATTENDING DR NO]
, /*TEST*/PDV.pract_rpt_name AS [ATTENDING DR]
, PAV.drg_no AS [MS DRG]
, PAV.Days_Stay AS [LOS]
, 'I' AS [ADMIT PATIENT STATUS]
, 'I' AS [DISCHARGE PATIENT STATUS]
, DDM.dsch_disp_desc AS [DISCHARGE DISPOSITION]
, VR.ward_cd AS [DISCHARGE UNIT]
-- FROM DB(S)
FROM smsdss.BMH_PLM_PtAcct_V PAV
JOIN smsdss.pract_dim_v PDV
ON PAV.Adm_Dr_No = PDV.src_pract_no
/*TEST*/AND PAV.Atn_Dr_No = PDV.src_pract_no
JOIN smsdss.pyr_dim PD
ON PAV.Pyr1_Co_Plan_Cd = PD.pyr_cd
JOIN smsmir.vst_rpt VR
ON PAV.PtNo_Num = VR.acct_no
JOIN smsdss.dsch_disp_mstr DDM
ON VR.dsch_disp = DDM.dsch_disp
-- FILTER(S)
WHERE PAV.Dsch_Date >= @SD
AND PAV.Dsch_Date < @ED
AND PAV.Plm_Pt_Acct_Type = 'I'
AND PAV.PtNo_Num < '20000000'
AND PDV.src_spclty_cd = 'HOSIM'
AND PD.orgz_cd = 'S0X0'
AND PD.pyr_name != '?'
) A
SELECT * FROM @T1
你覺得我應該創建像@ADM_DR
和@ATN_DR
和WHERE
子句中的變量設置它們等於select語句,將讓他們? 謝謝,
謝謝傑森,我打算這樣做我覺得有點複雜的方式,並作出一個變量,我很高興我沒有做。 –
我修改了更多的僞代碼,因此更容易閱讀。很高興我能幫上忙。 – Jason