Dates and times
Build real dates, group them by month, measure gaps, and notice when a date has been stored as text.
What runs in CodeSplash
- Monotonic trend tests (Mann–Kendall family) Verified in Python and R
You will be able to
- Build a date from parts and format it
- Group by period with date_trunc
- Compute a difference between dates and check for gaps
Read first
Make a real date
ReadThe air quality table stores Month and Day as separate numbers, for summer 1973. make_date turns them into a real date, and then date functions work.
SELECT make_date(1973, Month, Day) AS date,
dayname(make_date(1973, Month, Day)) AS weekday,
Temp
FROM new_york_air_quality_1973_sample
ORDER BY date
LIMIT 3;
May 1, 1973 was a Tuesday. A number can't tell you that, and neither can a date stored as text.
New York air quality, 1973Group by period
In the appdate_trunc rounds a date down to the start of its period, which makes it good for grouping.
SELECT date_trunc('month', make_date(1973, Month, Day)) AS month,
round(avg(Temp), 1) AS mean_temp,
count(*) AS days
FROM new_york_air_quality_1973_sample
GROUP BY month
ORDER BY month;
Change 'month' to 'week'. Weeks start on Monday, so the first and last group may have fewer days than the others. Check days before you compare the groups.
Which was hottest?
PredictPredict first
Which month had the highest mean daily maximum temperature in New York in summer 1973?
Reveal
August, ahead of July by a tenth of a degree. A gap that small over a single summer is the kind you shouldn't read much into. Summaries don't carry their own uncertainty.
Differences and formats
Exploredate_diff counts the boundaries between two dates. strftime formats a date for reading.
SELECT date_diff('day', DATE '1973-05-01', DATE '1973-09-30') AS days_between,
strftime(DATE '1973-05-01', '%B %d, %Y') AS pretty;
Now find the weekend days. DuckDB's dayofweek numbers Sunday as 0 and Saturday as 6.
SELECT count(*) AS weekend_days
FROM new_york_air_quality_1973_sample
WHERE dayofweek(make_date(1973, Month, Day)) IN (0, 6);
Other databases number weekdays differently, so check before you move this query.
When a date is really text
ExploreThe messy customer sample stores signup_date as text, in several formats. Sort it and look:
SELECT signup_date
FROM messy_customer_list_sample
ORDER BY signup_date
LIMIT 8;
Messy customer list
Text sorts character by character, so 15.08.2024 falls before 2021-…. Any date arithmetic would need parsing first. TRY_STRPTIME gives NULL instead of an error for values that don't fit a format, which shows you what's left to handle.
Insights flags this as a date stored as text, and Prepare's fix handles the formats.
Group by year yourself
In the appThis one uses the air passengers table, where month holds a full date. It opens in a new SQL tab, and Check compares your result with the answer's.
SQL exercise · air passengers
Group by a calendar period
Total the airline passengers for each year. Show the year as a number and the total, oldest year first, calling them year and passengers.
Hints
- year(d) and date_part('year', d) give the same number.
- The column called month holds a full date, so grouping on it directly gives one row per month.
Show a solution
SELECT year(month) AS year, sum(passengers) AS passengers
FROM {{table}}
GROUP BY year(month)
ORDER BY year;In the app this opens in a real SQL tab on the sample table and checks your result.
Check your understanding
CheckpointCheck yourself
How many days are there between May 1 and September 30, 1973, according to date_diff?
Show the answer
Answer: 152
It counts the boundaries crossed, so the table's 153 rows cover both end dates, one more than the difference.
Check yourself
How many weekend days does the table cover?
Show the answer
Answer: 44
44 of 153 days. Before you compare weekdays with weekends, look at how unevenly the groups are sized.
Check yourself
A date column sorts 10/1/2024 before 9/1/2024. What's the likely cause?
Show the answer
Answer: b
1 sorts before 9 as text. Parse the column into a real date before you sort, group or subtract.
Is it a trend?
ReflectA monthly table shows temperature rising then falling, which is a season. What would convince you of a trend across years? One summer can't. The Time Series track tests for trends properly:
Recap
Recapmake_datebuilds a date;date_truncgroups by period;date_diffmeasures gaps.- Check the number of days in each group before comparing periods.
- A date stored as text sorts and subtracts wrongly. Parse it first.
Concepts in this lesson