我正在尝试将某些行转换为SQL Server中的列,但所有连接的表都让我感到困惑。我不知道在哪里或如何添加CASE或PIVOT。
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 :(得分: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;
答案 1 :(得分: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'
答案 2 :(得分: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('+@columns+') )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