SELECT * FROM (
SELECT year(date) year, month(date) month, temp
FROM high_temps
WHERE date BETWEEN DATE '2015-01-01' AND DATE '2018-08-31'
)
PIVOT (
CAST(avg(temp) AS DECIMAL(4, 1))
FOR month in (
1 JAN, 2 FEB, 3 MAR, 4 APR, 5 MAY, 6 JUN,
7 JUL, 8 AUG, 9 SEP, 10 OCT, 11 NOV, 12 DEC
)
)
ORDER BY year DESC
SELECT * FROM (
SELECT year(date) year, month(date) month, temp, flag `H/L`
FROM (
SELECT date, temp, 'H' as flag
FROM high_temps
UNION ALL
SELECT date, temp, 'L' as flag
FROM low_temps
)
WHERE date BETWEEN DATE '2015-01-01' AND DATE '2018-08-31'
)
PIVOT (
CAST(avg(temp) AS DECIMAL(4, 1))
FOR month in (6 JUN, 7 JUL, 8 AUG, 9 SEP)
)
ORDER BY year DESC, `H/L` ASC
SELECT * FROM (
SELECT year(date) year, month(date) month, temp, flag
FROM (
SELECT date, temp, 'H' as flag
FROM high_temps
UNION ALL
SELECT date, temp, 'L' as flag
FROM low_temps
)
WHERE date BETWEEN DATE '2015-01-01' AND DATE '2018-08-31'
)
PIVOT (
CAST(avg(temp) AS DECIMAL(4, 1))
FOR (month, flag) in (
(6, 'H') JUN_hi, (6, 'L') JUN_lo,
(7, 'H') JUL_hi, (7, 'L') JUL_lo,
(8, 'H') AUG_hi, (8, 'L') AUG_lo,
(9, 'H') SEP_hi, (9, 'L') SEP_lo
)
)
ORDER BY year DESC
ScaDaMaLe Course site and book