Professional Documents
Culture Documents
Window Lag - Sqlzoo
Window Lag - Sqlzoo
Window Lag - Sqlzoo
Window LAG
Contents
COVID-19 Data
Window Function
Introducing the covid table
Introducing the LAG function
LAG operation
Number of new cases
Weekly changes
LAG using a JOIN
RANK()
Infection rate
Turning the corner
COVID-19 Data
Notes on the data:
This data was assembled based on work done by Rodrigo Pombo (https://github.com/pomber/covid19) based on John Hopkins
University (https://systems.jhu.edu/research/public-health/ncov/), based on World Health Organisation (https://www.who.int/health-topics/coronavirus).
The data was assembled 21st April 2020 - there are no plans to keep this data set up to date.
Window Function
The SQL Window functions include LAG, LEAD, RANK and NTILE. These functions operate over a "window" of rows - typically these are rows in the table
that are in some sense adjacent.
1.
The example uses a WHERE clause to show the cases in 'Italy' in March 2020.
FROM covid
ORDER BY whn;
Result:
name day_ confirmed deaths recovered
Spain 1 84 0 2
Spain 2 120 0 2
Spain 3 165 1 2
Spain 4 222 2 2
Spain 5 259 3 2
Spain 6 400 5 2
Spain 7 500 10 30
Spain 8 673 17 30
Spain 9 1073 28 32
S i 10 1695 35 32
https://sqlzoo.net/wiki/Window_LAG 1/6
12/26/21, 9:48 PM Window LAG - SQLZOO
SET @@sql_mode='ANSI';
2.
The LAG function is used to show data from the preceding row or the table.
When lining up rows the data is partitioned by country name and ordered by the
data whn. That means that only data from Italy is considered.
SET @@sql_mode='ANSI';
SELECT name, DAY(whn) day_, confirmed, LAG(confirmed, 1) OVER (PARTITION BY name ORDER BY whn) confirmed_yday
FROM covid
ORDER BY whn;
Result:
name day_ confirmed confirmed_yday
Italy 1 1694
Italy 2 2036 1694
Italy 3 2502 2036
Italy 4 3089 2502
Italy 5 3858 3089
Italy 6 4636 3858
Italy 7 5883 4636
It l 8 7375 5883
LAG operation
Here is the correct query showing the cases for the day before:
FROM covid
AND MONTH(whn) = 3
ORDER BY whn
Notice how the values in the LAG column match the value of the row diagonally above and to the left.
https://sqlzoo.net/wiki/Window_LAG 2/6
12/26/21, 9:48 PM Window LAG - SQLZOO
...
3.
The number of confirmed case is cumulative - but we can use LAG to recover the number of new cases reported for each day.
Show the number of new cases for each day, for Italy, for March.
SET @@sql_mode='ANSI';
SELECT name, DAY(whn) day_, (confirmed - LAG(confirmed, 1) OVER (PARTITION BY name ORDER BY whn)) AS day_count
FROM covid
ORDER BY whn;
Submit SQL Restore default
Result:
name day_ day_count
Italy 1
Italy 2 342
Italy 3 466
Italy 4 587
Italy 5 769
Italy 6 778
Italy 7 1247
Italy 8 1492
Italy 9 1797
Weekly changes
4.
The data gathered are necessarily estimates and are inaccurate. However by taking a longer time span we can mitigate some of the effects.
You can filter the data to view only Monday's figures WHERE WEEKDAY(whn) = 0.
Show the number of new cases in Italy for each week in 2020 - show Monday only.
SET @@sql_mode='ANSI';
SELECT name, DATE_FORMAT(whn,'%Y-%m-%d') date_, (confirmed - LAG(confirmed, 1) OVER (PARTITION BY name ORDER BY whn)) AS week_count
FROM covid
ORDER BY whn;
Result:
name date_ week_count
Italy 2020-01-27
Italy 2020-02-03 2
Italy 2020-02-10 1
Italy 2020-02-17 0
Italy 2020-02-24 226
https://sqlzoo.net/wiki/Window_LAG 3/6
12/26/21, 9:48 PM Window LAG - SQLZOO
Italy 2020-03-02 1807
Italy 2020-03-09 7136
Italy 2020-03-16 18808
Italy 2020-03-23 35947
Italy 2020-03-30 37812
5.
You can JOIN a table using DATE arithmetic. This will give different results if data is missing.
Show the number of new cases in Italy for each week - show Monday only.
In the sample query we JOIN this week tw with last week lw using the DATE_ADD function.
FROM covid tw LEFT JOIN covid lw ON (DATE_ADD(lw.whn, INTERVAL 1 WEEK) = tw.whn) AND (tw.name = lw.name)
ORDER BY tw.whn;
Correct answer
name date_ week_count
Italy 2020-01-27
Italy 2020-02-03 2
Italy 2020-02-10 1
Italy 2020-02-17 0
Italy 2020-02-24 226
Italy 2020-03-02 1807
Italy 2020-03-09 7136
Italy 2020 03 16 18808
RANK()
6.
The query shown shows the number of confirmed cases together with the world ranking for cases.
Notice that while Spain has the second highest confirmed cases, Italy has the second highest number of deaths due to the virus.
https://sqlzoo.net/wiki/Window_LAG 4/6
12/26/21, 9:48 PM Window LAG - SQLZOO
SET @@sql_mode='ANSI';
SELECT name, confirmed, RANK() OVER (ORDER BY confirmed DESC) rnk_confirmed, deaths, RANK() OVER (ORDER BY deaths DESC) rnk_deaths
FROM covid
Correct answer
name confirmed rnk_confirmed deaths rnk_deaths
US 799022 1 45179 1
Spain 200210 2 20852 3
Italy 181228 3 24114 2
France 154402 4 20241 4
Infection rate
7.
The query shown includes a JOIN t the world table so we can access the total population of each country and calculate infection rates (in cases per 100,000).
SET @@sql_mode='ANSI';
Correct answer
name infection_rat.. rank_infectio..
China 6 39
India 1 1
Indonesia 3 1
Brazil 20 57
Pakistan 5 39
Nigeria 0 1
Bangladesh 2 1
Russia 32 61
Japan 9 39
Mexico 7 39
Philippines 6 39
Vietnam 0 1
Ethiopia 0 1
E t 4 1
https://sqlzoo.net/wiki/Window_LAG 5/6
12/26/21, 9:48 PM Window LAG - SQLZOO
8.
For each country that has had at last 1000 new cases in a single day, show the date of the peak number of new cases.
@@ q _ ;
DATE_FORMAT(whn,'%Y-%m-%d') date,
confirmed - LAG(confirmed,1)
FROM covid) a) b
ORDER BY date;
Correct answer
name date newcases
China 2020-02-13 15136
Ecuador 2020-04-24 11536
Qatar 2020-05-30 2355
Chile 2020-06-06 13990
Pakistan 2020-06-14 12073
Kyrgyzstan 2020-07-18 11505
Equatorial Guinea 2020-07-31 1750
Peru 2020-08-02 21358
Luxembourg 2020-11-02 1967
Italy 2020-11-13 40902
Belize 2020-12-03 1382
Turkey 2020-12-10 823225
Sweden 2020-12-29 32485
Lithuania 2020-12-30 3984
Panama 2021-01-06 5186
Ireland 2021-01-08 8227
US 2021-01-08 303487
United Kingdom 2021-01-08 68192
Sudan 2021-01-10 1215
Lebanon 2021-01-15 6154
Dominican Republic 2021 01 16 2370
https://sqlzoo.net/wiki/Window_LAG 6/6