-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrainfall2.sql
27 lines (20 loc) · 875 Bytes
/
rainfall2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# rainfall2.sql
# rainfall table: each record indicates date of measurement and amount
# of precipitation on that day.
# See also the rainfall.sql file.
SOURCE rainfall.sql;
# calculate running total and average, permitting missing days
# create a couple of "missing" days. The precip values are zero,
# so the cumulative totals and averages for the days that remain
# should be the same as those computed above. (That makes it easy
# to compare the result from the following SELECT with the result
# from the SELECT in rainfall.sql.)
DELETE FROM rainfall WHERE precip = 0;
SELECT t1.date, t1.precip AS 'daily precip',
SUM(t2.precip) AS 'cum. precip',
DATEDIFF(MAX(t2.date),MIN(t2.date)) + 1 AS days,
SUM(t2.precip) / (TO_DAYS(MAX(t2.date)) - TO_DAYS(MIN(t2.date)) + 1)
AS 'avg. precip'
FROM rainfall AS t1, rainfall AS t2
WHERE t1.date >= t2.date
GROUP BY t1.date;