2

私はデータトランザクションテーブルを持っています

Table Data (Month,Year,Type,Color)
+-------+------+-----------------------+---------------+
| Month | Year | type                  | Color         |
+-------+------+-----------------------+---------------+
|     1 | 2013 | MATIC                 | BLACK         |
|     1 | 2013 | MATIC                 | BLACK         |
|     2 | 2013 | MATIC                 | BLACK         |
|     2 | 2013 | MATIC                 | RED           |
|     3 | 2013 | MATIC                 | WHITE         |
|     3 | 2013 | MATIC                 | WHITE         |
|     4 | 2013 | MATIC                 | WHITE         |
|     4 | 2013 | MATIC                 | WHITE         |
|     1 | 2013 | SPORT                 | RED           |
|     1 | 2013 | SPORT                 | YELLOW        |
|     2 | 2013 | SPORT                 | YELLOW        |
|     2 | 2013 | SPORT                 | BLACK         |
|     3 | 2013 | SPORT                 | BLACK         |
+-------+------+-----------------------+---------------+

タイプ、色、年、月ごとにカウント数が欲しいので、このような結果

+-------+------+-----------------------+---------------+--------+
| Month | Year | type                  | Color         | Number |
+-------+------+-----------------------+---------------+--------+
|     1 | 2013 | MATIC                 | BLACK         |      2 |
|     2 | 2013 | MATIC                 | BLACK         |      1 |
|     2 | 2013 | MATIC                 | RED           |      1 |
|     3 | 2013 | MATIC                 | WHITE         |      2 |
|     4 | 2013 | MATIC                 | WHITE         |      2 |
|     1 | 2013 | SPORT                 | RED           |      1 |
|     1 | 2013 | SPORT                 | YELLOW        |      1 |
|     2 | 2013 | SPORT                 | YELLOW        |      1 |
|     2 | 2013 | SPORT                 | BLACK         |      1 |
|     3 | 2013 | SPORT                 | BLACK         |      1 |
+-------+------+-----------------------+---------------+--------+

その月、年、種類、色ごとのグループ、番号を取得するためのクエリは何ですか?

4

1 に答える 1

3
SELECT *, COUNT(*)
FROM table_name
GROUP BY month, year, type, color;  --  TA DA!
于 2013-09-18T15:49:47.867 に答える