首页 > 解决方案 > SQL - 按一列计数和排序

问题描述

需要帮助来计算 Access 中的表并按该特定类型对它们进行分组。

例如,我有这张桌子

桌子

我需要它在查询中输出到这个:

查询输出

标签: sqlms-access

解决方案


这是条件聚合的一个例子。在 MS Access 中,语法为:

select itemname, itemtype,
       sum(iif(building = 'A', 1, 0)) as totalA,
       sum(iif(building = 'B', 1, 0)) as totalB,
       sum(iif(building = 'C', 1, 0)) as totalC
from t
group by itemname, itemtype
order by itemname, itemtype;

推荐阅读