问题
I have a query that is pulling data perfectly with one exception, I want the data sums to start over at an anniversary date. My current query is;
"qry_Vac2"
UserID Category Gain Used Left
the querys SQL;
SELECT
SchedulingLog.UserID,
SchedulingLog.Category,
Sum(IIf([CatDetail] Like 'Ann*',[Value],0))
AS Gain, Sum(IIf([CatDetail] Like '*Used*',[Value],0))+Sum(IIf([CatDetail] Like 'Adj*',[Value],0))
AS Used, [Gain]+[Used] AS [Left]
FROM
SchedulingLog
GROUP BY SchedulingLog.UserID, SchedulingLog.Category
HAVING (((SchedulingLog.Category) Like "Vac*"));
My ideas for a modification to reset the sum after anniversary event;
SELECT Roster.UserID, Roster.[WM DOH], Roster.Schedule, Month([WM DOH]) & "/" & Day([WM DOH]) & "/" & Year(Date()) AS [Anniversary Date], SchedulingLog.Category, Sum(IIf([CatDetail] Like 'Ann*',[Value],0)) AS Gain, Sum(IIf([CatDetail] Like '*Used*',[Value],0))+Sum(IIf([CatDetail] Like 'Adj*',[Value],0)) AS Used, [Gain]+[Used] AS [Left]
FROM SchedulingLog INNER JOIN Roster ON SchedulingLog.UserID = Roster.UserID
WHERE (((SchedulingLog.EventDate)>[Anniversary Date]))
GROUP BY Roster.UserID, Roster.[WM DOH], Roster.Schedule, Month([WM DOH]) & "/" & Day([WM DOH]) & "/" & Year(Date()), SchedulingLog.Category;
The modified query returns no data. Thoughts?
回答1:
Anniversary date does not exists as a field that you can reference in a where statement. To decide what to do, you need to post the format of the SchedulingLog date.
schedulinglog.eventdate > Month([WM DOH]) & "/" & Day([WM DOH]) & "/" & Year(Date())
Might work.
Or
schedulinglog.eventdate > DateSerial(Year(Date(),Month([WM DOH]),Day([WM DOH]))
Or
schedulinglog.eventdate > DateAdd("yyyy",1,[WM DOH])
来源:https://stackoverflow.com/questions/13952164/ms-access-2003-sql-modification-for-resetting-sum-values-based-on-a-date