我有一个看起来像这样的查询......
SELECT tbl1.ProjectID, tbl1.EntryDate AS StartDate, tbl2.EntryDate AS EndDate
FROM checklist_entries tbl1
INNER JOIN checklist_entries tbl2
ON tbl1.ProjectID = tbl2.ProjectID
WHERE tbl1.ChecklistDay = (SELECT ChecklistDayMin FROM milestone_def WHERE MilestoneDefID = [@milestoneID])
AND tbl2.ChecklistDay = (SELECT ChecklistDayMax FROM milestone_def WHERE MileStoneDefID = [@milestoneID])
如您所见,这是一个自连接表,它返回一个ProjectID(FK),一个StartDate和EndDate,它们都是表中的EntryDate列。为了返回记录,我需要传入一个整数值[@milestoneID],它允许子查询返回WHERE子句所需的最小值和最大值。
由于此查询返回两个日期,因此我想了解如何构建此查询并添加另一个返回的列,其中TotalDays(TotalDays为工作日,周一至周五)的整数值由返回值确定StartDate和EndDate。
我可以找到一个返回天数的查询,但基本上我想在ACCESS中创建一个查询,允许我调用此查询(并传入[@MilestoneID]参数)并为每个返回的记录添加一个列到TotalDays计数的末尾。
答案 0 :(得分:2)
与我的回答here类似,您可以使用
创建一个VBA函数以包含工作日的数量SELECT
... ,
CountWeekdays(tbl1.EntryDate, tbl2.EntryDate) AS TotalWeekdays
FROM ...
只需在Access中创建一个新的Module
并将以下代码粘贴到其中:
Public Function CountWeekdays(Date1 As Date, Date2 As Date) As Long
Dim StartDate As Date, EndDate As Date, _
Weekdays As Long, i As Long
If Date1 > Date2 Then
StartDate = Date2
EndDate = Date1
Else
StartDate = Date1
EndDate = Date2
End If
Weekdays = 0
For i = 0 To DateDiff("d", StartDate, EndDate)
Select Case Weekday(DateAdd("d", i, StartDate))
Case 1, 7
' weekend - do nothing
Case Else
Weekdays = Weekdays + 1
End Select
Next
CountWeekdays = Weekdays
End Function