Answer the question
In order to leave comments, you need to log in
How to convert DateTime to Date to group by date in SQL query - DELPHI?
Good afternoon dear experts!
I came across a problem with grouping SQL query results by date.
Table structure:
id - order number;
price - total cost;
date_insert - order date; - DateTime type I form
a request to calculate the average check and the number of orders:
SELECT id, date_insert, SUM(price), AVG(price), COUNT(id)
FROM b_sale_order
WHERE (canceled = 'N') AND (date_insert BETWEEN :StartD AND :EndD)
ORDER BY date_insert
GROUP BY date_insert
Answer the question
In order to leave comments, you need to log in
Actually everything works.
Here is the final query:
SELECT date_format(date_insert, "%Y.%m.%d") as Date, Sum(price), AVG(price) as SrCheck
FROM b_sale_order
WHERE (canceled = 'N') AND (date_insert BETWEEN '2017.01.01' AND '2017.01.31')
GROUP BY Date
ORDER BY Date
The error is that ORDER BY and GROUP BY must go in reverse order.
To group by date:
SELECT DATE(`date_insert`) AS `date`, SUM(`price`), AVG(`price`), COUNT(*)
FROM `b_sale_order`
WHERE (`canceled` = 'N') AND (`date_insert` BETWEEN :StartD AND :EndD)
GROUP BY `date`
ORDER BY `date`
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question