Mysql で集計行の追加 with rollup

Mysql について、group by でカウントして、最後に合計を表示するやり方。

できて当たり前のようだけど、見当がつかなかった。調べたら簡単にできたのでメモ。

[sql gutter="false"]mysql> select kousinbi,count(*) from tairyu_tmp group by kousinbi;
+————+———-+
| kousinbi?? | count(*) |
+————+———-+
| 2011-08-27 |??????? 3 |
| 2011-09-07 |??????? 1 |
| 2011-09-09 |??????? 1 |
| 2011-09-10 |??????? 2 |
| 2011-09-11 |????? 242 |
| 2011-09-12 |????? 300 |
| 2011-09-13 |????? 419 |
| 2011-09-14 |????? 232 |
| 2011-09-15 |????? 448 |
| 2011-09-16 |?????? 15 |
| 2011-09-17 |???? 1449 |
| 2011-09-20 |???? 1080 |
| 2011-09-21 |???? 2342 |
| 2011-09-22 |????? 260 |
| 2011-09-23 |???? 1538 |
| 2011-09-24 |???? 1319 |
| 2011-09-26 |?????? 35 |
| 2011-09-27 |???? 4073 |
+————+———-+
18 rows in set (0.56 sec)
[/sql]

 

最後に with rollup をつけるそうな。おみごと。
[sql gutter="false" highlight_lines="23″]mysql> select kousinbi,count(*) from tairyu_tmp group by kousinbi with rollup;
+————+———-+
| kousinbi?? | count(*) |
+————+———-+
| 2011-08-27 |??????? 3 |
| 2011-09-07 |??????? 1 |
| 2011-09-09 |??????? 1 |
| 2011-09-10 |??????? 2 |
| 2011-09-11 |????? 242 |
| 2011-09-12 |????? 300 |
| 2011-09-13 |????? 419 |
| 2011-09-14 |????? 232 |
| 2011-09-15 |????? 448 |
| 2011-09-16 |?????? 15 |
| 2011-09-17 |???? 1449 |
| 2011-09-20 |???? 1080 |
| 2011-09-21 |???? 2342 |
| 2011-09-22 |????? 260 |
| 2011-09-23 |???? 1538 |
| 2011-09-24 |???? 1319 |
| 2011-09-26 |?????? 35 |
| 2011-09-27 |???? 4073 |
| NULL?????? |??? 13759 |
+————+———-+
19 rows in set (0.56 sec)[/sql]

 

 

関連コンテンツ

5,664ビュー

パソコン

Posted by movinow