242

Having a table with a column like: mydate DATETIME ...

I have a query such as:

SELECT SUM(foo), mydate FROM a_table GROUP BY a_table.mydate;

This will group by the full datetime, including hours and minutes. I wish to make the group by, only by the date YYYY/MM/DD not by the YYYY/MM/DD/HH/mm.

How to do this?

1
  • 1
    A better approach described in this answer! Commented Jun 19, 2015 at 8:51

5 Answers 5

379

Cast the datetime to a date, then GROUP BY using this syntax:

SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table.mydate);

Or you can GROUP BY the alias as @orlandu63 suggested:

SELECT SUM(foo), DATE(mydate) DateOnly FROM a_table GROUP BY DateOnly;

Though I don't think it'll make any difference to performance, it is a little clearer.

Sign up to request clarification or add additional context in comments.

5 Comments

Are you sure the second one works? On SQL Server, this fails, and it fails for a good reason. I would expect it to fail anywhere else as well. Can you confirm that MySQL actually handles this query?
I just tried it and it works fine. MySQL is more permissive about GROUP BY and it trusts you to write a query that is not ambiguous.
Thanks for the info. I can't decide if this is a good thing or not, but it fits nicely into my opinion about MySQL. From a technical POV - how is this supposed to work? I only can imagine that the query parser substitutes the alias in the GROUP BY clause with the actual expression.
It's a performance trap! Keep in mind that using such a function - DATE() does not allow you to leverage indexes on this column.
The second one can also be shortened to SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY 2;
33

I found that I needed to group by the month and year so neither of the above worked for me. Instead I used date_format

SELECT date
FROM blog 
GROUP BY DATE_FORMAT(date, "%m-%y")
ORDER BY YEAR(date) DESC, MONTH(date) DESC 

Comments

21

Or:

SELECT SUM(foo), DATE(mydate) mydate FROM a_table GROUP BY mydate;

More efficient (I think.) Because you don't have to cast mydate twice per row.

3 Comments

I would be very surprised if MySQL ran the conversion twice. Only aggregate functions and expressions in the group by list are allowed in group by select statements. The engine already has to know that the two expressions are the same.
@Tmdean, ' Only aggregate functions and expressions in the group by list are allowed in group by select statements ' - can you explain it in easier words ?
@IstiaqueAhmed Aggregate Functions aggregate Rows (COUNT(), SUM(), AVG(), etc.) Expressions in the GROUP BY list, are the Expressions used to groups the rows (mydate in the example SELECT [...] GROUP BY mydate). All of these (and only these - according to @Tmdean) can be used in the SELECT to be part of the result.
9
SELECT SUM(No), HOUR(dateofissue) 
FROM tablename 
WHERE dateofissue>='2011-07-30' 
GROUP BY HOUR(dateofissue)

It will give the hour by sum from a particular day!

1 Comment

This will give you wrong results if done the query spans more than a day
0

this worked for me

select 
  CONVERT(date, CONVERT(VARCHAR(10),sd.Date,112)) as Date, 
  sd.CodId as CodId,
  p.Description ,
  sum(sd.Quantity)as Quantity,
  sum(sd.TotalQuantityXPriceWithIva) as TotalWithIva 
from 
  SaleDetails sd 
  join Sales s on sd.SaleId = s.SaleId 
  join Products p on sd.ProductId = p.ProductId 
Where 
  (
    sd.Date >=' 1/1/2021 00:00:00' 
    and sd.Date <= '26/10/2021 23:59:59' 
    and p.BarCode = '7790628000034'
    and ((s.VoucherTypeId >= 16 and s.VoucherTypeId <= 18) 
      or s.VoucherTypeId = 32  )) 
group by 
  CONVERT(VARCHAR(10),sd.Date,112), 
  sd.CodId , 
  p.Description 
order by CONVERT(VARCHAR(10),sd.Date,112) desc

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.