SQL服务器如何管理两列中具有相同值的行,对几列求和,逗号分隔另一列并显示1行

SQL Server how to manage rows that have same value in two column, sum few columns , Comma seperate another and display 1 row

我有以下 table:

共有11行。第 5&6 行具有相同的 SenderNameDateOfBirth。我想显示总共 10 行并按以下方式合并第 5 行和第 6 行:ReceiversNoofTransactions 列正在求和。

我已经按照以下方式创建了测试数据。

DECLARE @SenToRecList TABLE
                      (
                          NoofTransactions int, 
                          Sender varchar(50), 
                          SenderName varchar(200), 
                          Receivers int, 
                          Vol money, 
                          DateOfBirth varchar(50)
                      )

INSERT INTO @SenToRecList
VALUES (34, 50116, 'JACQUELINE MONROE' ,33, 2045.0000,'Jan  1 1988 12:00AM')

INSERT INTO @SenToRecList
VALUES (30, 43377, 'Suranjan rai' ,30, 427.7600,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (121, 44158 ,'Dev Bhattarai', 27, 3051.3100,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (63 ,44205 ,'Retry Hauaja' ,27, 1003.4500,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (45, 44343, 'Kessie Frost' ,25 ,694.4200,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (45, 443436, 'Kessie Frost' ,25 ,694.4200,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (43, 43236, 'Nam Dosan' ,24 ,1340.6700,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (47, 44466, 'Micah Bates', 22 ,589.9200,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (22, 50094, 'RUTH LEWIS', 22 ,632.6000,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (26, 40352, 'aliyan marki', 22, 2657.0000,'Oct 27 1971 12:00AM')

INSERT INTO @SenToRecList
VALUES (176 ,41604 ,'verified SMITH' ,22, 5511.2000,'Oct 27 1971 12:00AM')

请帮我创建这个查询。

您可以使用此查询来实现您的目标。 (SQL 服务器 2017(14.x)及更高版本)

SELECT SenderName, SUM(NoofTransactions) AS NoofTransactions, STRING_AGG(Sender, ',') AS Sender, SUM(Receivers) AS Receivers, SUM(Vol) AS Vol, DateOfBirth
FROM @SenToRecList
GROUP BY SenderName, DateOfBirth

进一步阅读; https://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver15

对于 SQL 2017 年之前的服务器,请检查此 link;