0

I'm looking for a query that will return me several rows into columns but without knowing the number of rows beforehand. I have searched and the only solutions I found involve knowing how many rows there are.

Here's an example table:

parentID    colA       colB
2           aaaaaa     1000.00
2           bbbbbb     1500.00
3           cccccc     500.00
3           dddddd     700.00
3           eeeeee     2000.00

and i need it to look like:

parentID    colA(n)      colB(n)     colA(n+1)     colB(n+1)     colA(n+2)     colB(n+2)
2           aaaaaaa      1000.00     bbbbbb        1500.00       NULL          NULL
3           cccccc       500.00      dddddd        700.00        eeeeee        2000.00

I realize this should be done in PHP but I need it to be in mysql for a third party excel exporter plugin I'm using.

Edit: Is there a way to do this if I know the maximum number of columns I'll need?

Falantar014
  • 405
  • 2
  • 5
  • 20
  • It must be done with dynamic SQL, either in PHP, or with a MySQL stored procedure that builds and executes a SQL statement dynamically. A single straight SQL statement cannot accomplish it. – Michael Berkowski Oct 30 '12 at 20:31
  • For dynamic number of columns it's not possible (and using dynamic sql as @Michael Berkowski proposed will introduce possible race condition) – zerkms Oct 30 '12 at 20:31
  • i was afraid of that... ok i'll either look into dynamic SQL or find a new solution to exporting to excel. thanks. – Falantar014 Oct 30 '12 at 20:33
  • what if i run a query prior to this to find out the max amount of rows for any given parentID? couldn't i use that information to build a query? – Falantar014 Oct 30 '12 at 20:49
  • You're looking for what is called a "pivot", which exists in SQL-Server, but not MySQL. Check out http://stackoverflow.com/questions/7674786/mysql-pivot-table – newfurniturey Oct 31 '12 at 12:37
  • It isn't exactly a pivot table i'm looking for, I don't need there to be a column called `aaaaaa` I just need it to be split up over several columns instead of one. None of the data (other than parentID) in the first table is consistent. – Falantar014 Oct 31 '12 at 12:52

1 Answers1

1

You cannot do a query in SQL without knowing the number of columns.

The columns of a SELECT-list must be fixed at the time of parsing the query. What you're asking for is that the state of data, which is not known until the query executes, determines the number of columns. That is not the way SQL works.

To accomplish a pivot-type operation, or any query where the data determines the columns, you have two choices:

  • Do a preparatory query to discover how many distinct groups you want to fetch, and use this to build the query with a matching number of columns.

  • Query all the data in rows, fetch it back into your application, and then transform the result set wholly within data structures (i.e. arrays) within your code.

Either way, you need to write application code, either before or after fetching the data.


Re your comment: You're right, this isn't a traditional pivot, but it's similar in that data is driving the number of columns. That is, you need as many columns as 2x the number of rows in the largest group. But they won't all be filled, because the number of rows per group varies. You don't have a fixed number of row per group, therefore you can't fill a fixed number of columns per group.

I'd really recommend you use the latter strategy: fetch the data as rows, as they are stored in the database. Then post-process it in your application code. Loop over the result set and build your data structure incrementally as you fetch rows from the database.

For example, in PHP:

while ($row = $stmt->fetch()) {
  $data[$row['parentID']][] = $row['colA'];
  $data[$row['parentID']][] = $row['colB'];
}
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • ok so for example, I'm able to find out how many rows I need and store it in a variable `numRows`, how would I then 'pivot' the data? It's not a traditional pivot table where I have a column with some sort of consistent data that I can arrange into columns. – Falantar014 Nov 20 '12 at 20:44