mysql 计算每年拥有的条目数每周

tf7tbtn2  于 2022-12-10  发布在  Mysql
关注(0)|答案(1)|浏览(122)

通过这个查询,我得到了我拥有的所有条目

select
  SUM(quantity) AS sumQuantity
from
  data

现在我想知道我每周有多少钱。我试过了,但我当然只能得到我每周添加/删除的量。我能用SQL读出来吗,或者我需要一些php?

select
  SUM(quantity) AS sumQuantity,
  CONCAT(
    YEAR(entry_date),
    WEEK(entry_date)
  ) AS yearWeek
from
  data
group by
  yearWeek
order by
  yearWeek desc
8e2ybdfx

8e2ybdfx1#

您当前的查询提供了每年每周的总数量。我怀疑您想要一个运算符;在MySQL8.0中,我们可以像这样使用sum() over()实现这一点:

select
    sum(quantity) AS sumQuantity,
    sum(sum(quantity)) over(order by yearweek(entry_date)) AS runningQuantity
    yearweek(entry_date) AS yearWeek
from data
group by yearWeek
order by yearWeek desc

附注:您可以使用内置函数yearweek()来计算年/周。

相关问题