query between two dates in MySQL grouped by range of days

Chique_Code :

I am using DOMO with SQL. I have two columns in my dataset: Date and Revenue. I only want to grab the data starting from March 1st, 2019 and group it weekly, so I only see the sum of Revenue for 1 week from March 1st, 2019 and going forward.

I want the output be something like this:

Date                      Revenue
March 1st - 7th, 2019        123 
March 8th - 15th, 2019       124

I have tried the following:

select '2019-03-01' AS `Date`,
ADDDATE('2019-03-01',INTERVAL 1 WEEK) AS '+1 Week'

But it only returns one date '2019-03-08'

ChocoLite :
SELECT WeekNumber, SUM(Revenue) 
FROM (SELECT DATEDIFF(week, '2019-03-01', Date) AS WeekNumber, Revenue FROM Table)
GROUP BY WeekNumber;

This SQL command should give you week-wise revenue from your database.

DATEDIFF( date_part , start_date , end_date)

The DATEDIFF() function returns a value of integer indicating the difference between the start_date and end_date, with the unit specified by date_part. In our command, it gives us the week number for the date in each row.

From the inner SQL function,

SELECT DATEDIFF(week, '2019-03-01', Date) AS WeekNumber, Revenue FROM Table

we obtain a new table which has the week number and revenue for each row in the original table. Now, we group them according to the week number and sum up the revenue column.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360281&siteId=1