Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need Assistance in Pivoting Dates and Providers over Procedures Expand / Collapse
Author
Message
Posted Saturday, August 17, 2013 2:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 20, 2014 8:58 AM
Points: 44, Visits: 175
Hi,

I have a table with the following layout:
[dbo].[EncounterProc](
[COMPANY CODE] [varchar](5) NULL,
[ENCOUNTER NUMBER] [varchar](20) NOT NULL,
[PROCEDURE CODE (ENCTR)] [varchar](15) NOT NULL,
[DATE OF SERVICE] [varchar](8) NOT NULL,
[ENCOUNTER PROC SEQUENCE] [numeric](18, 0) NOT NULL,
[SURGERY FLAG] [varchar](1) NULL,
[ORDERING PHYSICIAN] [varchar](10) NULL,
[SURGEON] [varchar](10) NULL,
[ASSISTING SURGEON(1)] [varchar](10) NULL,
[ASSISTING SURGEON(2)] [varchar](10) NULL,
[SURGERY REASON] [varchar](10) NULL,
[ANESTHESIOLOGIST] [varchar](10) NULL,
[ANESTHESIA TYPE] [varchar](2) NULL,
[PERFUSIONIST] [varchar](10) NULL,
[NURSE ANESTHETIST] [varchar](10) NULL,
[ANESTHESIA START TIME] [varchar](6) NULL,
[ANESTHESIA STOP TIME] [varchar](6) NULL,
[SURGERY START TIME] [varchar](6) NULL,
[SURGERY STOP TIME] [varchar](6) NULL,
[ENTERED OP RM TIME] [varchar](6) NULL,
[LEFT OP RM TIME] [varchar](6) NULL,
[PACU ADMIT TIME] [varchar](6) NULL,
[PACU DISCHARGE TIME] [varchar](6) NULL,
[USER DEFINED 1] [varchar](30) NULL,
[USER DEFINED 2] [varchar](30) NULL,
[USER DEFINED 3] [varchar](30) NULL,
[USER DEFINED 4] [varchar](30) NULL,
[USER DEFINED 5] [varchar](30) NULL,
[USER DEFINED 6] [varchar](30) NULL,
[USER DEFINED 7] [varchar](30) NULL,
[USER DEFINED 8] [varchar](30) NULL,
[USER DEFINED NUMBER 1] [numeric](18, 4) NULL,
[USER DEFINED DATE 1] [varchar](8) NULL,
[PRINCIPLE SECONDARY PROC] [varchar](1) NULL,
[Updated] [datetime] NULL,
CONSTRAINT [PK_EncounterProc2] PRIMARY KEY CLUSTERED
(
[ENCOUNTER NUMBER] ASC,
[PROCEDURE CODE (ENCTR)] ASC,
[DATE OF SERVICE] ASC,
[ENCOUNTER PROC SEQUENCE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[EncounterProc] ADD CONSTRAINT [DF_EncounterProc2_Updated] DEFAULT (getdate()) FOR [Updated]
GO

The data in the table is such that a given encounter can have up to 49 entries containing the account, service date, provider etc.

I need a stored procedure that pivots on each account (1 row per account) and outputs the qualifying procedures, associated date of service and provider. If the 49th procedure qualifies and the others don't, then proc 49 should be populated as well as date49 and provider49, otherwise all columns should be null except for the encounter.

I've written the following query:
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
) o
PIVOT ( MAX([Procedure Code (Enctr)] ) for RowNumber IN
( [1], [Date1], [MD1],[2], [Date2], [MD2], [3], [Date3], [MD3], [4], [Date4], [MD4])
)pr

Unfortunately, I receive an error stating invalid syntax near pr.
What must be done to the above query in order for it to produce the desired result and format?
Post #1485519
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse