SQL 查询 GROUP BY 分组
SQL query GROUP BY groups
我有这样的东西:
id
name
totalAmount
1
name1
10
2
name1
20
3
name1
25
4
name2
5
5
name2
12
并且需要看起来像这样:
id's
name
totalAmount
1,2
name1
30
2,3
name1
45
1,3
name1
35
1,2,3
name1
55
4,5
name2
17
我正在使用 STRING_AGG
但不知道如何分隔前 3 个 ID。
-- *** Test Data ***
CREATE TABLE #t
(
id int NOT NULL PRIMARY KEY
,[name] nvarchar(30) NOT NULL
,totalAmount money NOT NULL
);
INSERT INTO #t
VALUES (1, 'name1', 10)
,(2, 'name1', 20)
,(3, 'name1', 25)
,(4, 'name2', 5)
,(5, 'name2', 12);
-- *** End Test Data ***
SELECT CAST(T1.id AS varchar(10))
+ ',' + CAST(T2.id AS varchar(10)) AS ids
,T1.[name] AS [name]
,T1.totalAmount + T2.totalAmount AS totalAmount
FROM #t T1
JOIN #t T2
ON T1.[name] = T2.[name]
WHERE T1.id < T2.id
UNION ALL
SELECT CAST(T1.id AS varchar(10))
+ ',' + CAST(T2.id AS varchar(10))
+ ',' + CAST(T3.id AS varchar(10)) AS ids
,T1.[name] AS [name]
,T1.totalAmount + T2.totalAmount + T3.totalAmount AS totalAmount
FROM #t T1
JOIN #t T2
ON T1.[name] = T2.[name]
JOIN #t T3
ON T1.[name] = T3.[name]
WHERE T1.id < T2.id
AND T2.id < T3.id;
这是一个递归版本,可以处理超过 3 个 id 的名称和 returns 所有可能的组合。正如 Dai 指出的那样,要小心,因为组合的数量会迅速增加。但是如果你的真实数据就像你的例子(通常每个名字有 2-3 个 ID)那么它应该没问题。
值得注意的是,我这样做是为了好玩。也许您最好只存储原始数据并在应用程序层中执行此类恶作剧。
CREATE TABLE #data
(
id INT,
[name] VARCHAR(10),
totalAmount INT
);
INSERT INTO #data
VALUES
(1, 'name1', 10),
(2, 'name1', 20),
(3, 'name1', 25),
(4, 'name2', 5),
(5, 'name2', 12);
WITH cte (name, ids, maxid, tot) AS
(
SELECT a.name,
CONVERT(VARCHAR(8000), CONVERT(VARCHAR(10), a.id) + ',' + CONVERT(VARCHAR(10), b.id) ) AS ids,
b.id AS maxid,
a.totalAmount + b.totalAmount AS tot
FROM #data a
INNER JOIN #data b ON b.name = a.name AND a.id < b.id
UNION ALL
SELECT cte.name,
CONVERT(VARCHAR(8000), cte.ids + ',' +CONVERT(VARCHAR(10), a.id)),
a.id AS maxid,
cte.tot + a.totalAmount
FROM cte
INNER JOIN #data a ON cte.name = a.name
WHERE a.id > cte.maxid
)
SELECT ids, name, tot
FROM cte
我有这样的东西:
id | name | totalAmount |
---|---|---|
1 | name1 | 10 |
2 | name1 | 20 |
3 | name1 | 25 |
4 | name2 | 5 |
5 | name2 | 12 |
并且需要看起来像这样:
id's | name | totalAmount |
---|---|---|
1,2 | name1 | 30 |
2,3 | name1 | 45 |
1,3 | name1 | 35 |
1,2,3 | name1 | 55 |
4,5 | name2 | 17 |
我正在使用 STRING_AGG
但不知道如何分隔前 3 个 ID。
-- *** Test Data ***
CREATE TABLE #t
(
id int NOT NULL PRIMARY KEY
,[name] nvarchar(30) NOT NULL
,totalAmount money NOT NULL
);
INSERT INTO #t
VALUES (1, 'name1', 10)
,(2, 'name1', 20)
,(3, 'name1', 25)
,(4, 'name2', 5)
,(5, 'name2', 12);
-- *** End Test Data ***
SELECT CAST(T1.id AS varchar(10))
+ ',' + CAST(T2.id AS varchar(10)) AS ids
,T1.[name] AS [name]
,T1.totalAmount + T2.totalAmount AS totalAmount
FROM #t T1
JOIN #t T2
ON T1.[name] = T2.[name]
WHERE T1.id < T2.id
UNION ALL
SELECT CAST(T1.id AS varchar(10))
+ ',' + CAST(T2.id AS varchar(10))
+ ',' + CAST(T3.id AS varchar(10)) AS ids
,T1.[name] AS [name]
,T1.totalAmount + T2.totalAmount + T3.totalAmount AS totalAmount
FROM #t T1
JOIN #t T2
ON T1.[name] = T2.[name]
JOIN #t T3
ON T1.[name] = T3.[name]
WHERE T1.id < T2.id
AND T2.id < T3.id;
这是一个递归版本,可以处理超过 3 个 id 的名称和 returns 所有可能的组合。正如 Dai 指出的那样,要小心,因为组合的数量会迅速增加。但是如果你的真实数据就像你的例子(通常每个名字有 2-3 个 ID)那么它应该没问题。
值得注意的是,我这样做是为了好玩。也许您最好只存储原始数据并在应用程序层中执行此类恶作剧。
CREATE TABLE #data
(
id INT,
[name] VARCHAR(10),
totalAmount INT
);
INSERT INTO #data
VALUES
(1, 'name1', 10),
(2, 'name1', 20),
(3, 'name1', 25),
(4, 'name2', 5),
(5, 'name2', 12);
WITH cte (name, ids, maxid, tot) AS
(
SELECT a.name,
CONVERT(VARCHAR(8000), CONVERT(VARCHAR(10), a.id) + ',' + CONVERT(VARCHAR(10), b.id) ) AS ids,
b.id AS maxid,
a.totalAmount + b.totalAmount AS tot
FROM #data a
INNER JOIN #data b ON b.name = a.name AND a.id < b.id
UNION ALL
SELECT cte.name,
CONVERT(VARCHAR(8000), cte.ids + ',' +CONVERT(VARCHAR(10), a.id)),
a.id AS maxid,
cte.tot + a.totalAmount
FROM cte
INNER JOIN #data a ON cte.name = a.name
WHERE a.id > cte.maxid
)
SELECT ids, name, tot
FROM cte