2012-12-19 47 views
3

我有一個查詢完全拉取數據,但有一個例外,我希望數據總和在週年紀念日重新開始。我目前的查詢是;基於日期重置總和值的MS-Access 2003 SQL修改

"qry_Vac2" 
UserID Category Gain Used Left 

查詢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*")); 

我的想法是在週年事件發生後重置總和的修改;

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; 

修改的查詢不返回任何數據。思考?

回答

2

週年日期不存在作爲您可以在where語句中引用的字段。要決定要做什麼,您需要發佈SchedulingLog日期的格式。

schedulinglog.eventdate > Month([WM DOH]) & "/" & Day([WM DOH]) & "/" & Year(Date()) 

可能工作。

或者

schedulinglog.eventdate > DateSerial(Year(Date(),Month([WM DOH]),Day([WM DOH])) 

或者

schedulinglog.eventdate > DateAdd("yyyy",1,[WM DOH]) 
+0

有趣。它效果很好。我又一次試圖讓事情變得比他們需要的更困難。謝謝! –