按两列分组计数

时间:2013-07-09 07:24:21

标签: sql count group-by

假设我有一个表格看起来像这样:

:price
:payment_option_id
:created_at

现在,我可以通过这样做获得每个payment_option_id已售出的所有单位(每行被视为一个单位):

SELECT count(*) FROM Order WHERE payment_option_id = 3
SELECT count(*) FROM Order WHERE payment_option_id = 4
SELECT count(*) FROM Order WHERE payment_option_id = ...

但是,现在我想(如果可以在一个查询中)获得每天销售的所有单位,以便我可以报告类似的内容:

7月7日:
-Units with payment_option_id 1:4 units
-Units with payment_option_id 2:0 units

7月8日:
具有payment_option_id 1:12单位的单位 ...

获取此数据的方法是什么?

由于

2 个答案:

答案 0 :(得分:0)

您可以在组中使用逗号分隔的项目,如下所示:

SELECT count(*), created_at FROM Order GROUP BY payment_option_id, created_at

答案 1 :(得分:0)

请尝试:

select 
    Created_at,
    payment_option_id,
    COUNT(*) payment_option_id_num
from
    [Order]
group by Created_at, payment_option_id