Thank you for your solution, it is very elegant... and would love to use it, however I am getting the following error when attempting to run this in SQL Server Management Studio.
I made some minor changes to account for the corrected table name (HANES_ProcessedContainers), and the name of the id (PC_ID) field, as follows:
;WITH mycte AS
(SELECT PC_ID,col, val from (SELECT * FROM Hanes_ProcessedContainers) src
unpivot (val for col in ([Slot1],[Slot2],[Slot3],[Slot4],[Slot5]))unpvt
WHERE right(val,2)='02')
SELECT PC_ID, [Slot1],[Slot2],[Slot3],[Slot4],[Slot5] FROM (select
PC_ID,col, val FROM mycte) src
PIVOT(max(val) FOR col IN ([Slot1],[Slot2],[Slot3],[Slot4],[Slot5])) pvt
I receive the following errors:
"Unable to parse query text" Error Message: Line 1: Incorrect syntax near ';'. Line 1: Incorrect Syntax near 'unpivot'. Line 1: Incorrect Syntax near 'PIVOT'.
Your further assistance would be appreciated. Thanks!!
SmokinJoe
Member
85 Points
209 Posts
Re: SQL Column Selection Based on Meeting Criteria
May 23, 2012 12:49 PM|LINK
Hello Limno,
Thank you for your solution, it is very elegant... and would love to use it, however I am getting the following error when attempting to run this in SQL Server Management Studio.
I made some minor changes to account for the corrected table name (HANES_ProcessedContainers), and the name of the id (PC_ID) field, as follows:
;WITH mycte AS
(SELECT PC_ID,col, val from (SELECT * FROM Hanes_ProcessedContainers) src
unpivot (val for col in ([Slot1],[Slot2],[Slot3],[Slot4],[Slot5]))unpvt
WHERE right(val,2)='02')
SELECT PC_ID, [Slot1],[Slot2],[Slot3],[Slot4],[Slot5] FROM (select PC_ID,col, val FROM mycte) src
PIVOT(max(val) FOR col IN ([Slot1],[Slot2],[Slot3],[Slot4],[Slot5])) pvt
I receive the following errors:
"Unable to parse query text"
Error Message: Line 1: Incorrect syntax near ';'.
Line 1: Incorrect Syntax near 'unpivot'.
Line 1: Incorrect Syntax near 'PIVOT'.
Your further assistance would be appreciated. Thanks!!