There are two main problems with your query:
- You are grouping by the numbers 1 and 2. You may think that you are grouping by the columns at the positions 1 and 2, but this is not the case. There is no positional
GROUP BY
in Oracle.
- You are invoking a window function on
order_date
, but this column is not in your intermediate results. It is neither grouped by nor aggregated. You probably want TO_CHAR(order_date, 'YYYY-MM')
instead.
Another problem is a semantical one:
WHERE TO_CHAR(order_date, 'YYYY-MM') BETWEEN ... AND TO_CHAR(order_date, 'YYYY-MM')
This is true for every row, because each order date matches itself (except for NULL, but I guess there are no orders without an order date in your table). Maybe you want to compare with the current date? Are you trying to read all orders ordered last month or this month? But then, why BETWEEN
? It shouldn't be possible to find any orders made in the future. The newest order in the table can maximally be of today.
The corrected query:
SELECT
TO_CHAR(order_date, 'YYYY-MM'),
product_id,
SUM(quantity) AS sum_quan,
ROW_NUMBER() OVER (ORDER BY TO_CHAR(order_date, 'YYYY-MM')) AS rn
FROM customer_orders
WHERE 1 = 1
AND product_id = 2
AND order_date >= ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1)
GROUP BY TO_CHAR(order_date, 'YYYY-MM'), product_id
ORDER BY TO_CHAR(order_date, 'YYYY-MM'), product_id;