SQL query for Calculating Total No. of Orders per Day?

前端 未结 5 1471
自闭症患者
自闭症患者 2021-01-13 01:13

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            


        
相关标签:
5条回答
  • 2021-01-13 01:23

    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)
    
    0 讨论(0)
  • 2021-01-13 01:25

    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.

    0 讨论(0)
  • 2021-01-13 01:32
    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.

    0 讨论(0)
  • 2021-01-13 01:35

    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)
    
    0 讨论(0)
  • 2021-01-13 01:46
    select YEAR(datename(year,ORDER_DATE)) as YEARS,
           count(ORDER_nO) as [NO(ORDER)]
           from ProductOrder
           group by YEAR(datename(year,ORDER_DATE))
    
    0 讨论(0)
提交回复
热议问题