MySQL does not have a Pivot function to create a pivot table in MySQL. Pivot tables are useful for data analysis, allow you to display row values as columns to easily get insights. Tyerefore one needs to write SQL query to create pivot table in MySQL. You can use SUM and IF OR CASE statements to create Pivot Table.
Pivot Table in MySQL
Here are the steps to create pivot table in MySQL. Create a table to hold students exam results. See the example below.
CREATE TABLE exams (
id int(11) NOT NULL auto_increment,
name varchar(15),
exam int,
score int,
PRIMARY KEY (id)
);
/* insert some data into the table*/
insert into exams (name,exam,score) values ('Bob',1,70);
insert into exams (name,exam,score) values ('Bob',2,77);
insert into exams (name,exam,score) values ('Bob',3,71);
insert into exams (name,exam,score) values ('Bob',4,70);
insert into exams (name,exam,score) values ('Sue',1,89);
insert into exams (name,exam,score) values ('Sue',2,87);
insert into exams (name,exam,score) values ('Sue',3,88);
insert into exams (name,exam,score) values ('Sue',4,89);
mysql> select * from exams;
+------+------+------+-------+
| id | name | exam | score |
+------+------+------+-------+
| 1 | Bob | 1 | 70 |
| 2 | Bob | 2 | 77 |
| 3 | Bob | 3 | 71 |
| 4 | Bob | 4 | 70 |
| 5 | Sue | 1 | 89 |
| 6 | Sue | 2 | 87 |
| 7 | Sue | 3 | 88 |
| 8 | Sue | 4 | 89 |
+------+------+------+-------+
Let’s say you want to pivot the table by exam column so as to create 1 row for each student and 1 column for each exam, as shown below.
+--------+-----------+-----------+-----------+------------+
| name | exam1 | exam2 | exam3 | exam4 |
+--------+-----------+-----------+-----------+------------+
| Bob | 70 | 77 | 71 | 70 |
| Sue | 89 | 87 | 88 | 89 |
+--------+-----------+-----------+-----------+------------+
You can create a pivot table in MySQL using IF or CASE statement.
Create Pivot Table in MySQL using IF statement
Here’s the SQL query transpose rows to columns using IF statement.
SELECT name,
sum(IF(exam=1, score, NULL)) AS exam1,
sum(IF(exam=2, score, NULL)) AS exam2,
sum(IF(exam=3, score, NULL)) AS exam3,
sum(IF(exam=4, score, NULL)) AS exam4
FROM exams
GROUP BY name;
+--------+-----------+-----------+-----------+------------+
| name | exam1 | exam2 | exam3 | exam4 |
+--------+-----------+-----------+-----------+------------+
| Bob | 70 | 77 | 71 | 70 |
| Sue | 89 | 87 | 88 | 89 |
+--------+-----------+-----------+-----------+------------+
In the above query, you need to group by name column since you want 1 row for each student. Also, you need to provide 1 condition for each column you need to create, that is, 1 condition for each exam
Create Pivot Table in MySQL using CASE statement
Here’s the SQL query to convert rows to columns using CASE statement.
SELECT name,
sum(CASE WHEN exam=1 THEN score ELSE NULL END) AS exam1,
sum(CASE WHEN exam=2 THEN score ELSE NULL END) AS exam2,
sum(CASE WHEN exam=3 THEN score ELSE NULL END) AS exam3,
sum(CASE WHEN exam=4 THEN score ELSE NULL END) AS exam4
FROM exams
GROUP BY name;
+--------+-----------+-----------+-----------+------------+
| name | exam1 | exam2 | exam3 | exam4 |
+--------+-----------+-----------+-----------+------------+
| Bob | 70 | 77 | 71 | 70 |
| Sue | 89 | 87 | 88 | 89 |
+--------+-----------+-----------+-----------+------------+
The above query works similar to the one that uses IF condition above. You need to group by the column by which you want to pivot your data, that is, name. Also, you need to define 1 CASE statement for each exam number since you want to create separate columns for each exam.
You can also combine exam scores in your pivot table. For example, if you want to add up scores of exam1 and exam2 and show them in same column, you can use the following query.
SELECT name,
sum(CASE WHEN exam=1 or exam=2 THEN score ELSE NULL END) AS exam12,
sum(CASE WHEN exam=3 THEN score ELSE NULL END) AS exam3,
sum(CASE WHEN exam=4 THEN score ELSE NULL END) AS exam4
FROM exams
GROUP BY name;
+--------+------------+-----------+-----------+------------+
| name | exam12 | exam2 | exam3 | exam4 |
+--------+------------+-----------+-----------+------------+
| Bob | 147 | 77 | 71 | 70 |
| Sue | 176 | 87 | 88 | 89 |
+--------+------------+-----------+-----------+------------+
In the above table, the scores of exam 1 and exam 2 have been added and displayed in a single column exam12, by simply modifying the condition of 1st CASE statement, and removing the 2nd CASE statement.
Calculate Median in MySQL
Here’s the SQL query to calculate median for the score column.
SELECT AVG(dd.score) as median_val
FROM (
SELECT d.score, @rownum:=@rownum+1 as `row_number`, @total_rows:=@rownum
FROM exams d, (SELECT @rownum:=0) r
WHERE d.score is NOT NULL
-- put some where clause here
ORDER BY d.score
) as dd
WHERE dd.row_number IN ( FLOOR((@total_rows+1)/2), FLOOR((@total_rows+2)/2) );
+------------+
| median_val |
+------------+
| 82.00 |
+------------+