I have a table that has data coming in like my example here. I need to pivot the table but have no key to reference for the column name. I can do it if there are only two values with Max and Min. However I'm going to eventual end up with 20 columns. Right now I'm concatenating them all into a table and then Splitting them apart into another. I was hoping there was a more efficient way of doing this.
CREATE TABLE test (
ID VarChar(50),
PJQ VarChar(50));
INSERT INTO test
(ID, PJQ)
VALUES
(1001, 'C982401'),
(1001, 'F258147'),
(1000, 'Q985267'),
(1000, 'R258147'),
(1000, 'T258476');
And Then I can do this However it only works with two. I'm going to have to do this 20 times.
SELECT ID,
MAX(PJQ) PJQ1,
MIN(PJQ) PJQ2
FROM test
GROUP BY ID
I need to be able to do this.
ID PJQ1 PJQ2 PJQ3
1000 T258476 Q985267 T258476
1001 F258147 C982401
Any help on this problem would be very helpful. Thanks
Here is a link to Fiddle where I have been trying to figure this out. Fiddle