简体   繁体   English

Mysql select 上周每天的总和

[英]Mysql select last week sum per day

I have a query in my report dashboard that shows me total of last 7 days income in one number.我的报告仪表板中有一个查询,显示我过去 7 天的总收入在一个数字中。

Now I'm trying to get this number on a daily chart, for example:现在我试图在日线图上得到这个数字,例如:

April 7: 100,000 (total income from april 1 to 7)
April 8: 110,000 (total income from april 2 to 8)
...

How can I select like this?我怎么能像这样 select ?

Purchases table structure:采购表结构:

Value : int
Date : date

You can use window functions.您可以使用 window 函数。 If you have one row per day, this would often be used:如果你每天有一行,这通常会被使用:

select t.*,
       sum(income) over (order by date rows between 6 preceding and current row)
from t;

This adds up 7 rows, so if there are missing or duplicate dates it is not correct.这加起来有 7 行,因此如果日期缺失或重复,则不正确。

However, range really does limit to the dates you want:但是, range确实限制了您想要的日期:

select t.*,
       sum(income) over (order by date range between interval 6 day preceding and current row)
from t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM