如何根据条件分组和计数
How to group by and count based on a condition
我是 Access 新手,我构建了一个查询以根据字段对 table 进行分组,并根据条件获取记录数。
Table:
Category Status
Pen Open
Pencil Open
Pen Closed
Pencil Closed
我正在寻找一个查询,它给我一个如下所示的输出:
Category Open Closed
Pen 1 1
Pencil 1 1
到目前为止我试过了,
SELECT 类别,COUNT(Status='Open'),Count(Status='Closed') FROM table GROUP BY 类别;
此查询未解决问题。
TIA
一种方法是使用 sum
和 iif
函数进行条件聚合:
select
category,
sum(iif(status='Open',1,0)) as Open,
sum(iif(status='Closed',1,0)) as Closed,
from table
group by category
我是 Access 新手,我构建了一个查询以根据字段对 table 进行分组,并根据条件获取记录数。
Table:
Category Status
Pen Open
Pencil Open
Pen Closed
Pencil Closed
我正在寻找一个查询,它给我一个如下所示的输出:
Category Open Closed
Pen 1 1
Pencil 1 1
到目前为止我试过了, SELECT 类别,COUNT(Status='Open'),Count(Status='Closed') FROM table GROUP BY 类别; 此查询未解决问题。
TIA
一种方法是使用 sum
和 iif
函数进行条件聚合:
select
category,
sum(iif(status='Open',1,0)) as Open,
sum(iif(status='Closed',1,0)) as Closed,
from table
group by category