Question

I have a table that has several part number columns: PN1, PN2, PN3, otherData.

I want to create an efficient MySQL query that will collapse the part number columns into one column called PN that links the part numbers to otherData.

This transformation is necessary for our business logic. I have a query below that works correctly, but we need to improve performance drastically.

Is there a better way to do the following?

SQL Query which transforms the data correctly, but is inefficient:

SELECT 'Pn', 'Mfg', 'TableId', 'SourceColumn', 'OtherData'
SELECT PN1, Mfg, '26', 'PN1', OtherData FROM Table
UNION ALL
SELECT PN2, Mfg, '26', 'PN2', OtherData FROM Table
UNION ALL
SELECT PN3, Mfg, '26', 'PN3', OtherData FROM Table
UNION ALL
SELECT PN4, Mfg, '26', 'PN4', OtherData 
INTO OUTFILE 'XXX.dat'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM Table d;

The input table looks like the following table:

|  PN1  |  PN2  |  PN3    |  PN4    |  MFG  |  TableId  |  OtherData  |
-----------------------------------------------------------------------
|  asdf |  abde |  12354  |  asdfc  |  2    |  26       |  0.2456     |
|  nupo |  1354 |  null   |  null   |  2    |  26       |  1.53       |
|  ...  |  ...  |  ...    |  ...    |  ...  |  ...      |  ...        |
|  ...  |  ...  |  ...    |  ...    |  ...  |  ...      |  ...        |
|  ...  |  ...  |  ...    |  ...    |  ...  |  ...      |  ...        |

I want the output .dat file to look like this:

"Pn",    "Mfg", "TableId",  "SourceColumn","OtherData"
"asdf",  2,     26,         "PN1",         0.2456
"abde",  2,     26,         "PN2",         0.2456
"12354", 2,     26,         "PN3",         0.2456
"asdfc", 2,     26,         "PN4",         0.2456
"nupo",  2,     26,         "PN1",         1.53
"1354",  2,     26,         "PN2",         1.53
...

Notice that the PN1 through PN4 was collapsed into PN.

Is there a faster way to do this kind of transformation in MySQL?

Was it helpful?

Solution

I cross posted this question under the database administrators page over at

https://dba.stackexchange.com/questions/35514/better-query-to-select-multiple-columns-into-a-single-output-column

It appears that the query is (at least for MySQL) the best I can do for the time being. If MySQL ever implements support for PIVOT / UNPIVOT then this question should be revisited.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top