I have a table with column year, month, employee_count. I want to fetch employee_count from the latest month available in each year
From this:
year | month | employee_count |
---|---|---|
2010 | 1 | 230 |
2013 | 6 | 1205 |
2022 | 1 | 200 |
2023 | 1 | 20 |
2013 | 5 | 123 |
2010 | 11 | 33 |
2023 | 10 | 45 |
I need this:
year | month | employee_count |
---|---|---|
2010 | 11 | 33 |
2013 | 6 | 1205 |
2022 | 1 | 200 |
2023 | 10 | 45 |
2
Answers
Based on your description of the issue, and your desired output:
latest_month_per_year
) retrieve the latest month (MAX(month)
) for each year by grouping the data by year.As an outcome this will give you the latest
employee_count
per each year.I tried the below query with RIGHT JOIN which seems to work: