如何报告MS Access数据,计算列与group by

时间:2014-07-03 14:17:08

标签: sql vba ms-access asp-classic

我有一个Access 2003数据库,它在大约200个用户的中型组织中记录故障呼叫帮助请求。通过经典ASP页面记录(并附加到数据库中)呼叫,系统管理员团队使用单独的经典ASP网页来查看呼叫,提供响应等。

所有调用都记录在一个名为tblFaultCall的表中,其结构位于

之下
tblFault call
ID : Autonumber
strName
strPhone
dtmDateOpen : Date/Time (date call logged)
dtmDateClosed : Date/Time (date call closed)
dtmTime : Date/Time (time call logged)
strStatus (always 'Open', 'Pending' or 'Closed')
strCategory (always one of 10 categories, held as as list in tblCatgory, and used in lookup lists in the ASP web page)
strFaultDesc 
strResolution
strCallOwner
dtmDatePending : Date/Time (date call set to pending, if it ever was)

对于管理层,我需要一种轻松创建季度报告的方法,如下所示

Call recieved between dd/mm/yyyy and dd/mm/yyyy
----
Category    Calls recieved  Of which 'Closed'   closed within 5 days    Closed within 14 days   Open    Pending 
Cateogry x  1052            950                 700                     200                     50      50
Cateogry Y  65              60                  50                      5                       0       5

我需要一个简单的方法来做到这一点。我需要经理能够插入他想要的日期,然后点击一个按钮就可以了。我无法弄清楚如何创建一个提供所有这些的查询。只提供Open调用的类别和数量很容易,但是无法确定如何添加另一列来显示已关闭的呼叫数,或者在x天内关闭的数字等等。我可以创建单独的查询更难的列,但不是一起完成。

所以,选项是

经典ASP - 我认为计算字段会涉及很多单独的SQL 访问报告? 某种导出到Excel? Excel中的VBA链接回Access中准备好的查询?

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:1)

每个列都可以计算,主要是使用iif语句:

Total calls = count(calls)

Closed calls = sum(iif(<call is closed>,1,0)  (however you define <call is closed>)

Closed in 5 days = sum(iif(<call is closed in 5 days>,1,0))

等等

答案 1 :(得分:1)

您应该能够在一个查询中获取该数据。试试这个:

SELECT AllCalls.strCategory, CallsReceived, CallsClosed, ClosedWithin5Days, ClosedWithin14days, CallsOpen, CallsPending
FROM
((
   SELECT strCategory,
      Count(ID) AS CallsReceived,
      Sum(IIF(strStatus='Closed',1,0)) AS CallsClosed,
      Sum(IIF(strStatus='Open',1,0)) AS CallsOpen,
      Sum(IIF(strStatus='Pending',1,0)) AS CallsPending
   FROM tblFaultCall
   WHERE dtmDateOpen BETWEEN #6/1/2014# and #6/30/2014#
   GROUP BY strCategory
) AS AllCalls
LEFT JOIN 
(
   SELECT strCategory,
      Count(ID) AS ClosedWithin5Days
   FROM tblFaultCall
   WHERE DateDiff("d", dtmDateOpen, dtmDateClosed) <=5
     AND dtmDateOpen BETWEEN #6/1/2014# and #6/30/2014#
   GROUP BY strCategory
) AS FiveDay ON AllCalls.strCategory=FiveDay.strCategory)
LEFT JOIN 
(
   SELECT strCategory,
      Count(ID) AS ClosedWithin14Days
   FROM tblFaultCall
   WHERE DateDiff("d", dtmDateOpen, dtmDateClosed) between 5 and 14
     AND dtmDateOpen BETWEEN #6/1/2014# and #6/30/2014#
   GROUP BY strCategory
) AS FourteenDay ON AllCalls.strCategory=FourteenDay.strCategory

经典ASP部分应该与您的其他页面非常相似:查询数据库,循环生成结果数据,将其输出到屏幕。如果您也在生成电子表格,也可以使用相同的方法。