Ordered Set Functions

Have you ever needed to concatenate elements of a group into one string based on some order? That’s a scenario that an ordered set function could help address. An ordered set function is a type of aggregate function. What distinguishes it from a general set function (like SUM, MIN, MAX, and so on) is that there’s ordering relevance to the calculation, such as the order in which you want to concatenate the elements.

In this chapter, I will discuss ordered set functions and the kinds of solutions they help with. At the date of this writing, the only ordered set function that SQL Server supports is STRING_AGG for string concatenation. This function was added in SQL Server 2017. There are additional ordered set functions ...

Get T-SQL Window Functions: For data analysis and beyond, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.