简体   繁体   中英

mysql combine rows into columns

Let's say I have a table with the following information (there could be 40+ unique Measures)

table1

id    name    state    Measure       score
1     Joe     CA       work ethic    40
1     Joe     CA       cleanliness   80
1     Joe     CA       helpfulness   70
2     John    TX       work ethic    70
2     John    TX       helpfulness   50
3     Jack    AZ       helpfulness   50

and I would like to combine everyone's measures into separate columns and make the id unique into a new table that would look something like:

table2

id    name    state    workEthicScore    cleanlinessScore    helpfulnessScore
1     Joe     CA       40                80                  70
2     John    TX       70                null                50
3     Jack    AZ       null              null                50

So, ideally I would like to be able to create this new table without manually typing in all the distinct Measures. How would I go about this using Java and MYSQL? I don't want to use mysql group_concat as I would like to have these as separate columns and not combined into a single column.

Start building your query like this:

  SELECT t.id
       , t.name 
       , t.state
       , 0           AS workEthicScore
       , 0           AS cleanlinessScore
       , 0           AS helpfulnessScore
    FROM mytable t
   GROUP
      BY t.id
       , t.name 
       , t.state

To get the values of score returned, you can use an expression that does conditional aggregation, and replace the literal 0 in the statement above with an expression like this:

       , MAX(IF(t.measure = 'work ethic', t.score,NULL)) AS workEthicScore

Repeat that same pattern for the other values of measure .

For a query like this to work, the number of columns, and the names of the columns, and the expressions used to return a value for the column must be specified in the SQL statement. This cannot be done dynamically within the statement.

To get a result like this dynamically requires two separate statements. The second statement has to be of a form like shown above. You can use a separate statement to get some values to help you build that statement, eg

  SELECT m.measure FROM mytable m GROUP BY m.measure 

or, you could do something fancier, to generate the expression and alias to include in the SELECT list of the second statement:

  SELECT CONCAT('      , MAX(IF(t.measure='''
           ,REPLACE(m.measure,'''','''''')
           ,''',t.score,NULL) AS `'
           ,m.measure
           ,'`'
         ) AS expr
    FROM mytable m
   GROUP BY m.measure

It's not possible to get to get a result like this with dynamic columns, in a single statement.

Here is query-

mysql> select id, 
              name, 
              state, 
              max(if(measure='work ethic', score, null)) as `work ethic`, 
              max(if(measure='cleanliness',score, null)) cleanliness,        
              max(if(measure='helpfullness',score, null)) helpfullness 
        from tt 
        group by id;
+------+------+-------+------------+-------------+--------------+
| id   | name | state | work ethic | cleanliness | helpfullness |
+------+------+-------+------------+-------------+--------------+
|    1 | Joe  | CA    |         40 |          80 |           70 |
|    2 | Jhon | TX    |         70 |        NULL |           50 |
|    3 | Jack | AZ    |       NULL |        NULL |           50 |
+------+------+-------+------------+-------------+--------------+
3 rows in set (0.00 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM