Brightlane's financial reporting system records both the fiscal quarter and the submission hour for each transaction.
Write a query to return the quarter number and the hour-of-day for the timestamp '2024-08-22 14:30:00' in a single row.
Output:
- A single row with columns
quarter_numberandhour_of_day, each containing a number.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
EXTRACT(
quarter
FROM
'2024-08-22 14:30:00'::TIMESTAMP
) AS quarter_number,
EXTRACT(
HOUR
FROM
'2024-08-22 14:30:00'::TIMESTAMP
) AS hour_of_day The shape
EXTRACT pulls one numeric component at a time, so producing two components from the same datetime means writing two extractions side by side. The August 22 timestamp yields 3 for the quarter and 14 for the hour. The financial reporting system gets both dimensions of the transaction in a single row.
Clause by clause
SELECT EXTRACT(quarter FROM '2024-08-22 14:30:00'::timestamp) AS quarter_numberreturns the fiscal quarter the timestamp falls in. PostgreSQL maps months 1–3 to quarter1, months 4–6 to quarter2, and so on, so August (month8) lands in quarter3.EXTRACT(hour FROM '2024-08-22 14:30:00'::timestamp) AS hour_of_dayreturns the hour portion of the clock time. The minutes and seconds are discarded by the extraction; only the hour component,14, comes through.- There is no
FROMbecause both expressions evaluate against the same literal directly.
The trap
The quarter index is fixed to the calendar year, not to a custom fiscal year. EXTRACT(quarter FROM ...) always treats January through March as quarter 1. An organization whose fiscal year starts in any month other than January will need to derive its quarter index from the month number with arithmetic; the quarter field will silently report the wrong quarter for every row otherwise. The standard quarter the function returns is correct only when the reporting calendar matches the Gregorian calendar.
You practiced two EXTRACT calls in one statement — pull two different numeric components from the same source datetime.