根据条件在Access的SQL查询中创建日期字段

时间:2018-02-27 19:44:28

标签: ms-access access-vba

我是新手用户,最近才开始在Access中使用VBA。我的任务是更改复杂的数据库以反映一个月的新日期。而不是将月初作为1,我的客户希望月份从23日到22日。这将给他们一周的时间来准备处理。

我设法让大部分工作正常,但有以下异常。我需要根据新标准分配DT。

虽然它的开发方式远非我使用的方法。我坚持让它发挥作用。

以下是SQL视图中的旧代码:

SELECT G.Part,
       G.Process,
       Sum(G.QPass2) AS QtyPass,
       Sum(G.QFail2) AS QtyFail,
       Sum(G.QNull2) AS QtyNull,
       IIf(Sum(SYtd)=0,NULL,Sum(SYtd)) AS Sprayed_Yesterday,
       Sum(G.SpMTD) AS SprayedMTD,
       G.AftMkt,
       G.DT
FROM
  (SELECT Sprayed.Part,
          Sprayed.Process,
          Sum(Sprayed.QPass) AS QPass2,
          Sum(Sprayed.QFail) AS QFail2,
          Sum(Sprayed.QNull) AS QNull2,
          Sum(IIF(Sprayed.Date_Stamp = Date()-1,Sprayed.QPass + Sprayed.QFail + Sprayed.QNull,0)) AS SYtd,
          Sum(Sprayed.Qpass + Sprayed.QFail + Sprayed.QNull) AS SpMTD,
          Sprayed.AftMkt,
          Dateserial(Year(Date_Stamp), Month(Date_Stamp), 1) AS DT
   FROM Sprayed
   GROUP BY Part,
            Process,
            AftMkt,
            Dateserial(Year(Date_Stamp), Month(Date_Stamp), 1)) AS G
GROUP BY G.Part,
         G.Process,
         G.AftMkt,
         G.DT
HAVING (((Sum([G].[QPass2])+Sum([G].[QFail2])+Sum([SYtd]))>0));

2 个答案:

答案 0 :(得分:0)

DT的公式替换为:IIf(Day(Date_Stamp) > 22, DateSerial(Year(Date_Stamp), Month(Date_Stamp)+1, 1), DateSerial(Year(Date_Stamp), Month(Date_Stamp), 1))

答案 1 :(得分:0)

您可以 Date_Stamp 偏移一周:

Dateserial(Year(DateAdd("ww", 1, Date_Stamp)), Month(DateAdd("ww", 1, Date_Stamp)), 1) AS DT