Skip to content

Instantly share code, notes, and snippets.

@thomasthaddeus
Created July 25, 2022 18:36
Show Gist options
  • Save thomasthaddeus/6d51615993e2abeae6e580214e8685db to your computer and use it in GitHub Desktop.
Save thomasthaddeus/6d51615993e2abeae6e580214e8685db to your computer and use it in GitHub Desktop.
Window Functions in SQL
SELECT
artist,
week,
streams_millions,
streams_millions - LAG(streams_millions, 1, streams_millions) OVER (
PARTITION BY artist
ORDER BY week
) AS 'streams_millions_change',
chart_position,
LAG(chart_position, 1, chart_position) OVER (
PARTITION BY artist
ORDER BY week
) - chart_position AS 'chart_position_change'
FROM
streams
WHERE
artist = 'Lady Gaga';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment