MySQL - Rows to columns with strings -
i've read answer here, mysql - rows columns, , i've been able apply situation until aggregate function (step 3).
i'm trying convert rows in itemname
columns rows in itemvalue
in following table:
+--------+----------+-----------+ | hostid | itemname | itemvalue | +--------+----------+-----------+ | 1 | address | 12 street | | 2 | email | so@gmail | | 3 | name | legend | +--------+----------+-----------+
i applied following code:
create view table_extended ( select history.*, case when itemname = "address" itemvalue end address, case when itemname = "email" itemvalue end email, case when itemname = "name" itemvalue end name history );
and have table_extended:
+--------+----------+-----------+-------------+----------+---------+ | hostid | itemname | itemvalue | address | email | name | +--------+----------+-----------+-------------+----------+---------+ | 1 | address | 12 street | 12 street | null | null | | 1 | email | so@gmail | null | so@gmail | null | | 1 | name | legend | null | null | legend | +--------+----------+-----------+------+------+----------+---------+
in step 3 uses sum
aggregate extended table of values integers. tried creating view, following code, of course these strings turned of rows except hostid 0.
create view history_itemvalue_pivot ( select hostid, sum(address) address, sum(email) email, sum(name) name history_extended group hostid );
it looked this:
+--------+-------------+----------+---------+ | hostid | address | email | name | +--------+-------------+----------+---------+ | 1 | 0 | 0 | 0 | +--------+----------+-----------+------+----+
how can consolidate of rows null
in order following:
+--------+-------------+----------+---------+ | hostid | address | email | name | +--------+----------+-----------+-----------+ | 1 | 12 street | so@gmail | legend | +--------+----------+-----------+------+----+
Comments
Post a Comment