2017-08-28 88 views
0

我有以下查詢結合了來自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; 

如果日誌中有一個名爲LOGDATE日期字段和ELCOM有場名爲comdate我將如何設置這讓所有罪狀像2017年8月21日的範圍之間和08/28/2017?

回答

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; 
+0

感謝那些偉大的工作。 – spacerobot

+0

@spacerobot:在Access中,日期表達式應該總是包裝在八卦中:'#2017-08-21#'。 – Gustav

0

WHERE LOGDATE> = '2017年8月21日' AND LOGDATE < = '2017年8月28日'

或多或少你在找什麼?

當然,您可以用同樣的方法檢查elcom,如果您不想包含一個或其他實際邊界日期,您可以調整比較。

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;