I know how to make a pivot table in mysql (see code example below), but what if the number of columns in the pivot table is very large and I don't want to type 2000 or so tagnames? - Is there a way to have that list generated? Many thanks in advance.
drop table pivot;
create table pivot SELECT time,
max(if(tagname = 'a', value, null)) AS 'a',
max(if(tagname = 'b', value, null)) AS 'b',
max(if(tagname = 'c', value, null)) AS 'c'
FROM test where tagname in ('a','b','c')
GROUP BY time;
select * from pivot;
To create a Pivot Table in MySQL, you will need a special MySQL Pivot Table generator tool—for example, dbForge Studio for MySQL contains advanced Pivot Table functionality and can be used to pivot and unpivot data in MySQL. Another way to implement the Pivot Table function is to use a CASE statement.
You can use the PIVOT and UNPIVOT relational operators to change a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output.
A database table can store different types of data and sometimes we need to transform row-level data into column-level data. This problem can be solved by using the PIVOT() function. This function is used to rotate rows of a table into column values.
A PivotTable is an interactive way to quickly summarize large amounts of data. You can use a PivotTable to analyze numerical data in detail, and answer unanticipated questions about your data. A PivotTable is especially designed for: Querying large amounts of data in many user-friendly ways.
You can always create a shell script that does exactly that :-)
#!/bin/sh
mysql -BN test > /tmp/$$_tagnames.tmp <<SQL
select distinct tagname from test;
SQL
cat > /tmp/$$_create_table.sql <<EOF
drop table if exists pivot;
create table pivot select
EOF
while read tag; do
echo "max(if(tagname = '$tag', value, null)) AS '$tag'," >> /tmp/$$_create_table.sql
done < /tmp/$$_tagnames.tmp
cat >> /tmp/$$_create_table.sql <<EOF
time
FROM test
GROUP BY time;
select * from pivot;
EOF
mysql -Bt test < /tmp/$$_create_table.sql
rm /tmp/$$_create_table.sql
rm /tmp/$$_tagnames.tmp
Data:
mysql> select * from test;
+---------+-------+---------------------+
| tagname | value | time |
+---------+-------+---------------------+
| a | foo | 2012-12-21 00:00:01 |
| b | foo | 2012-04-27 00:00:01 |
| c | bar | 2012-03-27 00:00:01 |
| d | bar | 2012-12-21 00:00:01 |
+---------+-------+---------------------+
4 rows in set (0.00 sec)
Script output:
$ ./pivot.sh
+------+------+------+------+---------------------+
| a | b | c | d | time |
+------+------+------+------+---------------------+
| NULL | NULL | bar | NULL | 2012-03-27 00:00:01 |
| NULL | foo | NULL | NULL | 2012-04-27 00:00:01 |
| foo | NULL | NULL | bar | 2012-12-21 00:00:01 |
+------+------+------+------+---------------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With