使用 MS-SQL 2019。我正在尝试按季度将代表评级汇总到单行中。因此,消费者可以在单行中按季度查看评级。 所以输出是这样的:
/* rep | ratings | quartc */
/* ------ | ------------------------------- | --------------- */
/* 911911 | 1,2,2,2,1 | 2,2,2,2,1 | 2,2,1,2 | Q1M | Q2M | Q3M */
或者更好喜欢
/* rep | Qtr: ratings */
/* ------ | ----------------------------------------
/* 911911 | Q1M: 1,2,2,2,1 Q2M:2,2,2,2,1 Q3M:2,2,1,2 */
我在使用 xml 和其他东西方面取得了进展,但我似乎无法理解如何在评级字符串中按季度进行分组。 如果您有一个好主意,这里有一些可运行的实验代码。
/* 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');
GO
/*TRIED SO FAR, IS CLOSE */
Select
[RefRowInQtr] = [row], A.rep,
ratings = stuff((
select ',' + B.rate
from RepRatings B
WHERE A.rep=B.rep
for xml path('')),1,1,'')
,quartc = stuff((
select distinct ' ' + B.quartc
from RepRatings B
WHERE A.rep=B.rep
for xml path('')),1,1,'')
From RepRatings A
假设这是 sql server 2017 或更高版本,您可以使用
STRING_AGG
代替 for xml path
:
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
结果:
代表 | 收视率 | 四次 |
---|---|---|
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
结果:
代表 | 收视率 |
---|---|
911911 | 第一季度:1,1,2,2,2,1|第二季度:2,2,2,2,1,1|第三季度:2,2,2,1,2 |