简体   繁体   中英

How to get the MIN and MAX date from each month

I have a data in mysql that look like this in my first table.

ID |     date_created        | cost  |  project_id
1   2019-09-01 00:00:00         10          1
2   2019-09-02 00:00:00         20          1
3   2019-09-03 00:00:00         30          1
4   2019-09-04 00:00:00         40          1
5   2019-09-05 00:00:00         50          1
6   2019-10-01 00:00:00         20          5
7   2019-10-02 00:00:00         40          5
8   2019-10-03 00:00:00         60          5
9   2019-10-04 00:00:00         70          5
10  2019-10-12 00:00:00         80          5

Here is my second table

ID |     date_created        | usage  |  project_id
1   2019-09-01 00:00:00         10          1
2   2019-09-02 00:00:00         20          1
3   2019-09-03 00:00:00         30          1
4   2019-09-04 00:00:00         40          1
5   2019-09-05 00:00:00         50          1
6   2019-10-01 00:00:00         20          5
7   2019-10-02 00:00:00         40          5
8   2019-10-03 00:00:00         60          5
9   2019-10-04 00:00:00         70          5
10  2019-10-12 00:00:00         80          5

the results that i am trying to achieved is like this.

ID | date_created             | cost(SUM) | total_usage (SUM)

1    2019-09-01 - 2019-09-05     150      |  150

2    2019-10-01 - 2019-10-12     260      |  260

this is my query. i can already get the sum of the cost but when i put the the LEFT JOIN to get the sum of the usage, instead of having 2 rows only. im getting wrong values and having multiple rows.

SELECT MONTHNAME(date_created), SUM(cost), SUM(usage)
FROM first_table as first_table
LEFT JOIN second_table as second_table ON first_table.project_id = second_table.project_id
GROUP BY MONTH(first_table.date_created), YEAR(first_table.date_created)

i need help in getting the sum of the cost and also the usage. is there any way to do this, or my way is just wrong.

You should never consider months without also considering the year. Also, the unaggregated columns in the SELECT should match the columns in the GROUP BY . Most databases enforce this, including the most recent versions of MySQL.

The answer to your question is to just include the additional columns you want in the SELECT :

SELECT YEAR(date_created), MONTHNAME(date_created), SUM(cost), MIN(cost), MAX(cost)
FROM table_name
GROUP BY YEAR(date_created), MONTH(date_created), MONTHNAME(date_created);

This should return the results that you expect. It's a simple aggregate query where results are grouped by year and month (grouping on month only would cause issues if your data spans over multiple years):

SELECT 
    CONCAT(MIN(date_created), ' - ', MAX(date_created)) date_interval, 
    SUM(cost) total_cost
FROM mytable
GROUP BY YEAR(date_created), MONTH(date_created)

Demo on DB Fiddle :

| date_interval           | total_cost |
| ----------------------- | ---------- |
| 2019-09-01 - 2019-09-05 | 150        |
| 2019-10-01 - 2019-10-12 | 270        |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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