Note that there are some explanatory texts on larger screens.

plurals
  1. POTSQL PIVOT is throwing multi-part identifier could not be bound
    primarykey
    data
    text
    <p>I've created a stored procedure in SQL Server 2005 with the following syntax:</p> <pre><code>SELECT [Encounter Number], [AdmitDate - CCYYMMDD], [DischargeDate - CCYYMMDD], Encounter.LengthOfStay ,pr.[1] as Proc1 ,pr.[2] as Proc2 ,pr.[3] as Proc3 ,pr.[4] as Proc4 ,pr.[5] as Proc5 ,pr.[6] as Proc6 ,pr.[7] as Proc7 ,pr.[8] as Proc8 ,pr.[9] as Proc9 ,pr.[10] as Proc10 ,pr.[11] as Proc11 ,pr.[12] as Proc12 ,pr.[13] as Proc13 ,pr.[14] as Proc14 ,pr.[15] as Proc15 ,pr.[16] as Proc16 ,pr.[17] as Proc17 ,pr.[18] as Proc18 ,pr.[19] as Proc19 ,pr.[20] as Proc20 ,pr.[21] as Proc21 ,pr.[22] as Proc22 ,pr.[23] as Proc23 ,pr.[24] as Proc24 ,pr.[25] as Proc25 ,pr.[26] as Proc26 ,pr.[27] as Proc27 ,pr.[28] as Proc28 ,pr.[29] as Proc29 ,pr.[30] as Proc30 ,pr.[31] as Proc31 ,pr.[32] as Proc32 ,pr.[33] as Proc33 ,pr.[34] as Proc34 ,pr.[35] as Proc35 ,pr.[36] as Proc36 ,pr.[37] as Proc37 ,pr.[38] as Proc38 ,pr.[39] as Proc39 ,pr.[40] as Proc40 ,pr.[41] as Proc41 ,pr.[42] as Proc42 ,pr.[43] as Proc43 ,pr.[44] as Proc44 ,pr.[45] as Proc45 ,pr.[46] as Proc46 ,pr.[47] as Proc47 ,pr.[48] as Proc48 ,pr.[49] as Proc49, CASE WHEN [Procedure Code (ENCTR)] in ('01.25','01.14','01.59') then 'Brain' WHEN [Procedure Code (ENCTR)] = '03.09' then 'Spinal Canal' WHEN [Procedure Code (ENCTR)] in ('06.4','06.81','26.32') then 'Head/Neck' WHEN [Procedure Code (ENCTR)] in ('32.29','32.39','32.49','32.59') then 'Lungs/Thorax' WHEN [Procedure Code (ENCTR)] in ('35.12','35.21','35.22','35.23','35.24') then 'Cardiac Valve' WHEN [Procedure Code (ENCTR)] = '36.10' then 'CABG' WHEN [Procedure Code (ENCTR)] = '00.66' then 'PCI' WHEN [Procedure Code (ENCTR)] in ('37.33','37.34','37.35','37.36','37.37') then 'Excision Heart Lesion' WHEN [Procedure Code (ENCTR)] = '37.94' then 'Implant Auto Cardioversion/Defib System' WHEN [Procedure Code (ENCTR)] in ('39.52','39.71','39.78') then 'Abdominal Aortic Aneurysm' WHEN [Procedure Code (ENCTR)] in ('39.50','39.79','39.72','39.74','39.75','39.76','00.62') then 'Other Vascular' WHEN [Procedure Code (ENCTR)] = '38.12' then 'Carotid Endarterectomy' WHEN [Procedure Code (ENCTR)] in ('38.18','39.29') then 'Lower Limb Vascular' WHEN [Procedure Code (ENCTR)] in ('38.34','38.44','38.45','39.25','39.71') then 'Other Aortic Vascular' WHEN [Procedure Code (ENCTR)] in ('44.38','44.95','44.67') then 'Other Gastrointestinal' WHEN [Procedure Code (ENCTR)] in ('45.71','45.72','45.73','45.74','45.76','45.77','45.78','45.79','45.81','45.82','45.83','45.41', '45.42','45.43','45.44','45.45','45.46','45.47','45.48','45.49','48.35','48.63') then 'Colorectal' END as ProcGrouper From Encounter left outer join DimFactEncounter on Encounter.EncounterNumber = DimFactEncounter.EncounterNumber Left Outer JOIN ( 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) o PIVOT ( MAX([Procedure Code (Enctr)] ) for RowNumber IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],[11], [12], [13], [14], [15], [16], [17], [18], [19], [20] ,[21], [22], [23], [24], [25], [26], [27], [28], [29], [30] ,[31], [32], [33], [34], [35], [36], [37], [38], [39], [40] ,[41], [42], [43], [44], [45], [46], [47], [48], [49]) ) t ) pr on Encounter.EncounterNumber = EncounterProc.[Encounter Number] where [Date of Service] between @StartDate and @EndDate and Encounter.InOutCode = 'I' and AdmitSubService &lt;&gt; 'SIG' and Encounter.HSP# = 1 and Encounter.ActualTotalCharge &gt; 0 and Encounter.Age &gt;= 65 and Encounter.PayorGroup = 'Medicare' and [ED Flag] is null and [DischargeDisposition] not in ('MA', 'TA', '7Z') and EncounterProc.[Procedure Code (ENCTR)] in ('01.25','01.14','01.59','03.09','06.4','06.81','26.32','32.29','32.39','32.49', '32.59','35.12','35.21','35.22','35.23','35.24','36.10','00.66','37.33','37.34','37.35','37.36', '37.37','37.94','39.52','39.71','39.78','39.50','39.79','39.72','39.74','39.75','39.76','00.62', '38.12','38.18','39.29','38.34', '38.44','38.45','39.25','39.71','44.38','44.95','44.67','45.71','45.72','45.73','45.74','45.75', '45.76','45.77', '45.78','45.79','45.81','45.82','45.83','45.41','45.42','45.43','45.44','45.45','45.46','45.47' '45.48','45.49', '48.35','48.63') </code></pre> <p>When I attempt to alter the stored procedure, I receive the following:</p> <blockquote> <p>Msg 4104, Level 16, State 1, Procedure sp_sc_ConRptsSurgery, Line 16<br> The multi-part identifier "EncounterProc.Encounter Number" could not be bound.</p> </blockquote> <p>What must be done to the above syntax to correct the above error?</p> <p>UPDATE: I changed the left outer join to the following:</p> <pre><code>Left Outer JOIN ( SELECT * FROM (SELECT [Encounter Number] ,[Procedure Code (Enctr)] ,Row_Number() OVER ( Partition By [Encounter Number] Order By [EncounterNumber], [Procedure Code (Enctr)] ) AS RowNumber FROM EncounterProc) o PIVOT ( MAX([Procedure Code (Enctr)] ) for RowNumber IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],[11], [12], [13], [14], [15], [16], [17], [18], [19], [20] ,[21], [22], [23], [24], [25], [26], [27], [28], [29], [30] ,[31], [32], [33], [34], [35], [36], [37], [38], [39], [40] ,[41], [42], [43], [44], [45], [46], [47], [48], [49]) ) t ) pr on Encounter.EncounterNumber = pr.[Encounter Number] </code></pre> <p>UPDATE 2: Here is the structure of the EncounterProc Table:</p> <pre><code>CREATE TABLE [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] </code></pre> <p>Unforutnately, the errors are still persisting including:</p> <pre><code>Msg 207, Level 16, State 1, Procedure sp_sc_ConRptsSurgery, Line 95 Invalid column name 'EncounterNumber'. Msg 207, Level 16, State 1, Procedure sp_sc_ConRptsSurgery, Line 103 Invalid column name 'Date of Service'. Msg 207, Level 16, State 1, Procedure sp_sc_ConRptsSurgery, Line 103 Invalid column name 'Date of Service'. Msg 207, Level 16, State 1, Procedure sp_sc_ConRptsSurgery, Line 112 Invalid column name 'Procedure Code (ENCTR)'. </code></pre> <p>Why are these column names deemed invalid?</p> <p>Update 3: Not sure why my question was downvoted? After parsing thrugh the error message, I restructured the stored procedure as follows:</p> <pre><code>SELECT Encounter.EncounterNumber, [AdmitDate - CCYYMMDD], [DischargeDate - CCYYMMDD], Encounter.LengthOfStay ,pr.[1] as Proc1 ,pr.[2] as Proc2 ,pr.[3] as Proc3 ,pr.[4] as Proc4 ,pr.[5] as Proc5 ,pr.[6] as Proc6 ,pr.[7] as Proc7 ,pr.[8] as Proc8 ,pr.[9] as Proc9 ,pr.[10] as Proc10 ,pr.[11] as Proc11 ,pr.[12] as Proc12 ,pr.[13] as Proc13 ,pr.[14] as Proc14 ,pr.[15] as Proc15 ,pr.[16] as Proc16 ,pr.[17] as Proc17 ,pr.[18] as Proc18 ,pr.[19] as Proc19 ,pr.[20] as Proc20 ,pr.[21] as Proc21 ,pr.[22] as Proc22 ,pr.[23] as Proc23 ,pr.[24] as Proc24 ,pr.[25] as Proc25 ,pr.[26] as Proc26 ,pr.[27] as Proc27 ,pr.[28] as Proc28 ,pr.[29] as Proc29 ,pr.[30] as Proc30 ,pr.[31] as Proc31 ,pr.[32] as Proc32 ,pr.[33] as Proc33 ,pr.[34] as Proc34 ,pr.[35] as Proc35 ,pr.[36] as Proc36 ,pr.[37] as Proc37 ,pr.[38] as Proc38 ,pr.[39] as Proc39 ,pr.[40] as Proc40 ,pr.[41] as Proc41 ,pr.[42] as Proc42 ,pr.[43] as Proc43 ,pr.[44] as Proc44 ,pr.[45] as Proc45 ,pr.[46] as Proc46 ,pr.[47] as Proc47 ,pr.[48] as Proc48 ,pr.[49] as Proc49, CASE WHEN [Procedure Code (ENCTR)] in ('01.25','01.14','01.59') then 'Brain' WHEN [Procedure Code (ENCTR)] = '03.09' then 'Spinal Canal' WHEN [Procedure Code (ENCTR)] in ('06.4','06.81','26.32') then 'Head/Neck' WHEN [Procedure Code (ENCTR)] in ('32.29','32.39','32.49','32.59') then 'Lungs/Thorax' WHEN [Procedure Code (ENCTR)] in ('35.12','35.21','35.22','35.23','35.24') then 'Cardiac Valve' WHEN [Procedure Code (ENCTR)] = '36.10' then 'CABG' WHEN [Procedure Code (ENCTR)] = '00.66' then 'PCI' WHEN [Procedure Code (ENCTR)] in ('37.33','37.34','37.35','37.36','37.37') then 'Excision Heart Lesion' WHEN [Procedure Code (ENCTR)] = '37.94' then 'Implant Auto Cardioversion/Defib System' WHEN [Procedure Code (ENCTR)] in ('39.52','39.71','39.78') then 'Abdominal Aortic Aneurysm' WHEN [Procedure Code (ENCTR)] in ('39.50','39.79','39.72','39.74','39.75','39.76','00.62') then 'Other Vascular' WHEN [Procedure Code (ENCTR)] = '38.12' then 'Carotid Endarterectomy' WHEN [Procedure Code (ENCTR)] in ('38.18','39.29') then 'Lower Limb Vascular' WHEN [Procedure Code (ENCTR)] in ('38.34','38.44','38.45','39.25','39.71') then 'Other Aortic Vascular' WHEN [Procedure Code (ENCTR)] in ('44.38','44.95','44.67') then 'Other Gastrointestinal' WHEN [Procedure Code (ENCTR)] in ('45.71','45.72','45.73','45.74','45.76','45.77','45.78','45.79','45.81','45.82','45.83','45.41', '45.42','45.43','45.44','45.45','45.46','45.47','45.48','45.49','48.35','48.63') then 'Colorectal' END as ProcGrouper From Encounter left outer join DimFactEncounter on Encounter.EncounterNumber = DimFactEncounter.EncounterNumber Left Outer JOIN EncounterProc on Encounter.EncounterNumber = EncounterProc.[Encounter Number] Left Outer JOIN ( SELECT [Encounter Number], [DATE OF SERVICE], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],[11], [12], [13], [14], [15], [16], [17], [18], [19], [20],[21], [22], [23], [24], [25], [26], [27], [28], [29], [30] ,[31], [32], [33], [34], [35], [36], [37], [38], [39], [40] ,[41], [42], [43], [44], [45], [46], [47], [48], [49] FROM ( SELECT [Encounter Number], [DATE OF SERVICE] ,[Procedure Code (Enctr)] ,Row_Number() OVER ( Partition By [Encounter Number] Order By [Encounter Number], [Procedure Code (Enctr)] ) AS RowNumber FROM EncounterProc where [Procedure Code (ENCTR)] in ('01.25','01.14','01.59','03.09','06.4','06.81','26.32','32.29','32.39','32.49', '32.59','35.12','35.21','35.22','35.23','35.24','36.10','00.66','37.33','37.34','37.35','37.36', '37.37','37.94','39.52','39.71','39.78','39.50','39.79','39.72','39.74','39.75','39.76','00.62', '38.12','38.18','39.29','38.34', '38.44','38.45','39.25','39.71','44.38','44.95','44.67','45.71','45.72','45.73','45.74','45.75', '45.76','45.77', '45.78','45.79','45.81','45.82','45.83','45.41','45.42','45.43','45.44','45.45', '45.46','45.47','45.48','45.49', '48.35','48.63') ) o PIVOT ( MAX([Procedure Code (Enctr)]) for RowNumber IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],[11], [12], [13], [14],[15], [16], [17], [18], [19], [20],[21], [22], [23], [24], [25], [26], [27], [28], [29], [30] ,[31], [32], [33], [34], [35], [36], [37], [38], [39], [40] ,[41], [42], [43], [44], [45], [46], [47], [48], [49]) ) t ) pr on pr.[Encounter Number] = Encounter.EncounterNumber where pr.[Date of Service] between @StartDate and @EndDate and Encounter.InOutCode = 'I' and AdmitSubService &lt;&gt; 'SIG' and Encounter.HSP# = 1 and Encounter.ActualTotalCharge &gt; 0 and Encounter.Age &gt;= 65 and Encounter.PayorGroup = 'Medicare' and [ED Flag] is null and [DischargeDisposition] not in ('MA', 'TA', '7Z') </code></pre> <p>The stored procedure runs error free now. However, I'm seeing the same encounter number duplicated for however many rows as it has procedures. If there are 7 procedures, the stored procedure returns 7 rows for this encounter.</p> <p>How would I modify the updated code above to ensure only one row per encounter is returned by the stored procedure?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload