Can anyone post a SQL query for Calculating Total No. of Orders per Day?
Here are the Columns along with their data in my Database.
order_id
MySQL's date() function will return a DATEIME or TIMESTAMP value without any hour/minute/second info - which means you reduce the accuracy to the day of the value.
So all you need to do is group by that and then add your aggregate functions to the right columns.
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM [Table]
GROUP BY date(order_placed_date)
A group by is your friend here. It can aggregate on grouped rows. An example query would be:
SELECT order_placed_date, SUM(order_total)
FROM orders
GROUP BY order_placed_date
Of course, in your example you'd probably want to extract just the day/month/year part using the DATE() function, and group by that.
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM orders
GROUP BY 1
Just copied Peter's answer but altered it so it will work. Plus shortcuted the group by.
I don't know what it is in MySQL but in MSSQL it would be this,
select
datepart(yyyy, order_placed_date),
datepart(mm, order_placed_date),
datepart(dd, order_placed_date),
sum(order_total)
from orders
group by datepart(yyyy, order_placed_date), datepart(mm, order_placed_date), datepart (dd, order_placed_date)
select YEAR(datename(year,ORDER_DATE)) as YEARS,
count(ORDER_nO) as [NO(ORDER)]
from ProductOrder
group by YEAR(datename(year,ORDER_DATE))