MS-Access 2003 SQL modification for resetting sum values based on a date

偶尔善良 提交于 2020-01-03 20:03:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!