By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
/* THE DATA */
CREATE TABLE [dbo].[RepRatings](
[row] [bigint] NULL,
[rep] [int] NOT NULL,
[quartc] [varchar](3) NOT NULL,
[rate] [varchar](3) NULL
) ON [PRIMARY];
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (1, 911911, N'Q1M', N'1');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (2, 911911, N'Q1M', N'1');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (3, 911911, N'Q1M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (4, 911911, N'Q1M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (5, 911911, N'Q1M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (6, 911911, N'Q1M', N'1');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (1, 911911, N'Q2M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (2, 911911, N'Q2M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (3, 911911, N'Q2M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (4, 911911, N'Q2M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (5, 911911, N'Q2M', N'1');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (6, 911911, N'Q2M', N'1');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (1, 911911, N'Q3M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (2, 911911, N'Q3M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (3, 911911, N'Q3M', N'2');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (4, 911911, N'Q3M', N'1');
INSERT [dbo].[RepRatings] ([row], [rep], [quartc], [rate]) VALUES (5, 911911, N'Q3M', N'2');
17 rows affected
select rep, STRING_AGG(rates, '|') as ratings, STRING_AGG(quartc, '|') as quartc
from (
select rep, quartc, STRING_AGG(rate, ',') as rates
from [RepRatings]
group by rep, quartc
) s
group by rep
rep | ratings | quartc |
---|---|---|
911911 | 1,1,2,2,2,1|2,2,2,2,1,1|2,2,2,1,2 | Q1M|Q2M|Q3M |
select rep, STRING_AGG(CONCAT(quartc, ': ', rates), '|') as ratings
from (
select rep, quartc, STRING_AGG(rate, ',') as rates
from [RepRatings]
group by rep, quartc
) s
group by rep
rep | ratings |
---|---|
911911 | Q1M: 1,1,2,2,2,1|Q2M: 2,2,2,2,1,1|Q3M: 2,2,2,1,2 |