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.