This is basically a PIVOT
(you did not specify RDBMS) I am assuming MySQL and it does not have a PIVOT
function so you will need to replicate this using an aggregate function with a CASE
statement. This solution adds a rownumber
to each row so you can determine how many name
values you need to transform to columns.
If you know how many name
values you are going to have you can hard-code the values:
select plantid,
max(case when nameRn = 'name1' then name end) Name1,
max(case when nameRn = 'name2' then name end) Name2,
max(case when nameRn = 'name3' then name end) Name3
from
(
select plantid, name,
concat('name', @num := if(@plantid = `plantid`, @num + 1, 1)) as nameRn,
@plantid := `plantid` as dummy
from
(
select plantid, name, @rn:=@rn+1 overall_row_num
from yourtable, (SELECT @rn:=0) r
) x
order by plantid, overall_row_num
) src
group by plantid;
See SQL Fiddle with Demo
If you have an unknown number of values, then you can use a prepared statement to generate a dynamic version of this:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when nameRn = ''',
nameRn,
''' then name end) AS ',
nameRn
)
) INTO @sql
FROM
(
select plantid, name,
concat('name', @num := if(@plantid = `plantid`, @num + 1, 1)) as nameRn,
@plantid := `plantid` as dummy
from
(
select plantid, name, @rn:=@rn+1 overall_row_num
from yourtable, (SELECT @rn:=0) r
) x
order by plantid, overall_row_num
) src;
SET @sql = CONCAT('SELECT plantid, ', @sql, '
FROM
(
select plantid, name,
concat(''name'', @num := if(@plantid = `plantid`, @num + 1, 1)) as nameRn,
@plantid := `plantid` as dummy
from
(
select plantid, name, @rn:=@rn+1 overall_row_num
from yourtable, (SELECT @rn:=0) r
) x
order by plantid, overall_row_num
) src
GROUP BY plantid');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo
Both will generate the same result:
| PLANTID | NAME1 | NAME2 | NAME3 |
-------------------------------------
| 1 | tree | rose | (null) |
| 2 | bush | (null) | (null) |
| 3 | tree | bush | rose |