Pivot a single column with different values into multiple column

I have a table the has a column that has multiple values and has an ID for each value, for example  "Extender_List_ID = 2" is for Caliber and "Extender_List_ID = 1"  is for the Model. both values are in the same column "STRNG132" I want to separate them into 2 columns (MODEL CALIBER) based on the ID's  

SELECT     STRNG132 AS Caliber
FROM            (SELECT        a.Extender_Window_ID, b.FIELDNAM, b.Extender_List_ID, c.Extender_List_Item_ID, c.STRNG132
                          FROM            EXT20021 AS c INNER JOIN
                                                    EXT20010 AS b ON c.Extender_List_ID = b.Extender_List_ID CROSS JOIN
                                                    EXT20100 AS a
                          WHERE        (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_1 = b.Field_ID) OR
                                                    (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_2 = b.Field_ID) OR
                                                    (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_3 = b.Field_ID)) AS FI
WHERE        (Extender_List_ID = 2)

SELECT          STRNG132 AS Model
FROM            (SELECT        a.Extender_Window_ID, b.FIELDNAM, b.Extender_List_ID, c.Extender_List_Item_ID, c.STRNG132
                          FROM            EXT20021 AS c INNER JOIN
                                                    EXT20010 AS b ON c.Extender_List_ID = b.Extender_List_ID CROSS JOIN
                                                    EXT20100 AS a
                          WHERE        (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_1 = b.Field_ID) OR
                                                    (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_2 = b.Field_ID) OR
                                                    (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_3 = b.Field_ID)) AS FI
WHERE        (Extender_List_ID = 1)

Open in new window

skull52IT director Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
is possible can you please show few rows from the output you current have and the output you need.
0
Pawan KumarDatabase ExpertCommented:
Please check if this helps.

SELECT *
FROM            
(
	SELECT        
		a.Extender_Window_ID, b.FIELDNAM, b.Extender_List_ID, c.Extender_List_Item_ID
		,CASE WHEN Extender_List_ID = 1 THEN c.STRNG132 END Model
		,CASE WHEN Extender_List_ID = 2 THEN c.STRNG132 END Caliber	 												
		FROM            EXT20021 AS c INNER JOIN
		EXT20010 AS b ON c.Extender_List_ID = b.Extender_List_ID CROSS JOIN
		EXT20100 AS a
		WHERE        (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_1 = b.Field_ID) OR
		(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_2 = b.Field_ID) OR
		(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_3 = b.Field_ID)
												
) AS FI

Open in new window

0
skull52IT director Author Commented:
Caliber Value Current
STRNG132 
.68 CAL                                                                                                                              
10GA                                                                                                                                 
10MM AUTO                                                                                                                            
12GA                                                                                                                                 
12GA/22LR

Open in new window

 

Model Value Current
STRNG132
#1                                                                                                                                   
03A3                                                                                                                                 
1-A                                                                                                                                  
1-AE                                                                                                                                 
1-B 

Open in new window



This is what I want it to look like
Model      Caliber
#1         .68 CAL
03A3     10GA
1-A        10MM AUTO
1-AE      12GA
1-B        12GA/22LR
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Pawan KumarDatabase ExpertCommented:
Please check my last comment...?
0
skull52IT director Author Commented:
got the following error

Msg 209, Level 16, State 1, Line 40
Ambiguous column name 'Extender_List_ID'.
Msg 209, Level 16, State 1, Line 41
Ambiguous column name 'Extender_List_ID'.
0
Pawan KumarDatabase ExpertCommented:
sorry. my bad. updated.

SELECT *
FROM            
(
	SELECT        
		a.Extender_Window_ID, b.FIELDNAM, b.Extender_List_ID, c.Extender_List_Item_ID
		,CASE WHEN c.Extender_List_ID = 1 THEN c.STRNG132 END Model
		,CASE WHEN c.Extender_List_ID = 2 THEN c.STRNG132 END Caliber	 												
		FROM            EXT20021 AS c INNER JOIN
		EXT20010 AS b ON c.Extender_List_ID = b.Extender_List_ID CROSS JOIN
		EXT20100 AS a
		WHERE        (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_1 = b.Field_ID) OR
		(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_2 = b.Field_ID) OR
		(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_3 = b.Field_ID)
												
) AS FI

Open in new window

0
Pawan KumarDatabase ExpertCommented:
I think this should be do it... if not last one.

;WITH CTE AS
(
	SELECT STRNG132 AS Caliber , ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rnk
	FROM            
	(

		SELECT        a.Extender_Window_ID, b.FIELDNAM, b.Extender_List_ID, c.Extender_List_Item_ID, c.STRNG132
		FROM            EXT20021 AS c INNER JOIN
								EXT20010 AS b ON c.Extender_List_ID = b.Extender_List_ID CROSS JOIN
								EXT20100 AS a
		WHERE        (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_1 = b.Field_ID) OR
								(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_2 = b.Field_ID) OR
								(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_3 = b.Field_ID)
						
	) AS FI
	WHERE        (Extender_List_ID = 2)
)
,CTE1 AS 
(
	SELECT          STRNG132 AS Model, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rnk
	FROM            (SELECT        a.Extender_Window_ID, b.FIELDNAM, b.Extender_List_ID, c.Extender_List_Item_ID, c.STRNG132
	FROM            EXT20021 AS c INNER JOIN
							EXT20010 AS b ON c.Extender_List_ID = b.Extender_List_ID CROSS JOIN
							EXT20100 AS a
	WHERE        (a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_1 = b.Field_ID) OR
							(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_2 = b.Field_ID) OR
							(a.Extender_Window_ID = 'Firearminfo') AND (a.Extender_Field_IDs_3 = b.Field_ID)) AS FI
	WHERE        (Extender_List_ID = 1)
)
SELECT c.Caliber , c1.Model FROM CTE1 c1 INNER JOIN CTE c ON c.rnk = c1.rnk

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skull52IT director Author Commented:
Bingo.... that's what i needed, thanks, Pawan
0
Pawan KumarDatabase ExpertCommented:
glad to help as always. :)
0
skull52IT director Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.