查询单个日期,按条件计数值

时间:2018-07-10 09:38:09

标签: ms-access ms-access-2016

我想要一个仅显示单个日期的查询(因此没有单个日期出现两次/多次),计算具有给定日期的记录数,并进一步计算满足其他条件的另一个字段的记录数:

Date    | total amount | Field2 = "Aaa" | Field2 = "Bbb" | Filed2 = ....
10.7.18 | 32           | 2              | 3              | ...
11.7.18 | 20           | 5              | 2              | ...

我的方法是将属性设置为“唯一值”为“是”的查询,然后我两次添加了日期字段,第一次将其功能设置为“ group by”,第二次将其功能设置为“计数”;这仅给我我个人的日期,以及给定日期的记录数。到目前为止一切都很好。

现在,我添加“ Field2”,将其功能设置为“ count”,并将条件添加至“ = Value(“ Aaa”)“。这将显示一个空的记录列表。

1 个答案:

答案 0 :(得分:2)

尝试一下:

Select
    [Date],
    Sum(Amount) As TotalAmount,
    Sum(Abs(Field2 = "Aaa")) As CountA,
    Sum(Abs(Field2 = "Bbb")) As CountB,
    Sum(Abs(Field2 = "Ccc")) As CountC
From
    YourTable
Group By
    [Date]

如果您只需要总数 ,请使用:

Select
    [Date],
    Count(*) As TotalCount,
    Sum(Abs(Field2 = "Aaa")) As CountA,
    Sum(Abs(Field2 = "Bbb")) As CountB,
    Sum(Abs(Field2 = "Ccc")) As CountC
From
    YourTable
Group By
    [Date]