Where to use WITHIN GROUP function without STRING_AGG function

Sudip Bhatt 2,281 Reputation points
2021-01-07T17:02:20.707+00:00

see this code

https://stackoverflow.com/questions/194852/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-serv?rq=1

SELECT GroupName, STRING_AGG(Name, ', ') WITHIN GROUP (ORDER BY Name ASC) AS Departments
FROM HumanResources.Department 
GROUP BY GroupName;

1) What WITHIN GROUP is doing in above sql ? WITHIN GROUP used to show data in specific order ?

thanks

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-01-08T02:17:55.36+00:00

    Hi @Sudip Bhatt

    In STRING_AGG,optionally specify order of concatenated results using WITHIN GROUP clause.
    Please refer to an example:

        USE AdventureWorks2017  
        GO  
          
        SELECT TOP 10 City, STRING_AGG(CONVERT(NVARCHAR(max), EmailAddress), ';') WITHIN GROUP (ORDER BY EmailAddress ASC) AS emails   
        FROM Person.BusinessEntityAddress AS BEA    
        INNER JOIN Person.Address AS A ON BEA.AddressID = A.AddressID  
        INNER JOIN Person.EmailAddress AS EA ON BEA.BusinessEntityID = EA.BusinessEntityID   
        GROUP BY City  
        ORDER BY City;  
          
        SELECT TOP 10 City, STRING_AGG(CONVERT(NVARCHAR(max), EmailAddress), ';') AS emails   
        FROM Person.BusinessEntityAddress AS BEA    
        INNER JOIN Person.Address AS A ON BEA.AddressID = A.AddressID  
        INNER JOIN Person.EmailAddress AS EA ON BEA.BusinessEntityID = EA.BusinessEntityID   
        GROUP BY City  
        ORDER BY City;  
    

    Output:
    54685-image.png

    The WITHIN GROUP clause is specified in the first query, so EmailAddress is separated by ‘;’ in alphabetical order and merged in one line.
    The WITHIN GROUP clause is omitted in the second query, so the out-of-order EmailAddress is separated by ‘;’ and merged in one line.

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tom Cooper 8,481 Reputation points
    2021-01-07T17:38:05.477+00:00

    WITHIN GROUP is used to specify the order that the items in STRING_AGG will appear. For example, in your query if the GroupName "Headquarters" had three departments, "Sales", Marketing", and "Finance" the result would be "Finance, Marketing, Sales".

    If you don't use the WITHIN GROUP clause, then the order of the items is undetermined.

    Tom

    1 person found this answer helpful.
    0 comments No comments

  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-01-07T22:48:22.293+00:00

    WITHIN GROUP is a clause used with some aggregate and analytic function where you want an order-related result. For string_agg you use it to mandate that the list is sorted in a certain order. (And I am quite sure that I have explained this before.)

    WITHIN GROUP can also be used with the functions PERCENTILE_DISC and PERCENTILE_CONT. In fact, with these functions, the clause is mandatory.

    ... and before you ask what these functions do, please review your old threads. I am quite sure that I have explained these to you before when you were asking about median.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.