MS Access 查询,如何使用 SQL 将单个日期分组为周

2024-04-21

我目前有两张桌子。其中有员工姓名和与该姓名相关的号码。

另一个日期有时间表日期,其中包含员工编号、日期和该日期的工作小时数列。

我想创建一个交叉表查询,该查询在一列中显示员工姓名,在每列中显示周末的日期,然后显示特定员工该周的小时数总和。

我当前的查询有效,但只能按月分组。我很难弄清楚如何将几天分组为几周。

TRANSFORM Sum(tblTimeSheetData.WorkHours) AS SumOfHours
SELECT tblEmployees.Combined
FROM tblTimeSheetData RIGHT JOIN tblEmployees ON tblTimeSheetData.EmployeeID = 
tblEmployees.EmployeeID
GROUP BY tblEmployees.Combined
ORDER BY tblEmployees.Combined, Format([WorkDate],"yyyy-mm")
PIVOT Format([WorkDate],"yyyy-mm");

由于第一周和最后一周的数字跨日历年,因此必须包括年份和周数:

Option Compare Database
Option Explicit

    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, for a date value, a formatted string expression with
' year and weeknumber according to ISO-8601.
' Optionally, a W is used as separator between the year and week parts.
'
' Typical usage:
'
'   FormatWeekIso8601(Date)
'   ->  2017-23
'
'   FormatWeekIso8601(Date, True)
'   ->  2017W23
'
' 2017-04-28. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function FormatWeekIso8601( _
    ByVal Expression As Variant, _
    Optional ByVal WeekSeparator As Boolean) _
    As String

    Const Iso8601Separator  As String = "W"
    Const NeutralSeparator  As String = "-"

    Dim Result              As String

    Dim IsoYear As Integer
    Dim IsoWeek As Integer

    If IsDate(Expression) Then
        IsoWeek = Week(DateValue(Expression), IsoYear)
        Result = _
            VBA.Format(IsoYear, String(3, "0")) & _
            IIf(WeekSeparator, Iso8601Separator, NeutralSeparator) & _
            VBA.Format(IsoWeek, String(2, "0"))
    End If

    FormatWeekIso8601 = Result

End Function

' 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

如果您只想以 ultimo 周日期为基准,可以使用以下表达式:

DateAdd("d", 7 - Weekday([WorkDate], vbMonday), [WorkDate])

