0

Can anyone post a SQL query for Calculating the Total Number of Orders that was submitted per Day for the last 7 days?

ORDER_DETAIL_ID - DESCRIPTION: A unique identifier for an Order Detail. It is populated from an Oracle sequence.
CREATE_TS   - DESCRIPTION: Contains the date and time the row was created.

I was trying to use this one but not really sure if that would be correct

SELECT date(CREATE_TS)
     , COUNT(ORDER_DETAIL_ID) AS num_orders
     , SUM(order_total) AS daily_total
  FROM vestaadm.order_detail
 GROUP BY date(CREATE_TS)

any sugestions woulf be graetly appreciated.

MT0
  • 143,790
  • 11
  • 59
  • 117
  • Retrieving data for the last X days is a common question. Did you search the archives? Example: [MySQL Select last 7 days](https://stackoverflow.com/questions/24272335/mysql-select-last-7-days) If you ran into problems, please update your post with some sample data, and an explanation of what was wrong with the results. – SOS Apr 11 '22 at 02:34
  • Are you using MySQL or Oracle? – jarlh Apr 11 '22 at 06:47

1 Answers1

0

Supposing the last 7 days exclude today, that could be interpreted as between date_add(current_date(),interval -1 day) and date_add(current_date() ,interval -7 day) :

SELECT date(CREATE_TS) , COUNT(ORDER_DETAIL_ID) AS num_orders , SUM(order_total) AS daily_total 
FROM vestaadm.order_detail WHERE date(CREATE_TS) 
BETWEEN date_add(current_date(),interval -1 day) AND date_add(current_date() ,interval -7 day)
GROUP BY date(CREATE_TS);
blabla_bingo
  • 1,825
  • 1
  • 2
  • 5