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 回答