Skip to content
Advertisement

Get yearly average of per id

This code gets the number of ids that that show up for each month with in the year.

This is the result i get:

I want to get the yearly average of each ID, and the overall count. The results will look like this:

Advertisement

Answer

You can add another level of aggregation:

Notes:

  • you don’t actually need a CTE to pre-filter the data: this can be done directly in the first aggregation query

  • I changed the filtering logic to use half-open intervals; if your dates have time components that are other than '00:00:00', then this avoids filtering out the last day of the month (and if not, it doesn’t hurt either)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement