String Aggregate in SQL Server 2017

String Aggregate in SQL Server 2017

A common request when writing queries is to aggregate text values in a column into a single list of values with a separator, such as a comma. For example, a customer might have several purchases made by different salespeople, and we would like to generate a report that displays a list of all the salespeople associated with each customer. Something like this:

In previous versions of SQL Server, we could accomplish this with all kinds of methods. For example, we could use the PIVOT function or the FOR XML PATH clause, or we could implement a CLR aggregate function. All of these methods are quite complicated.

Finally, in SQL Server 2017, we have a new built-in T-SQL function - STRING_AGG. This function does all the hard work for us, and it's so much easier to use than the other methods mentioned above.

Here is how the query looks like:

SELECT
	CustomerId	= CustomerId ,
	SalesPeople	= STRING_AGG (SalesPerson , N',')
FROM
	Sales.Transactions
GROUP BY
	CustomerId
ORDER BY
	CustomerId ASC;

You can even add sorting to the text values within each group by using the WITHIN GROUP clause, like this:

SELECT
	CustomerId	= CustomerId ,
	SalesPeople	= STRING_AGG (SalesPerson , N',') WITHIN GROUP (ORDER BY SalesPerson ASC)
FROM
	Sales.Transactions
GROUP BY
	CustomerId
ORDER BY
	CustomerId ASC;

Here is a script you can download that creates the "Sales.Transactions" table, populates it with some data, and demonstrates the STRING_AGG function.

Enjoy!

Naomi Nicole Vardi

BI Expert | Austin, TX

7 年

What a great addition! Came across this issue several times lately, and I love the elegance of this solution.

回复
Stephen Rosehill

Senior Database Administrator at SAFE Credit Union

7 年

This solves an existing problem I have been working on. Time to upgrade to 2017!

回复
Mohamed Almogtaba Ali

Business Intelligence Consultant at Malomatia

7 年

So cool and useful feature especially in Dynamic SQL

回复

要查看或添加评论,请登录

Guy Glantser的更多文章

  • SQL Saturday Albany 2019

    SQL Saturday Albany 2019

    I love speaking and I love to travel. And I'm fortunate to be able to combine the two from time to time.

    7 条评论
  • My Recap of SQL Saturday Athens

    My Recap of SQL Saturday Athens

    I had the honor and the pleasure to speak in SQL Saturday Athens last week. It was a short visit to Athens, but I…

    6 条评论
  • Introduction to Azure Machine Learning

    Introduction to Azure Machine Learning

    Last week I presented a session about Azure Machine Learning in the Big Data Demystified meetup. It was an introductory…

    3 条评论
  • Average Number of Commands in Replication Transactions

    Average Number of Commands in Replication Transactions

    Recently I had the pleasure of troubleshooting transactional replication with multiple publications and large…

    1 条评论
  • How We Improve the Quality of Our Services

    How We Improve the Quality of Our Services

    Once a year we run a customer satisfaction survey among our customers. It's important for us to provide the best…

  • SQL Saturday Israel 2019

    SQL Saturday Israel 2019

    It's over. And it was awesome! SQL Saturday Israel is just getting better every year.

    2 条评论
  • Benchmarking

    Benchmarking

    As DBAs, in almost every work that we do on SQL Server, we change something. We might be rewriting a query, adding a…

  • My SQLBits 2019 Session

    My SQLBits 2019 Session

    I have just finished my session in #SQLBits - How to Make Sure Your SSIS Packages Will Never Fail. I began my session…

    4 条评论
  • Working with Very Large Tables

    Working with Very Large Tables

    In July and August 2017, I presented an online session with the title “Working with Very Large Tables Like a Pro in SQL…

  • Customer Satisfaction Survey 2018 - The Results

    Customer Satisfaction Survey 2018 - The Results

    One of our core values is Service Experience. It's really important for us to provide a unique level of service to our…

    3 条评论

社区洞察

其他会员也浏览了