SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,894 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi Everyone. I'm having trouble trying to get my data to display properly. For every three years, I need to add the year to a new group. Here's an example:
2018 - 1
2019 - 1
2020 - 1
2021 - 2
2022 - 2
2023 - 2
2024 - 3
2025 - 3
2026 - 3
2027 - 4
2028 - 4
2029 - 4
Any help would be greatly appreciated. Thank you.
Guessing that you have a table like this:
declare @years as table ( [year] int )
insert @years values
( 2018 ),
( 2019 ),
( 2020 ),
( 2021 ),
( 2022 ),
( 2023 ),
( 2024 ),
( 2025 ),
( 2026 ),
( 2027 ),
( 2028 ),
( 2029 )
check this query:
select *, (dense_rank() over (order by [year]) - 1) / 3 + 1 as [group]
from @years
order by [year]