Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created January 31, 2012 17:22
Show Gist options
  • Save chanmix51/1711698 to your computer and use it in GitHub Desktop.
Save chanmix51/1711698 to your computer and use it in GitHub Desktop.
ALTER TABLE city ADD COLUMN last_weather_data weather_probe;
UPDATE
city c
SET
last_weather = wp
FROM
(
SELECT
wp.city_name,
wp.created_at,
rank() OVER created_at_window AS ranking
FROM
weather_probe wp
WINDOW
created_at_window AS (PARTITION BY city_name ORDER BY created_at DESC)
) wpr
NATURAL JOIN weather_probe wp
WHERE
wpr.ranking = 1
AND
c.name = wp.city_name
RETURNING *
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment