Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
Created October 17, 2022 13:38
Show Gist options
  • Select an option

  • Save stevesohcot/b312564928707c945649514d7742eedb to your computer and use it in GitHub Desktop.

Select an option

Save stevesohcot/b312564928707c945649514d7742eedb to your computer and use it in GitHub Desktop.
SQL - get latest day for each month in dataset
SELECT maxDay
FROM (
SELECT theYear, theMonth, MAX(theDay) AS maxDay
FROM (
SELECT YEAR(myDate) AS theYear, MONTH(myDate) AS theMonth, MAX(myDate) AS theDay
FROM myTable
GROUP BY myDate
) AS qryGetMaxDateInner
GROUP BY
theYear, theMonth
) AS qryMaxDate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment