如何根据多个列按数据分组,然后在SQL中与相同值合并行
我有一个表如下:
DECLARE @t1 TABLE
(
Country VARCHAR(MAX) NOT NULL,
AccountName VARCHAR(MAX) NOT NULL,
DealReference VARCHAR(MAX),
[Probability 0%] INT,
[Probability 50%] INT,
[Probability 100%] INT
)
INSERT INTO @t1 (Country, AccountName, DealReference, [Probability 0%], [Probability 50%], [Probability 100%]) VALUES
('Austria', 'Tech Data Vienna', 'AT-379871323', 0, 8000, 0),
('Austria', 'Tech Data Vienna', 'AT-379871323', 3000, 0, 0),
('Finland', 'Fly Logictics', 'FN-3897214', 6000, 0, 0),
('Germany', 'Electronics De Gmbh', 'DE-20948332', 4000, 0, 0 ),
('Germany', 'Electronics De Gmbh', 'DE-2174634', 0, 2000, 0 ),
('Norway', 'MK distribution Oslo', 'NE-3539232', 9000, 0, 0),
('Sweden', 'Bio Pharm Stockholm', 'SE-3897214', 2500, 0, 0),
('Sweden', 'Bio Pharm Stockholm', 'SE-3897214', 0, 0, 1000);
select * from @t1 order by 1
我想将@t1 转换为以下结果。
基本上,我将通过 country 和 accountName 进行组,并在最后添加总列。 在通过进行组时,我想合并交易值通过逗号分开。 如果多行有一个独特的值,它将仅捕获一个值。
下面是我的解决方案,一切都可以脱颖而出。
我正在使用SQL Server Management Studio 18.11.1,基于官方文档和多个回答的线程,我应该在这里使用string_agg(dreadRereference,',',',',',')
,
但是我得到了这个错误
'string_agg'不是公认的内置功能名称。
this 解决方案对我有用,但是当我在 +100k行上运行查询时, XML路径非常慢。
有什么建议,我做错了什么? 非常感谢。
I have a table as below :
DECLARE @t1 TABLE
(
Country VARCHAR(MAX) NOT NULL,
AccountName VARCHAR(MAX) NOT NULL,
DealReference VARCHAR(MAX),
[Probability 0%] INT,
[Probability 50%] INT,
[Probability 100%] INT
)
INSERT INTO @t1 (Country, AccountName, DealReference, [Probability 0%], [Probability 50%], [Probability 100%]) VALUES
('Austria', 'Tech Data Vienna', 'AT-379871323', 0, 8000, 0),
('Austria', 'Tech Data Vienna', 'AT-379871323', 3000, 0, 0),
('Finland', 'Fly Logictics', 'FN-3897214', 6000, 0, 0),
('Germany', 'Electronics De Gmbh', 'DE-20948332', 4000, 0, 0 ),
('Germany', 'Electronics De Gmbh', 'DE-2174634', 0, 2000, 0 ),
('Norway', 'MK distribution Oslo', 'NE-3539232', 9000, 0, 0),
('Sweden', 'Bio Pharm Stockholm', 'SE-3897214', 2500, 0, 0),
('Sweden', 'Bio Pharm Stockholm', 'SE-3897214', 0, 0, 1000);
select * from @t1 order by 1
I want to transform @t1 into the below result.
Basically I will do group by Country and AccountName and add the Total column at the end.
While doing the group by, I want to merge the DealReference values seperate by a comma.
In case there is one unique value for multiple rows it will catch only one value.
Below is my solution, everything works apart apart the column DealReference I couldn't figure out how to merge the values into one cell.
I'm using SQL Server Management Studio 18.11.1, based on the official documentation and multiple answered threads here I should use STRING_AGG(DealReference, ', ')
But I'm getting this error
'STRING_AGG' is not a recognized built-in function name.
This solution for example from a similar issue works for me but the XML PATH is very slow when I run the query on +100k rows.
any suggestions please what I am doing wrong ?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论