查询工作年之间的记录

问题描述 投票:0回答:2

[业务从每年的10月1日开始。我需要两个查询,一个查询返回当前营业年度的所有记录,另一个查询返回上一个营业年度的所有记录。在这种情况下,year年并不重要。

我写过:

strSql1 = "SELECT * FROM Orders WHERE DateDiff('d', 
[OrderDate], [YearStart])<=365 ORDER BY [OrderDate] ASC"

Me.frmCurrentYear.Form.RecordSource = strSql1

strSql2 = "SELECT * FROM Orders WHERE DateDiff('d', [OrderDate], 
[YearStart])>365 AND DateDiff('d', [PurchaseDate],[YearStart]<=730) ORDER BY 
[OrderDate] ASC"

Me.frmPreviousYear.Form.RecordSource = strSql2 

问题:

  1. 从我的代码中,我将不得不向表中添加YearStart字段。有办法避免这种情况吗?

  2. 使用BETWEEN会更有效吗?

谢谢。

sql ms-access access-vba
2个回答
0
投票

1-您总是可以计算出例如月份是oct,nov或dec,然后是当前年份,否则是上一年。

case when Month(GetDate()) >=10 then Cast('10-01'+Cast(Year(GetDate() as Varchar(5)) as date)
     when Month(GetDate()) <10 then Cast('10-01'+Cast(Year(GetDate())-1 as varcher(5)) as date) 
END as YearStart

2- SQL : BETWEEN vs <= and >=


0
投票

DateSerial用于此类任务:

strSql1 = "SELECT * FROM Orders " & _
"WHERE DateDiff('m', [OrderDate], DateSerial(Year(Date()), 1, 1) Between -3 And 9 " & _
"ORDER BY [OrderDate] ASC"
strSql2 = "SELECT * FROM Orders " & _
"WHERE DateDiff('m', [OrderDate], DateSerial(Year(Date()) - 1, 1, 1) Between -3 And 9 " & _
"ORDER BY [OrderDate] ASC"
© www.soinside.com 2019 - 2024. All rights reserved.