Deviopath › PostgreSQL

Orders Between Two Dates

Hard · Functions

A function can return a whole result set: RETURNS TABLE (col1 type, col2 type). You then use it like a table: SELECT * FROM my_function(...).

Create customer_orders(p_customer_id integer, p_from date, p_to date) that returns TABLE (order_id integer, ordered_on date, amount numeric): the orders of that customer placed from p_from to p_to, both days included (the whole p_to day counts, up to 23:59). Leave out orders with status 'cancelled'. ordered_on is the date part of ordered_at.

Next: Latest Two Orders per Customer