Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have the following query that sort of works, but I find it to be kludgy and inefficient. Is there a better way to write this query?

I know this is a bit of an odd query, else I would probably have found some advice when searching, but it is necessary for our business logic. We've been doing this in Hibernate queries and using software to output the file but we're needing to improve performance drastically and that means using raw sql. The output of this query is to be used to partially populate a helper table that we query to figure out which of our 40-odd product tables a particular item is contained in.

Could the following query be replicated using something like GROUP_CONCAT in reverse? Note that the second and third column must be repeated on each line. The fields all contain the same type of data.

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;

Input 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
    ...
    ...
    ...

If I take the INTO OUTFILE clause out, I get an output that looks like this:

    |  Pn      |  Mfg     |  TableId    |  SourceColumn  | OtherData  |
    -----------------------------------------------------------------
    |  "Pn"    |  {blob}  |  "TableId"  |  "PN"          |  {blob}    |
    |  "asdf"  |  {blob}  |  "PN1"      |  "PN1"         |  {blob}    |
    |  "abde"  |  {blob}  |  "PN2"      |  "PN2"         |  {blob}    |
    |  "12354" |  {blob}  |  "PN3"      |  "PN3"         |  {blob}    |
    |  "asdfc" |  {blob}  |  "PN4"      |  "PN4"         |  {blob}    |
    |  "nupo"  |  {blob}  |  "PN1"      |  "PN1"         |  {blob}    |
    |  "1354"  |  {blob}  |  "PN2"      |  "PN2"         |  {blob}    |
    |  ...     |  ...     |  ...        |   ...          |   ...      |
    |  ...     |  ...     |  ...        |   ...          |   ...      |
    |  ...     |  ...     |  ...        |   ...          |   ...      |
share|improve this question
    
What you are looking to do is referred to as "UNPIVOT" in the Microsoft SQL Server world. A quick search for MySQL UNPIVOT on DuckDuckGo.com (or Google, or Bing), returns a ton of helpful results. duckduckgo.com/?q=mysql+unpivot –  Max Vernon Feb 28 '13 at 15:48
    
I appreciate your reply. From what I can see when looking at search results however the alternate solutions appear to be even more kludgy and inefficient than the query I've already written. I guess I'll just have to stick with what I've got for now. In the meantime, if you'll copy your response into an answer I'll mark it as accepted, with comment that my existing solution seems to be the best option as things stand now –  StormeHawke Mar 1 '13 at 17:09

1 Answer 1

up vote 1 down vote accepted

What you are looking to do is referred to as "UNPIVOT" in the Microsoft SQL Server world. A quick search for MySQL UNPIVOT on DuckDuckGo.com (or Google, or Bing), returns a ton of helpful results. duckduckgo.com/?q=mysql+unpivot

share|improve this answer
1  
Thank you for your reply. Doing some research on your suggestion leads me to believe that for the time being my existing solution is the most efficient option currently available for MySQL. I appreciate you taking time to answer my question. –  StormeHawke Mar 1 '13 at 19:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.