Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Last active August 29, 2015 14:01
Show Gist options
  • Save hidayat365/e4e828acbb1dae153351 to your computer and use it in GitHub Desktop.
Save hidayat365/e4e828acbb1dae153351 to your computer and use it in GitHub Desktop.
Another MySQL Cross Tab or Pivot Table
F:\xampp\mysql\bin>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> SELECT a.rent_date
-> , SUM(CASE WHEN a.branch_id='B001' THEN b.rent_amount ELSE 0 END) manado
-> , SUM(CASE WHEN a.branch_id='B002' THEN b.rent_amount ELSE 0 END) bitung
-> , SUM(CASE WHEN a.branch_id='B003' THEN b.rent_amount ELSE 0 END) tomohon
-> , SUM(b.rent_amount) total
-> FROM t_rent a
-> JOIN t_rent_detail b ON a.rent_id = b.rent_id
-> JOIN t_branch c ON a.branch_id=c.branch_id
-> WHERE a.rent_date BETWEEN '2014-05-01' AND '2014-05-30'
-> GROUP BY rent_date ;
+------------+--------+--------+---------+-------+
| rent_date | manado | bitung | tomohon | total |
+------------+--------+--------+---------+-------+
| 2014-05-11 | 40000 | 10000 | 0 | 50000 |
+------------+--------+--------+---------+-------+
1 row in set (0.01 sec)
mysql>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment