按月计算日期大于X(日,月,年)的记录

时间:2015-07-04 17:03:18

标签: sql ms-access

我想计算过去12个月(包括当月)的按月记录数。

直到我们到了7月,它结合了2014年7月和2015年的数据。

SELECT Max(MonthName(DatePart("m",[FormSentOff]),True)) AS Mth,
       Count(tblDisclosure.ID) AS CountOfID
FROM tblDisclosure
GROUP BY DatePart("m",[FormSentOff])
HAVING (((DatePart("m",[FormSentOff])) Is Not Null)
   AND ((Max(MonthName(DatePart("m",[FormSentOff]),True))) Is Not Null))
ORDER BY Max(tblDisclosure.FormSentOff), Max(MonthName(DatePart("m",[FormSentOff]),True));`

我试图构建子查询但没有成功,并且尝试以各种方式操作上述代码但没有成功。

1 个答案:

答案 0 :(得分:2)

空气代码:

 
SELECT 
  Format(Max(FormSentOff),"mmm") as Mth, 
  Count(*) as CountOfRecords
FROM tblDisclosure
WHERE FormSentOff >= DateAdd("m",-11,DateSerial(Year(Date()),Month(Date()),1))
GROUP BY Year(FormSentOff), Month(FormSentOff)
ORDER BY Year(FormSentOff), Month(FormSentOff)