Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pivot table in mysql

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;
like image 262
user1316758 Avatar asked Apr 06 '12 04:04

user1316758


People also ask

Can I use PIVOT in MySQL?

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.

Can you pivot table in SQL?

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.

How do I PIVOT rows into columns in MySQL?

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.

What is pivot table and its function?

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.


1 Answers

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 |
+------+------+------+------+---------------------+
like image 122
Toni Avatar answered Sep 21 '22 15:09

Toni