access sql select*其中weeknum< currentweek and year< =当前年份

n53p2ov0  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(261)

我肯定我漏掉了一些明显的东西,但我想不出来。我的问题很简单,我有一个包含以下信息的表:
年,周数,数据
我只想选择周数小于当前周数的数据。这很简单。但我真正想要的是在当前周数和当前年份之前注册的所有数据。意思是如果我有以下数据:

Year   Week   Data
2019    20    123
2019    21    234
2020    20    345
2020    21    456

如果本周是2020年第21周,那么我希望输出为:

year   Week   Data
2019    20     123
2019    21     234
2020    20     234

我不想要2020-21,因为那是我当前查询的年份/周:

SELECT T.Year, T.Week, T.Data
FROM tblData AS T
WHERE Year<=year(Date()) AND Week<Weeknumber(Date())

这个查询的问题是,它没有显示2019年超过21周,我希望它这样做。我知道为什么这个sql不起作用,但是我不知道如何正确地执行它。
我该怎么做?

wbgh16ku

wbgh16ku1#

年份是一个保留字,不应该用保留字作为任何东西的名称。我用yr作为字段名。
考虑:

SELECT Yr, Week, Data
FROM Table1
WHERE [Week] Between 1 And IIf([Yr]=Year(Date()), DatePart("ww", Date()) - 1, 53);
91zkwejq

91zkwejq2#

由于您来自丹麦,根据iso 8601,表格很可能会反映年份和周数。但是,vba的任何本机函数都不会返回iso 8601日期的年和周,对于新年前后的日期,年份通常与日历年不同。
因此,需要返回iso 8601年和周的函数:

Public Const MaxWeekValue           As Integer = 53
    Public Const MinWeekValue           As Integer = 1
    Public Const MaxMonthValue          As Integer = 12
    Public Const MinMonthValue          As Integer = 1

' Returns the ISO 8601 week of a date.
' The related ISO year is returned by ref.
'
' 2016-01-06. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Week( _
    ByVal Date1 As Date, _
    Optional ByRef IsoYear As Integer) _
    As Integer

    Dim Month       As Integer
    Dim Interval    As String
    Dim Result      As Integer

    Interval = "ww"

    Month = VBA.Month(Date1)
    ' Initially, set the ISO year to the calendar year.
    IsoYear = VBA.Year(Date1)

    Result = DatePart(Interval, Date1, vbMonday, vbFirstFourDays)
    If Result = MaxWeekValue Then
        If DatePart(Interval, DateAdd(Interval, 1, Date1), vbMonday, vbFirstFourDays) = MinWeekValue Then
            ' OK. The next week is the first week of the following year.
        Else
            ' This is really the first week of the next ISO year.
            ' Correct for DatePart bug.
            Result = MinWeekValue
        End If
    End If

    ' Adjust year where week number belongs to next or previous year.
    If Month = MinMonthValue Then
        If Result >= MaxWeekValue - 1 Then
            ' This is an early date of January belonging to the last week of the previous ISO year.
            IsoYear = IsoYear - 1
        End If
    ElseIf Month = MaxMonthValue Then
        If Result = MinWeekValue Then
            ' This is a late date of December belonging to the first week of the next ISO year.
            IsoYear = IsoYear + 1
        End If
    End If

    ' IsoYear is returned by reference.
    Week = Result

End Function

' Returns the ISO 8601 year of a date.
'
' 2016-01-06. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function YearOfWeek( _
    ByVal Date1 As Date) _
    As Integer

    Dim IsoYear As Integer

    ' Get the ISO 8601 year of Date1.
    Week Date1, IsoYear

    YearOfWeek = IsoYear

End Function

举个例子:

? Week(#2024/12/31#)
 1 

? YearOfWeek(#2024/12/31#)
 2025

现在,在查询中使用这些选项:

SELECT 
    T.Year, 
    T.Week, 
    T.Data
FROM 
    tblData AS T
WHERE 
    T.Year * 100 + T.Week < YearOfWeek(Date()) * 100 + Week(Date())

您可以安全地将year作为字段名,只要您在查询中以表名作为前缀或将其括在括号中: [Year] .

lh80um4z

lh80um4z3#

在进一步测试之后,6月7日非常接近,但sql最终删除了2019年比当前一周多的周数(我不希望这样)。我的问题可能还不够清楚,但这里有一个sql where标准,它给出了我想要的东西。也就是说,我只希望查询从当前年份的当前星期中删除数据。所有比这更古老的东西,我都想展示。

WHERE tbl.Week Between 1 And IIf(tbl.Yr=Year(Date()),Datepart("ww",Date())-1,53)

但是谢谢你6月7日的帮助,因为你直接引导我找到了答案

相关问题