Using DATE_TRUNC
The DATE_TRUNC() function will truncate timestamp or interval data types to return a timestamp or interval at a specified precision. The precision values are a subset of the field identifiers that can be used with the EXTRACT() and DATE_PART() functions. DATE_TRUNC() will return an interval or timestamp rather than a number. For example
SELECT DATE_TRUNC('month', TIMESTAMP '2005-05-21 15:30:30');
Result: 2005-05-01 00;00:00
Now, let's experiment with different precisions and ultimately modify the queries from the previous exercises to aggregate rental activity.
Questo esercizio fa parte del corso
Functions for Manipulating Data in PostgreSQL
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
-- Truncate rental_date by year
SELECT ___(___, ___) AS rental_year
FROM rental;