I have an output containing fields ID, PRODUCT, BARCODE, TYPEDESC and VALUEDESC. Each product can have multiple TypeDesc with a ValueDesc. Example
I need to PIVOT this, but with the TYPEDESC not being unique, that will create a new column for each TypeDesc.
The desired output would look like this
Where VALUEx and TYPEx would go as far as what there are TypeDesc for each ID. I'm not sure how to go about it, I don't think I'm approaching this the right way
SELECT * FROM ( SELECT ID,PRODUCT,BARCODE,TYPEDESC,VALUEDESC FROM TABLE1 ) SOURCE
PIVOT (
VALUEDESC FOR
TYPEDESC IN (SELECT TYPEDESC FROM SOURCE)
) AS NEWTABLE
Is this going to require a for loop or cursor? Any assistance would be appreciated.
UPDATE: PIVOT is not going to work here. Im going to have to loop through each Product, and populate a table that has an amount of Columns (TypeDesc1, ValueDesc1, 2,3,4,5...) to match select max(cnt) from ( select product,Typedesc,count(1) as cnt from Table1 group by product,typedesc) x
If the loop stops at 3, columns 4-end would be blank. Could this work ?