我有一个包含过程数据的表。对于给定的遭遇,最多可以有 49 个条目。我想为每个帐户输出一行,并包括程序、日期和提供者。为此,我尝试编写 CASE 语句:
Select DISTINCT [Encounter Number],
CASE When [Encounter Proc Sequence] = '1' Then [Procedure Code (Enctr)]
END as 'Proc1',
Case When [Encounter Proc Sequence] = '1' Then [Date of Service]
END as 'SvcDate1',
CASE When [Encounter Proc Sequence] = '1' Then [Surgeon]
END as 'Surgeon1',
CASE When [Encounter Proc Sequence] = '2' Then [Procedure Code (Enctr)]
END as 'Proc2',
Case When [Encounter Proc Sequence] = '2' Then [Date of Service]
END as 'SvcDate2',
CASE When [Encounter Proc Sequence] = '2' Then [Surgeon]
END as 'Surgeon2',
CASE When [Encounter Proc Sequence] = '3' Then [Procedure Code (Enctr)]
END as 'Proc3',
Case When [Encounter Proc Sequence] = '3' Then [Date of Service]
END as 'SvcDate3',
CASE When [Encounter Proc Sequence] = '3' Then [Surgeon]
END as 'Surgeon3',
CASE When [Encounter Proc Sequence] = '4' Then [Procedure Code (Enctr)]
END as 'Proc4',
Case When [Encounter Proc Sequence] = '4' Then [Date of Service]
END as 'SvcDate4',
CASE When [Encounter Proc Sequence] = '4' Then [Surgeon]
END as 'Surgeon4',
CASE When [Encounter Proc Sequence] = '5' Then [Procedure Code (Enctr)]
END as 'Proc5',
Case When [Encounter Proc Sequence] = '5' Then [Date of Service]
END as 'SvcDate5',
CASE When [Encounter Proc Sequence] = '5' Then [Surgeon]
END as 'Surgeon5',
CASE When [Encounter Proc Sequence] = '6' Then [Procedure Code (Enctr)]
END as 'Proc6',
Case When [Encounter Proc Sequence] = '6' Then [Date of Service]
END as 'SvcDate6',
CASE When [Encounter Proc Sequence] = '6' Then [Surgeon]
END as 'Surgeon6',
CASE When [Encounter Proc Sequence] = '7' Then [Procedure Code (Enctr)]
END as 'Proc7',
Case When [Encounter Proc Sequence] = '7' Then [Date of Service]
END as 'SvcDate7',
CASE When [Encounter Proc Sequence] = '7' Then [Surgeon]
END as 'Surgeon7'
from EncounterProc
where [Date of Service] between '20090101' and '20091231'
and [Procedure Code (ENCTR)] is not null
Group by [Encounter Number], [Encounter Proc Sequence],[Procedure Code (ENCTR)], [Date of
Service], Surgeon
查询输出显示了一些重复的行,并且查询没有将过程 3(例如)放在 Proc3 列中。
我将如何为这种情况设置 PIVOT 语法?
更新:我已阅读 PIVOT 上的 MSDN 库文档,以及评论中共享的链接。我尝试使这些来源适应我的需要,如下所示:
Select [Encounter Number],
[1] as Proc1,
[Date1] as SvcDate1,
[MD1] as MD1,
[2] as Proc2,
[Date2] as SvcDate2,
[MD2] as MD2,
[3] as Proc3,
[Date3] as SvcDate3,
[MD3] as MD3,
[4] as Proc4,
[Date4] as SvcDate4,
[MD4] as MD4
From
(Select * From
(SELECT [Encounter Number]
,[Procedure Code (Enctr)]
,Row_Number() OVER ( Partition By [Encounter Number] Order By
[Encounter Number], [Procedure Code (Enctr)] ) AS RowNumber
FROM EncounterProc)
PIVOT ( MAX([Procedure Code (Enctr)] ) for RowNumber IN
( [1], [Date1], [MD1],[2], [Date2], [MD2], [3], [Date3], [MD3], [4], [Date4],
[MD4]))
当我运行此查询时,SSMS 给我以下错误:
Incorrect syntax near the keyword 'PIVOT'.
Update2:根据 Stuart 的评论,我将查询修改为:
Select [Encounter Number],
p.[1] as Proc1,
p.[Date1] as SvcDate1,
p.[MD1] as MD1,
p.[2] as Proc2,
p.[Date2] as SvcDate2,
p.[MD2] as MD2,
p.[3] as Proc3,
p.[Date3] as SvcDate3,
p.[MD3] as MD3,
p.[4] as Proc4,
p.[Date4] as SvcDate4,
p.[MD4] as MD4
From
(Select * From
(SELECT [Encounter Number]
,[Procedure Code (Enctr)]
,[Date of Service]
,[Surgeon]
,Row_Number() OVER ( Partition By [Encounter Number] Order By
[Encounter Number], [Encounter Proc Sequence] ) AS RowNumber
FROM EncounterProc) p
PIVOT ( MAX([Procedure Code (Enctr)] ) for RowNumber IN
( [1], [Date1], [MD1],[2], [Date2], [MD2], [3], [Date3], [MD3], [4], [Date4],
[MD4]) )
我现在收到一个新错误,说明:
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near ')'.
它指向 PIVOT 语句的右括号。
必须做些什么来修复语法错误?