Chad Callihan messes with groups:
When was the last time you wrote a SQL query and knew something was possible but just couldn’t remember how? I had one of those moments this week with STRING_AGG and ordering data, and although it was frustrating, I knew it would make a worthwhile blog post. Let’s look at some examples using STRING_AGG and WITHIN GROUP (aka the clause that slipped my mind).
There’s a perfectly good reason why WITHIN GROUP
might slip your mind: STRING_AGG()
is known as an ordered set function (versus a window function which uses an OVER()
clause). It’s also the only ordered set function SQL Server supports, so you don’t get too many opportunities to use the key phrase.