如何添加日期范围过滤器来计算查询

时间:2017-08-28 18:57:55

标签: sql ms-access

我有以下查询,它结合了来自2个单独表的计数。我希望能够根据日期范围对其进行过滤。这是我正在使用的查询:

SELECT combined.name, Count(combined.id) AS CountOfid
FROM (select ecmreq as name, entryid as id from log
    union all
    select comauthor, comid from elcom
 )  AS combined
GROUP BY combined.name;

如果log有一个名为logdate的日期字段,而elcom有一个名为comdate的字段,我该如何设置它以获得介于08/21/2017和08/28/2017之间的所有计数?

3 个答案:

答案 0 :(得分:1)

您只需将其包含在子查询的WHERE中即可。

SELECT combined.name, Count(combined.id) AS CountOfid
FROM (select ecmreq as name, entryid as id from log WHERE logdate >= '2017-08-21' AND logdate <= '2017-08-28'
    union all
    select comauthor, comid from elcom WHERE comdate >= '2017-08-21' AND comdate <= '2017-08-28'
 )  AS combined
GROUP BY combined.name;

答案 1 :(得分:1)

SELECT combined.name, Count(combined.id) AS CountOfid
FROM (select ecmreq as name, entryid as id, logdate as recdate from log
    union all
    select comauthor, comid, comdate as recdate from elcom
 )  AS combined
where combined.recdate between '08/21/2017' and '08/28/2017'
GROUP BY combined.name;

答案 2 :(得分:0)

WHERE logdate&gt; =&#39; 2017-08-21&#39; AND logdate&lt; =&#39; 2017-08-28&#39;

或多或少你正在寻找什么?

当然,你可以用同样的方式检查elcom,如果你不想包含一个或另一个实际的边界日期,你可以调整比较。