Group by data by month with selected fields not to be included in group by class Is there any way to group by the data by the only month but I need the no_inv and date_inv columns to be selected too.
Mysql query :
SELECT no_inv, date_inv, SUM(total_unpaid) AS unpaid, SUM(total_paid) AS paid
FROM invoice
GROUP BY MONTH(date_inv)
LIMIT 3
It doesn't make sense (even a as concept) to be able to SELECT non-aggregated fields that aren't part of the GROUP BY clause, because when you group on a field, you're literally condensing many rows into one row for each unique value of that field you're grouping on. So it wouldn't be possible to display a non-grouped (and non-aggregated) field because that field may have multiple different values within the grouping of the field in the GROUP BY clause, so it would not be possible to show two different values of the non-grouped field in the same row for the grouped field.
That being said, I think I understand the reasoning behind your question, which I assume is you're more so looking for a way to aggregate across a grouping and display that aggregation with the other non-grouped fields. This is possible to accomplish with window functions via the OVER and PARTITION BY clause like this:
SELECT no_inv, date_inv, SUM(total_unpaid) OVER (PARTITION BY MONTH(date_inv)) AS unpaid, 
    SUM(total_paid) OVER (PARTITION BY MONTH(date_inv)) AS paid
FROM invoice
LIMIT 3

Please note the OVER() clause (and  window functions) was introduced in MySQL 8.0.