2017-09-29 5 views
1

一部の行をSQL​​ Serverの列に変換しようとしていますが、結合されたテーブルがすべて混乱しています。私はCASEまたはPIVOTをどこに追加するかわからない。SQL Server:結合されたテーブルを持つ列に一部の行を変換する

DECLARE @BeginDateTime DATETIME 
DECLARE @EndDateTime DATETIME 
SET @BeginDateTime = '20170901' 
SET @EndDateTime = '20170922' 

SELECT DISTINCT  
CONVERT(VARCHAR(10), BILL_TABLE.srvc_thru_dt, 101) AS DischargeDate, PATIENT_TABLE.[Master Patient ID] AS MedRec, PATIENT_TABLE.[Hospital #] AS VisitID, LOCATION_TABLE.loc_ds AS Location, PERSON_TABLE.lst_nm AS LastName, PERSON_TABLE.fst_nm AS FirstName, CONVERT(VARCHAR(10), PATIENT_TABLE.[Date of Birth], 101) AS DOB, PATIENT_TABLE.ZIP, DIAGNOSIS_TABLE.ICD9_diag_ty + CONVERT(VARCHAR(10),DIAGNOSIS_TABLE.ICD9_rank_no) AS DxRank, CODING_TABLE.icd9_code AS Dx 

FROM   BILL_TABLE INNER JOIN 
      PATIENT_TABLE ON BILL_TABLE.vst_int_id = PATIENT_TABLE.[Visit #] INNER JOIN 
      PERSON_TABLE ON PATIENT_TABLE.psn_int_id = PERSON_TABLE.psn_int_id LEFT OUTER JOIN 
      LOCATION_TABLE ON PATIENT_TABLE.loc_lvl_5_id = LOCATION_TABLE.loc_int_id LEFT OUTER JOIN 
      DIAGNOSIS_TABLE ON PATIENT_TABLE.[Visit #] = DIAGNOSIS_TABLE.vst_int_id LEFT OUTER JOIN 
      CODING_TABLE ON DIAGNOSIS_TABLE.icd9_int_id = CODING_TABLE.icd9_int_id 

WHERE (bil_cre_dt >= @BeginDateTime) AND 
    bil_cre_dt < @EndDateTime AND 
    srvc_cd_int_id IN (20329, 21912, 20332, 20333, 20335, 20337, 20339, 20340, 20341, 20343) AND 
    DIAGNOSIS_TABLE.ICD9_diag_ty != 'A' 

これは私に次のような結果が得られます。

DischargeDate | MedRec | VisitID | Location | LastName | FirstName | DOB | ZIP | DxRank | Dx 
---------------|--------|---------|----------|----------|-----------|-----------|-------|--------|------ 
09/20/2017  | 12345 | 111100 | 203 | TESTER | JEFF | 01/01/1984| 90210 | P1 | 4005A 
09/20/2017  | 12345 | 111100 | 203 | TESTER | JEFF | 01/01/1984| 90210 | S2 | R42 
09/20/2017  | 12345 | 111100 | 203 | TESTER | JEFF | 01/01/1984| 90210 | S3 | E860 
09/21/2017  | 23456 | 111101 | 205 | TESTER | BILL | 02/02/2007| 45321 | P1 | N390 

私は結果がこのように見える必要があります。

DischargeDate | MedRec | VisitID | Location | LastName | FirstName | DOB | ZIP | P1 | S2 | S3 
---------------|--------|---------|----------|----------|-----------|-----------|-------|------|------|------- 
09/20/2017  | 12345 | 111100 | 203 | TESTER | JEFF | 01/01/1984| 90210 |4005A | R42 | E860 
09/21/2017  | 23456 | 111101 | 205 | TESTER | BILL | 02/02/2007| 45321 |N39 | NULL| NULL 
+0

正確には.. SQL Serverまたは私のSQLを使用して..? –

+0

これはSQL Serverです。 – 4TDs1Game

答えて

0

私はこのクエリを実行することができませんでしたが、私は、これは動作するはずだと思います -

DECLARE @BeginDateTime DATETIME 
DECLARE @EndDateTime DATETIME 
SET @BeginDateTime = '20170901' 
SET @EndDateTime = '20170922' 

;WITH PIVOTDATA 
AS 
(
    SELECT DISTINCT  
     CONVERT(VARCHAR(10), BT.srvc_thru_dt, 101) AS DischargeDate, 
     PT.[Master Patient ID] AS MedRec, 
     PT.[Hospital #] AS VisitID, 
     LT.loc_ds AS Location, 
     PT1.lst_nm AS LastName, 
     PT1.fst_nm AS FirstName, 
     CONVERT(VARCHAR(10), PT.[Date of Birth], 101) AS DOB, 
     PT.ZIP, 
     DT.ICD9_diag_ty + CONVERT(VARCHAR(10),DT.ICD9_rank_no) AS DxRank, 
     CT.icd9_code AS Dx 
    FROM BILL_TABLE BT 
    INNER JOIN PATIENT_TABLE PT 
    ON BT.vst_int_id = PT.[Visit #] 
    INNER JOIN PERSON_TABLE PT1 
    ON PT.psn_int_id = PT1.psn_int_id 
    LEFT OUTER JOIN LOCATION_TABLE LT 
    ON PT.loc_lvl_5_id = LT.loc_int_id 
    LEFT OUTER JOIN DIAGNOSIS_TABLE DT 
    ON PT.[Visit #] = DT.vst_int_id 
    LEFT OUTER JOIN CODING_TABLE CT 
    ON DT.icd9_int_id = CT.icd9_int_id 
    WHERE (bil_cre_dt >= @BeginDateTime) 
     AND bil_cre_dt < @EndDateTime 
     AND srvc_cd_int_id IN (20329, 21912, 20332, 20333, 20335, 20337, 20339, 20340, 20341, 20343) 
     AND DT.ICD9_diag_ty != 'A' 
) 
SELECT 
    DischargeDate, 
    MedRec, 
    VisitID, 
    Location, 
    LastName, 
    FirstName, 
    DOB, 
    ZIP, 
    P1, S2, S3 
FROM PIVOTDATA 
PIVOT(MAX(Dx) FOR DxRank IN (P1, S2, S3)) AS P; 
0

これは:

DECLARE @BeginDateTime DATETIME 
DECLARE @EndDateTime DATETIME 
SET @BeginDateTime = '20170901' 
SET @EndDateTime = '20170922' 

SELECT DISTINCT  
CONVERT(VARCHAR(10), BILL_TABLE.srvc_thru_dt, 101) AS DischargeDate, PATIENT_TABLE.[Master Patient ID] AS MedRec, PATIENT_TABLE.[Hospital #] AS VisitID, LOCATION_TABLE.loc_ds AS Location, 
PERSON_TABLE.lst_nm AS LastName, PERSON_TABLE.fst_nm AS FirstName, CONVERT(VARCHAR(10), PATIENT_TABLE.[Date of Birth], 101) AS DOB, 
    PATIENT_TABLE.ZIP, 
CASE when (DIAGNOSIS_TABLE.ICD9_diag_ty + CONVERT(VARCHAR(10),DIAGNOSIS_TABLE.ICD9_rank_no))='P1' then CODING_TABLE.icd9_code END P1, 
CASE WHEN (DIAGNOSIS_TABLE.ICD9_diag_ty + CONVERT(VARCHAR(10),DIAGNOSIS_TABLE.ICD9_rank_no))='S2' then CODING_TABLE.icd9_code END S2, 
CASE WHEN (DIAGNOSIS_TABLE.ICD9_diag_ty + CONVERT(VARCHAR(10),DIAGNOSIS_TABLE.ICD9_rank_no))='S3' then CODING_TABLE.icd9_code END S3 
FROM   BILL_TABLE INNER JOIN 
      PATIENT_TABLE ON BILL_TABLE.vst_int_id = PATIENT_TABLE.[Visit #] INNER JOIN 
      PERSON_TABLE ON PATIENT_TABLE.psn_int_id = PERSON_TABLE.psn_int_id LEFT OUTER JOIN 
      LOCATION_TABLE ON PATIENT_TABLE.loc_lvl_5_id = LOCATION_TABLE.loc_int_id LEFT OUTER JOIN 
      DIAGNOSIS_TABLE ON PATIENT_TABLE.[Visit #] = DIAGNOSIS_TABLE.vst_int_id LEFT OUTER JOIN 
      CODING_TABLE ON DIAGNOSIS_TABLE.icd9_int_id = CODING_TABLE.icd9_int_id 

WHERE (bil_cre_dt >= @BeginDateTime) AND 
    bil_cre_dt < @EndDateTime AND 
    srvc_cd_int_id IN (20329, 21912, 20332, 20333, 20335, 20337, 20339, 20340, 20341, 20343) AND 
    DIAGNOSIS_TABLE.ICD9_diag_ty != 'A' 
0

私はこのクエリを試してみてください。

あなたの結果のデータ

select * into #Result from 
(
select '09/20/2017'DischargeDate ,'12345' MedRec ,'111100' VisitID ,'203' Location ,'TESTER' LastName ,'JEFF' FirstName ,'01/01/1984' DOB ,'90210' ZIP ,'P1'DxRank ,'4005A' Dx 
union all 
select '09/20/2017', '12345', '111100', '203', 'TESTER', 'JEFF', '01/01/1984', '90210', 'S2', 'R42' 
union all 
select '09/20/2017', '12345', '111100', '203', 'TESTER', 'JEFF', '01/01/1984', '90210', 'S3', 'E860' 
union all 
select '09/21/2017', '23456', '111101', '205', 'TESTER', 'BILL', '02/02/2007', '45321', 'P1', 'N390' 
)as a 

静的クエリ

;with cte as (
select * from (
select MedRec,DxRank ,Dx from #result 
)as b 
pivot(max(Dx)for DxRank in([P1],[S2],[S3]))as a 
) 
select distinct b.DischargeDate , b.MedRec , b.VisitID , b.Location , b.LastName , b.FirstName ,b.DOB,[P1],[S2],[S3] from cte a 
inner join 
(select DischargeDate , MedRec , VisitID , Location , LastName , FirstName , DOB ,ZIP  from #Result)b 
on b.MedRec=a.Medrec 

動的問合せあなたがしているデータベースエンジン

Declare @columns varchar(max)=stuff((select distinct ','+Dxrank from #result for xml path('')),1,1,'') 
Declare @Query nvarchar(max) 
set @Query=';with cte as (
select * from (
select MedRec,DxRank ,Dx from #result 
)as b 
pivot(max(Dx)for DxRank in('[email protected]+'))as a 
) 
select distinct b.DischargeDate , b.MedRec , b.VisitID , b.Location , b.LastName , b.FirstName , b.DOB,'+ @columns+ ' from cte a 
inner join 
(select DischargeDate , MedRec , VisitID , Location , LastName , FirstName , DOB  from #Result)b 
on b.MedRec=a.Medrec' 

print @Query 
EXEC SP_EXECUTESQL @Query 
関連する問題