sql - Oracle group orders by date and sum the total -
my orders table looks like:
order_id (number) order_total (number) created_date (timestamp) status (varchar2) my goal set of rows each row represents orders on date, i'm trying group orders date , sum of order_total. i'm limiting results selecting orders last 30 days.
to clarify, example if there 30 orders in last 30 days on unique days 30 rows in result. example: if there 10 orders on 30th july, , 1 order on 31st july i'm aiming 2 rows in result set, order_total summed 10 orders in first row, , second row of course have order_total of single order on 31st.
my attempt far:
select sum(order_total) total_amount, to_char(created_date, 'dd/mm/yyyy') grouped_date orders status = 'complete' , created_date >= (sysdate-30) group to_char(created_date, 'dd'), to_char(created_date, 'mm'), to_char(created_date, 'yyyy') order created_date asc this gives error:
ora-00936: missing expression
i have tried use solution this question don't think quite fits scenario (this group expression has come from).
assuming order_id should not there, , created_date has time component (which seems it's timestamp), need truncate date remove time when doing aggregation:
select sum(order_total) total_amount, to_char(trunc(created_date), 'dd/mm/yyyy') grouped_date orders status = 'complete' , created_date >= trunc(sysdate-30) group trunc(created_date) order trunc(created_date) asc i've applied trunc where clause, otherwise ignore orders 30 days ago between midnight , whatever time ran query today. , i've used trunc'd date directly in order by, rather column alias, order right when go across month-end - ordering dd/mm/yyyy string value put 01/07/2013 before 30/06/2013, example.
quick sql fiddle.
Comments
Post a Comment