Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Created May 23, 2013 23:17
Show Gist options
  • Save hidayat365/5640235 to your computer and use it in GitHub Desktop.
Save hidayat365/5640235 to your computer and use it in GitHub Desktop.
Another MySQL crosstab query
D:\xampp\mysql\bin>mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.16 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> create table jawaban as
-> select 1 Id, 1 pertanyaanId, 123 respondenId, 'B' jawaban union all
-> select 2 Id, 2 pertanyaanId, 123 respondenId, 'A' jawaban union all
-> select 3 Id, 3 pertanyaanId, 123 respondenId, 'C' jawaban union all
-> select 4 Id, 4 pertanyaanId, 123 respondenId, 'D' jawaban union all
-> select 5 Id, 5 pertanyaanId, 123 respondenId, 'A' jawaban union all
-> select 6 Id, 1 pertanyaanId, 456 respondenId, 'A' jawaban union all
-> select 7 Id, 2 pertanyaanId, 456 respondenId, 'D' jawaban union all
-> select 8 Id, 3 pertanyaanId, 456 respondenId, 'C' jawaban union all
-> select 9 Id, 4 pertanyaanId, 456 respondenId, 'A' jawaban union all
-> select 10 Id, 5 pertanyaanId, 456 respondenId, 'B' jawaban ;
Query OK, 10 rows affected (0.04 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from jawaban;
+----+--------------+-------------+---------+
| Id | pertanyaanId | respondenId | jawaban |
+----+--------------+-------------+---------+
| 1 | 1 | 123 | B |
| 2 | 2 | 123 | A |
| 3 | 3 | 123 | C |
| 4 | 4 | 123 | D |
| 5 | 5 | 123 | A |
| 6 | 1 | 456 | A |
| 7 | 2 | 456 | D |
| 8 | 3 | 456 | C |
| 9 | 4 | 456 | A |
| 10 | 5 | 456 | B |
+----+--------------+-------------+---------+
10 rows in set (0.00 sec)
mysql> select respondenId
-> , max(case when pertanyaanId=1 then jawaban else null end) u1
-> , max(case when pertanyaanId=2 then jawaban else null end) u2
-> , max(case when pertanyaanId=3 then jawaban else null end) u3
-> , max(case when pertanyaanId=4 then jawaban else null end) u4
-> , max(case when pertanyaanId=5 then jawaban else null end) u5
-> from jawaban
-> group by respondenId ;
+-------------+------+------+------+------+------+
| respondenId | u1 | u2 | u3 | u4 | u5 |
+-------------+------+------+------+------+------+
| 123 | B | A | C | D | A |
| 456 | A | D | C | A | B |
+-------------+------+------+------+------+------+
2 rows in set (0.00 sec)
mysql>
@hidayat365
Copy link
Author

query dasarnya seperti ini

select respondenId
, max(case when pertanyaanId=1 then jawaban else null end) u1
, max(case when pertanyaanId=2 then jawaban else null end) u2
, max(case when pertanyaanId=3 then jawaban else null end) u3
, max(case when pertanyaanId=4 then jawaban else null end) u4
, max(case when pertanyaanId=5 then jawaban else null end) u5
from jawaban
group by respondenId

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment