SQL 中分组依据的不同计数

Distinct Count with Group By in SQL

我在 SQL 的 table 工作,并试图在我的数据集中的某些日子里获取每个零售商的不同交易数量。现在这就是我所拥有的:

SELECT COUNT (*), pos_table.Retailer

FROM pos_table

WHERE Month(Timestamp) = 3 AND Day(Timestamp) = 3 OR Day(Timestamp) = 4

Group By Retailer

这为我提供了每个零售商的数量,但是当我添加“COUNT (DISTINCT pos_table.Transaction) 代替 COUNT (*) 时,它不会 运行。

Access不支持count(distinct [colunm])你可以尝试写一个子查询来实现。

select COUNT(t1.Transaction),t1.Retailer
from (
    SELECT DISTINCT t1.Transaction,t1.Retailer
    FROM pos_table AS t1
    WHERE Month(t1.Timestamp) = 3 AND Day(t1.Timestamp) = 3 OR Day(t1.Timestamp) = 4
) AS t1
GROUP BY t1.Retailer