Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:36:37+00:00 2026-05-22T15:36:37+00:00

In PostgreSQL, there is this very useful string_agg function, that allows query like: SELECT

  • 0

In PostgreSQL, there is this very useful string_agg function, that allows query like:

SELECT
    contacts.first_name,
    contacts.last_name,
    (
        SELECT
            string_agg(number, ', ' ORDER BY phones.priority)
        FROM
            phones
        WHERE
            phones.contact_id = contacts.id
    ) AS phone_numbers
FROM
    contacts

Similarly, this can be done in MySQL using group_concat.

Now, I am trying to port this into SQL Server as a CLR User-Defined Aggregate. The code itself is not a problem, as there are thousands of examples all over the web to create this particular aggregate (which beg the question: why is it not part of SQL Server already??).

The problem is, I can’t find a clean way to apply the ORDER BY, since SQL Server not only doesn’t support ORDER BY within aggregate function, it also forbids the use of ORDER BY in subquery. My best bet is this:

SELECT
    contacts.first_name,
    contacts.last_name,
    (
        SELECT
            dbo.string_agg(number, ', ')
        FROM
            (
                SELECT TOP <some really large number>
                    number
                FROM
                    phones
                WHERE
                    phones.contact_id = contacts.id
                ORDER BY
                    phones.priority
            ) AS phones
    ) AS phone_numbers
FROM
    contacts

Is there a better workaround? And yes, I have read Is order by clause allowed in a subquery, and this is, dare I say, a valid use case of ORDER BY in subquery.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:36:37+00:00Added an answer on May 22, 2026 at 3:36 pm

    Looks like there is no better alternative than using a subquery with TOP <some really large number> to get around SQL Server’s limitation:

    SELECT
        contacts.first_name,
        contacts.last_name,
        (
            SELECT
                dbo.string_agg(number, ', ')
            FROM
                (
                    SELECT TOP <some really large number>
                        number
                    FROM
                        phones
                    WHERE
                        phones.contact_id = contacts.id
                    ORDER BY
                        phones.priority
                ) AS phones
        ) AS phone_numbers
    FROM
        contacts
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In postgresql a query in the querylog gets something like this: 2009-02-05 00:12:27 CET
Is there a PostgreSQL HA solution that can handle a splitbrain situation gracefully. To
is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks
is there a solution for batch insert via hibernate in partitioned postgresql table? currently
Does anyone know what substantial differences there are between the regular PostgreSQL and Postgres
Can anyone recommend for/against the time-travel functions in postgresql's contrib/spi module? Is there an
In Postgresql you can create additional Aggregate Functions with CREATE AGGREGATE name(...); But this
I've been using PostgreSQL a little bit lately, and one of the things that
Is there a way to make psycopg and postgres deal with errors without having
In PostgreSQL 8.3 database I have bookings table referencing booking_transactions table by ID. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.