这假设采用 ISO 周编号,其中星期一是一周的第一天。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MS Access 查询,如何使用 SQL 将单个日期分组为周 的相关文章

  • MS Access 表单按钮,允许用户浏览/选择文件,然后将文件导入到表中

    在我的数据库中 我可以使用以下命令创建命令按钮导入文件 DoCmd TransferText acImportDelim 导入的原始数据 导入规范 导入的原始数据 D Users Denise Griffith Documents Grif
  • PostgreSQL Age() 函数:在不同月份登陆时出现不同/意外的结果

    今天 我在 PostgreSQL 9 6 中运行此查询时遇到了无法解释的结果 SELECT age 2018 06 30 2018 05 19 AS one age 2018 07 01 2018 05 20 AS two 两列的预期结果
  • 为什么此 SQL 更新失败(“列名无效”)?

    我有一个 SQL Server CE 表 如下所示 我正在尝试像这样更新其唯一记录 update workTables set fileType INV 但我得到 Why UPDATE 请查看相关问题here https stackover
  • 通过货币换算获取每种产品类型的最低价格

    我想选择每种产品类型中最便宜的 包括运费 价格转换为当地货币 最便宜 产品 价格 产品 运费 seller to aud 我的数据库有如下表 PRODUCTS SELLERS id type id seller id price shipp
  • SQL SERVER 中的排序依据和大小写

    我需要在存储过程中按功能排序 一个值被发布到网络服务 并且基于该值我必须以某种方式对结果进行排序 即 当 ColName 按 ColName 发布订单时 当 ColName2 由 ColName2 发布订单时 我正在研究使用 Case 但出
  • SqlCommand 参数与 String.Format [重复]

    这个问题在这里已经有答案了 我一直在互联网上搜索 但似乎找不到任何可以解释我的问题的内容 可能是我没有使用正确的搜索字符串 所以我在这里发帖希望有人可以帮助我有了这个 我的程序是使用Visual Studio 2010用C 编写的 我注意到
  • 如何在sqlite中创建物化视图?

    我对物化视图和 SQLite 进行了无数次搜索 据我所知 2004 年和 2006 年似乎有人提到 SQLite 没有物化视图 紧随其后的是 SQLite 的变更日志2008年3月 http www sqlite org releaselo
  • 使用 WHILE 创建虚拟数据

    我尝试使用 a 在表中插入一些虚拟数据WHILE 但它运行得非常非常慢 我在想也许我写的代码不正确 你能看一下并确认一下吗 Insert dummy data DECLARE i int Content int SET i 5001 WHI
  • 使用按位函数查询 BIT 字段时,MySQL 不使用索引

    我的 MySQL 表中有一个 BIT 类型的字段 我想使用位值存储记录的状态 例如 1 status1 2 status2 4 status3 8 status4 每条记录可以同时具有多种状态 对于 status1 和 status3 该值
  • 如何以最少的查询次数获取帖子列表和关联标签

    我的表格结构如下 标签 更多的是一个类别 id 标签名称 描述 slug POSTS ID 标题 网址 邮戳 id idPost idTag USERS ID 用户名 userSlug VOTES id idPost idUser 每个帖子
  • 限制 SQL Server 连接到特定 IP 地址

    我想将 SQL Server 实例的连接限制为特定 IP 地址 我想阻止来自除特定列表之外的任何 IP 地址的任何连接 这是可以在 SQL Server 实例或数据库中配置的东西吗 听起来像是你会使用Windows防火墙 http tech
  • TOAD 将 &String 视为绑定变量

    我正在使用 Oracle Data Integrator 开发一些 ETL 有时会使用 TOAD 测试部分代码 今天我遇到了 TOAD 的问题 我有一行像 AND column value like DEV PROD 当我尝试运行包含上面过
  • 如何在 sqlalchemy 中创建基于文字的查询?

    我创建了一个函数来创建表达式 def test operator1 operation operator2 return literal column operator1 op operation operator2 现在当我用 test
  • 如何有效地从 DB2 表中删除所有行

    我有一个大约有 50 万行的表 我想删除所有行 如果我做简单的delete from tbl 事务日志已满 我不关心这种情况下的事务 无论如何我都不想回滚 我可以删除许多事务中的行 但是有更好的方法吗 如何有效地从 DB2 中的表中删除所有
  • 获取家庭成员

    假设以下家庭 其构建架构是 create table PersonConn child int parent int insert into PersonConn values 1 2 insert into PersonConn valu
  • SPARK SQL - 当时的情况

    我是 SPARK SQL 的新手 SPARK SQL 中是否有相当于 CASE WHEN CONDITION THEN 0 ELSE 1 END 的内容 select case when 1 1 then 1 else 0 end from
  • 使用两个日期之间的随机日期时间更新每一行

    我有一个专栏叫date created我希望每一行保存一个随机日期 日期距当前时间为 2 天 我正在运行以下查询 但它会更新具有相同随机日期的所有行 我希望每一行都是随机的并且不相同 update table set date create
  • 如何在 SQL Server 中使用 nvarchar 变量为 unicode 用户添加前缀“N”?

    如何在 SQL Server 中使用 nvarchar 变量为 unicode 用户添加前缀 N 例如 给定这个变量 declare Query1 nvarchar max 我可以这样分配它 set Query1 N 但是如果我想使用怎么办
  • 分层查询

    我希望我能够解释困扰我的问题 我有以下分层数据集 这只是 34K 记录的子集 PARENT ID CHILD ID EXAM TUDA12802 TUDA12982 N TUDA12982 TUDA12984 J TUDA12984 TUD
  • SQL 删除表并重新创建并保留数据

    在我们最初的设计中 我们搞砸了表中的外键约束 现在表已充满数据 我们无法在不删除表中所有记录的情况下更改它 我能想到的唯一解决方案是创建一个备份表并将所有记录放在那里 然后删除所有记录 更改表并开始将它们添加回来 还有其他 更好 的想法吗

随机推荐