Skip to content

Instantly share code, notes, and snippets.

@James-McNeill
Created February 19, 2022 20:39
Show Gist options
  • Save James-McNeill/55c7271ce314db2dd04a68de84ce0ee2 to your computer and use it in GitHub Desktop.
Save James-McNeill/55c7271ce314db2dd04a68de84ce0ee2 to your computer and use it in GitHub Desktop.
-- Analytical functions (LAG)
SELECT *,
LAG(t1.Value)
OVER(
PARTITION BY t1.Area, t1.Property_type
ORDER BY t1.Date
) lagValue,
LAG(t1.Value, 3)
OVER(
PARTITION BY t1.Area, t1.Property_type
ORDER BY t1.Date
) lagValue3,
COALESCE(
LAG(t1.Value)
OVER(
PARTITION BY t1.Area, t1.Property_type
ORDER BY t1.Date
)
, 0) lagValueZero,
LAG(t1.Value, 3, 0)
OVER(
PARTITION BY t1.Area, t1.Property_type
ORDER BY t1.Date
) lagValue3
FROM Medium.dbo.HPM09_202112 AS t1
WHERE t1.Value > 0
AND t1.Date BETWEEN '2020-01-01' AND '2020-06-01'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment