组日期双周(Group Date Bi

编程入门 行业动态 更新时间:2024-10-21 12:01:24
组日期双周(Group Date Bi-Weekly)

我试图在我的平板电脑组中每两周总结subtotals 。 关于如何过滤最后两周间隔有很多答案,但到目前为止还没有关于如何正确进行分组的有效解决方案。

表列: order_date , subtotal

我每周都有成功的分组,但不知道如何每两周进行一次

SELECT CONCAT(YEAR(order_date), '/', WEEK(order_date)) AS week_name, SUM(subtotal) FROM m_orders

I am trying to sum subtotals in my tablet group bi-weekly. There are a lot of answers on how to filter last bi-weekly interval, but so far no working solution on how to properly do the grouping.

Table Columns: order_date, subtotal

I've had success with grouping weekly, but no idea how to do it biweekly

SELECT CONCAT(YEAR(order_date), '/', WEEK(order_date)) AS week_name, SUM(subtotal) FROM m_orders

最满意答案

group by WEEK(order_date) DIV 2将group by WEEK(order_date) DIV 2追加到您的查询中:

SELECT CONCAT(YEAR(order_date), '/', WEEK(order_date) DIV 2) AS fortnight_number, SUM(subtotal) FROM m_orders group by YEAR(order_date), WEEK(order_date) DIV 2

在年初和年末有一些问题,MySql的week函数的不同参数决定了返回的范围(0-53或1-53)以及如何确定一年中的第一周,但这是基本想法。

(根据业务需求,您可能需要week(order_date, 1) / 2第二个参数1指定周从星期一开始,范围从0到53,一年的第一周是第一周,有三个那一年或者更多的日子。当然,请与业务部门联系。)

编辑:更正/到DIV

Append group by WEEK(order_date) DIV 2 to your query:

SELECT CONCAT(YEAR(order_date), '/', WEEK(order_date) DIV 2) AS fortnight_number, SUM(subtotal) FROM m_orders group by YEAR(order_date), WEEK(order_date) DIV 2

There are some issues around start of year and end of year, and the different arguments to MySql's week function determine both the range returned (0-53 or 1-53) and how the first week of the year is determined, but this is the basic idea.

(Depending on business requirements, you probably want week(order_date, 1) / 2. The second argument, 1, specifies that weeks start on Mondays, range from 0-53, and the first week of the year is the first week with three or more days in that year. But check with the business, of course.)

Edit: corrected / to DIV

更多推荐

本文发布于:2023-08-04 22:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:双周   日期   Group   Date   Bi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!