SQLTeam.com | Weblogs | Forums

Concat multiples data based on another column

I would like to concatenate the following data in sql code.

R12 Months Year Month
R12CY 202012
R12CY 202101
R12CY 202102
R12CY 202103
R12CY 202104
R12PY 201912
R12PY 202001
R12PY 202002
R12PY 202003
R12PY 202004

I was thinking something like
case when [R12] = 'R12PY' then concat(min(yearmonth)) + '-' + concat(max(yearmonth))
when [r12] = 'R12CY' then concat(min(yearmonth)) + '-' + concat(max(yearmonth))

The goal is to have it like 201912 - 202004 for R12PY and 202012 - 202104 for the R12CY and I could just write it like that but the data changes every month so I would be manually writing something and want this automated

Cte as R12, min , max group by R12

From cte min plus max

If you could provide directly usable sample data -- i.e. CREATE TABLE and INSERT statement(s) rather than just a picture -- I'll write code to do this.