SQL Year-to-Date

The business-reporting name for 'current year' — often abbreviated YTD, comparing this year's progress to the same point in a prior year.

What & Why

Year to date means January 1 through the reporting cutoff. The worked Queryflo table fixes the cutoff at April 1, 2024, so it contains the January, February, and March leads.

See How It Works

BUSINESS QUESTION

Using 2024-03-31 as the report date, Marketing wants leads created since the start of that year.

idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
EXAMPLE QUERY
SELECT
  id AS lead_id,
  source,
  created_at
FROM marketing.leads
WHERE created_at >= TIMESTAMPTZ '2024-01-01 00:00:00+00'
  AND created_at < TIMESTAMPTZ '2024-04-01 00:00:00+00'
ORDER BY created_at, lead_id;
RESULT — exact output from the displayed Queryflo rows
lead_idsourcecreated_at
301google_ads2024-01-21 09:10:00+00
302google_ads2024-01-24 12:40:00+00
303email2024-02-16 08:30:00+00
304linkedin2024-03-20 17:15:00+00

The fixed January 1–April 1 boundaries contain all four displayed leads.

Now You Try

Practice this concept

Return leads in the fixed 2024 year-to-date window from January 1 through April 1, ordered by creation time and lead id.

Available schema
marketing

Prefix tables with marketing.table_name.

lead_idsourcecreated_at
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario