Unit 4, lesson 4 · date_trunc, CAST
Rolling a date back to its month or week
Which month and week did each order fall in?
Reports usually count by month or by week, so each date needs a label for its period. date_trunc('month', x) rolls a date back to the 1st of its month, and date_trunc('week', x) back to the Monday that starts its week. It returns a timestamp, a date with a time, so turn it back into a plain date with ::date, the short form of CAST(x AS date): date_trunc('month', DATE '2026-03-17')::date gives '2026-03-01', and date_trunc('week', DATE '2026-03-19')::date gives '2026-03-16'. In unit 7 you will count orders for each of these labels.
SELECT signup_date,
CAST(date_trunc('month', signup_date) AS date) AS signup_month
FROM customers
LIMIT 5;Your task
For a weekly shipping report, label every order with the status shipped with the first day of its month and the Monday of its week. Return the id, order_date, month_start and week_start.
Tables you will use: orders
Try it in your browserFree, no sign-up. Your query is checked as you go.