使用单个SQL来获取具有不同where子句的多个计数,这可能吗?

时间:2013-02-04 23:16:05

标签: mysql sql count where-clause

我有一张如下表格,

+-----+--------+-----------+----------+
| id  | type_id| product_id| date     |
+-----+--------+-----------+----------+
|  1  |   1    |   300     |22/01/2013|
|  2  |   1    |   800     |22/01/2013|
|  3  |   1    |   400     |30/01/2013|
|  4  |   1    |   300     |05/02/2013|
|  5  |   5    |   300     |27/02/2013|
|  6  |   1    |   300     |28/02/2013|
|  7  |   3    |   400     |12/03/2013|
|  8  |   5    |   400     |02/03/2013|
|  9  |   1    |   300     |06/03/2013|
| 10  |   1    |   400     |06/03/2013|
| 11  |   5    |   400     |06/03/2013|
| 12  |   1    |   400     |08/03/2013|

让我们说如果我想按月和按年计算每个产品组的每种类型的数量,这可能吗?我所追求的输出应该类似于以下...

+-----------+------------+-------------+------------+------+------+
|product_id |count_type_1|count_type_3 |count_tyep_5| month| year |
+-----------+------------+-------------+------------+------+------+
| 300       |     1      |      0      |     0      |   01 | 2013 |
| 800       |     1      |      0      |     0      |   01 | 2013 |
| 400       |     1      |      0      |     0      |   01 | 2013 |
| 300       |     2      |      0      |     1      |   02 | 2013 |
| 300       |     1      |      0      |     0      |   03 | 2013 |
| 400       |     2      |      1      |     2      |   03 | 2013 | 

这可以使用单个SQL实现吗?

PS:这是在MYSQL服务器上

2 个答案:

答案 0 :(得分:6)

SELECT
  product_id,
  COUNT(CASE WHEN type_id=1 THEN 1 END) count_type_1,
  COUNT(CASE WHEN type_id=2 THEN 1 END) count_type_2,
  COUNT(CASE WHEN type_id=3 THEN 1 END) count_type_3,
  COUNT(CASE WHEN type_id=4 THEN 1 END) count_type_4,
  COUNT(CASE WHEN type_id=5 THEN 1 END) count_type_5,
  MONTH(date) month,
  YEAR(date) year
FROM
  products
GROUP BY
  product_id,
  MONTH(date),
  YEAR(date)

答案 1 :(得分:5)

您可以使用以下内容 pivot 使用聚合函数和CASE表达式创建每列的数据:

select 
  product_id,
  sum(case when type_id=1 then 1 else 0 end) count_type_1,
  sum(case when type_id=2 then 1 else 0 end) count_type_2,
  sum(case when type_id=3 then 1 else 0 end) count_type_3,
  sum(case when type_id=4 then 1 else 0 end) count_type_4,
  sum(case when type_id=5 then 1 else 0 end) count_type_5,
  month(date) month,
  year(date) year
from yourtable
group by product_id, month(date), year(date)

请参阅SQL Fiddle with Demo

这给出了结果:

| PRODUCT_ID | COUNT_TYPE_1 | COUNT_TYPE_2 | COUNT_TYPE_3 | COUNT_TYPE_4 | COUNT_TYPE_5 | MONTH | YEAR |
--------------------------------------------------------------------------------------------------------
|        300 |            1 |            0 |            0 |            0 |            0 |     1 | 2013 |
|        300 |            2 |            0 |            0 |            0 |            1 |     2 | 2013 |
|        300 |            1 |            0 |            0 |            0 |            0 |     3 | 2013 |
|        400 |            1 |            0 |            0 |            0 |            0 |     1 | 2013 |
|        400 |            2 |            0 |            1 |            0 |            2 |     3 | 2013 |
|        800 |            1 |            0 |            0 |            0 |            0 |     1 | 2013 |