gpt4 book ai didi

sql - 如何按指定周末结束的一周分组?

转载 作者:可可西里 更新时间:2023-11-01 08:08:11 27 4
gpt4 key购买 nike

我需要运行按周分组的报告。这可以通过使用 group by week(date) 来完成,但客户想要设置标志着一周结束的星期几。所以它可以是星期二、星期三等。我如何通过查询将其分组?

日期时间列类型为unix时间戳。

最佳答案

WEEK()函数采用可选的第二个参数来指定一周​​的开始:

This function returns the week number for date. The two-argument form of WEEK() enables you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53.

但只能设置为星期日或星期一。


更新:除了下面的评论之外,您可能需要考虑在表中添加一个新列作为分组字段,基于 WEEK(DATE_ADD(date INTERVAL x DAY )),如评论中所建议的那样。您可能想要创建 triggers每当更新 date 字段以及插入新行时自动生成此值。然后,您就可以根据需要在这个新字段上创建一个可用的索引。

关于sql - 如何按指定周末结束的一周分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3757308/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com