MYSQL 查询按自定义月份日期分组?

发布于 2024-12-23 04:17:38 字数 487 浏览 1 评论 0原文

我编写了一个返回月销售额的查询。

SELECT
count(O.orderid) as Number_of_Orders,
concat (MonthName(FROM_UNIXTIME(O.`date`)),' - ',year(FROM_UNIXTIME(O.date))) as     Ordered_Month,
sum(O.total) as TotalAmount,
Month(FROM_UNIXTIME(O.`date`)) as Month_of_Year,
year(FROM_UNIXTIME(O.date)) as Sale_Year
FROM orders O
group by Month_of_Year, Sale_Year
order by Sale_Year DESC,Month_of_Year DESC

我想将其分组为自定义日期,例如,

而不是 1 日到 1 日,它应该分组为每个月的 10 日 -10 日。 不知道如何以这种方式分组!

I wrote a query that returns monthly sales.

SELECT
count(O.orderid) as Number_of_Orders,
concat (MonthName(FROM_UNIXTIME(O.`date`)),' - ',year(FROM_UNIXTIME(O.date))) as     Ordered_Month,
sum(O.total) as TotalAmount,
Month(FROM_UNIXTIME(O.`date`)) as Month_of_Year,
year(FROM_UNIXTIME(O.date)) as Sale_Year
FROM orders O
group by Month_of_Year, Sale_Year
order by Sale_Year DESC,Month_of_Year DESC

I would like to make it group for a custom date like

instead of 1st to 1st, it should group for 10th -10th of every month.
Not sure how to group it that way!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵山崖 2024-12-30 04:17:38

因为你正在处理时间“转变”,所以你必须在方程中进行数学计算才能“伪造它”。所以

SELECT
      count(O.orderid) as Number_of_Orders,
      concat(
         MonthName( Date_Sub( FROM_UNIXTIME(O.`date`), INTERVAL 10 DAY )),
         ' - ',
         Year( Date_Sub( FROM_UNIXTIME(O.date), INTERVAL 10 DAY) )
            ) as Ordered_Month,
      sum(O.total) as TotalAmount,
      Month( Date_Sub( FROM_UNIXTIME(O.`date`), INTERVAL 10 DAY )) as Month_of_Year,
      Year( Date_Sub( FROM_UNIXTIME(O.date), INTERVAL 10 DAY )) as Sale_Year
   FROM 
      orders O
   group by 
      Month_of_Year, 
      Sale_Year
   order by 
      Sale_Year DESC,
      Month_of_Year DESC

,本质上,您要获取日期,例如:3 月 11 日至 31 日 + 4 月 1 日至 10 日,并从中减去“10 天”...因此对于查询,它们将类似于 3 月 1 日至 31 日和 4 月11-30 会出现在每年的其余时间,如 4 月 1-20 日 + 5 月,依此类推......

because you are dealing with a time "shift", you'll have to do that math in your equation to "fake it out". Something like

SELECT
      count(O.orderid) as Number_of_Orders,
      concat(
         MonthName( Date_Sub( FROM_UNIXTIME(O.`date`), INTERVAL 10 DAY )),
         ' - ',
         Year( Date_Sub( FROM_UNIXTIME(O.date), INTERVAL 10 DAY) )
            ) as Ordered_Month,
      sum(O.total) as TotalAmount,
      Month( Date_Sub( FROM_UNIXTIME(O.`date`), INTERVAL 10 DAY )) as Month_of_Year,
      Year( Date_Sub( FROM_UNIXTIME(O.date), INTERVAL 10 DAY )) as Sale_Year
   FROM 
      orders O
   group by 
      Month_of_Year, 
      Sale_Year
   order by 
      Sale_Year DESC,
      Month_of_Year DESC

So, in essence, you are taking the dates ex: March 11-31 + April 1-10 and subtracting "10 days" from them... so for the query, they will look like March 1-31, and April 11-30 will appear like April 1-20 + May, etc for rest of each year...

悟红尘 2024-12-30 04:17:38

未测试。

group by Month_of_Year, ceil(day(o.`date`)/10), Sale_Year

这是一个更好的主意,可以避免有 4 个组,但只有 3 个组,

select 
month(my_date) as your_month,
year(my_date) as your_year,
case 
when day(my_date) <= 10 then 1
when day(my_date) between 11 and 20 then 2
else 3 end as decade,
count(*) as total
from table
group by 
your_month,your_year,decade

请根据您的需求进行调整。

Not tested.

group by Month_of_Year, ceil(day(o.`date`)/10), Sale_Year

This is a better idea in order to avoid having 4 groups but just 3

select 
month(my_date) as your_month,
year(my_date) as your_year,
case 
when day(my_date) <= 10 then 1
when day(my_date) between 11 and 20 then 2
else 3 end as decade,
count(*) as total
from table
group by 
your_month,your_year,decade

Adapt it to your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文