Learn SQL/Intermediate/Date & Time/SQL DATE_TRUNC by Month

SQL DATE_TRUNC by Month

Rounds down to the 1st of the current month — probably the single most common DATE_TRUNC precision in real reporting.

What & Why

DATE_TRUNC('month', date_column) lands on the 1st day of whatever month the date falls in. "Monthly active users," "revenue by month," and similar recurring business reports almost always group by exactly this expression.

See How It Works

BUSINESS QUESTION

Product wants monthly feedback totals.

iduser_idfeature_idratingnps_scorecommentcreated_at
1101159Clear and useful2024-01-25 12:15:00+00
210224NULLExport worked well2024-02-20 15:40:00+00
3103147Helpful setup flow2024-03-18 09:05:00+00
410433NULLNeeds clearer timing2024-04-08 17:30:00+00
EXAMPLE QUERY
SELECT
  DATE_TRUNC('month', created_at)::date AS feedback_month,
  COUNT(*) AS feedback_count
FROM product.feedback
GROUP BY feedback_month
ORDER BY feedback_month;
RESULT — exact output from the displayed Queryflo rows
feedback_monthfeedback_count
2024-01-011
2024-02-011
2024-03-011
2024-04-011

Each feedback row truncates to its actual month start.

Now You Try

Practice this concept

Product wants monthly feedback totals.

Available schema
product

Prefix tables with product.table_name.

feedback_monthfeedback_count
product.feedback
ColumnType
idinteger
user_idinteger
feature_idinteger
ratinginteger
nps_scoreinteger
commenttext
created_attimestamp with time zone
moderation_buckettext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario