I tried searching for posts but only found solutions for SQL Server/Access. I need a solution in MySQL (5.X).
I have a table (called history) with 3 columns: hostid, itemname, itemvalue.
If I do a select (select * from History
) it returns
+--------+----------+-----------+ | hostid | itemname | itemvalue | +--------+----------+-----------+ | 1 | A | 10 | +--------+----------+-----------+ | 1 | B | 3 | +--------+----------+-----------+ | 2 | A | 9 | +--------+----------+-----------+ | 2 | C | 40 | +--------+----------+-----------+
How to query the database to return similar content
+--------+------+-----+-----+ | hostid | A | B | C | +--------+------+-----+-----+ | 1 | 10 | 3 | 0 | +--------+------+-----+-----+ | 2 | 9 | 0 | 40 | +--------+------+-----+-----+
I will add a longer, more detailed description of the steps to resolve this issue. I apologize if it's too long.
I'll start with the foundation you gave and use it to define a few terms that I will use throughout the rest of this article. This will bebase table:
This will be our goal,Beautiful pivot table:
The value in thehistory.hostid
column will become they valuein the pivot table. The values in thehistory.itemname
column will becomex-values(for obvious reasons).When I have to solve the problem of creating a pivot table, I use a three-step process to solve it (with an optional fourth step):
. In the desired result,hostidprovides the
Step 2: Extend the base tabley value
anditemnameprovides thex value
.with additional columns. We usually want one column for each x value. Recall that our x-value column isitemname:
Note that we did not change the number of rows - we just added extra columns. Also note the pattern of NULL
- the row with
Step 3: Group and aggregate the extended tableitemname = "A"
has a non-null value for the new columnA
, and the other new column has a null value.. We needgroupingby hostid since it provides the y value:
(Note that we now have one row per y value.)
Okay, we're almost there! We just need to get rid of those uglyNULL.
Step 4: Beautify. We will replace any null values with zeros so that the result set looks better:
We’re done – we’ve built a beautiful pivot table using MySQL. Things to note when applying this process:What values to use in extra columns. I used
itemvalue NULL
- , but it could also be
sum
- , but
group by
- clause (and don't forget to
Known limitations:
This solution does not allow n columns in the pivot table - each pivot column needs to be added manually when extending the base table. So for 5 or 10 x values this solution is fine. 100 yuan, not very good. There are some solutions to using stored procedures to generate queries, but they are ugly and difficult to do correctly. I don't currently know of any good way to solve this problem when the pivot table needs to have many columns.in this example
What "neutral" values to use in extra columns. I used0
or""
, depending on your situationWhat aggregate function to use when grouping. I used
count
andmax
are also often used (max
is often used when building a single row) spread across multiple rows Object")Use multiple columns to represent y values. This solution is not limited to using a single column for the y values - just insert the extra columns into the
select
them)