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

  • SEARCH
  • Home
  • 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 8777595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:18:47+00:00 2026-06-13T19:18:47+00:00

I am using MS-SQL and I have a table like this: Count Code 1

  • 0

I am using MS-SQL and I have a table like this:

Count  Code
1      A
2      C
3      A
4      B
5      C
6      B

I need to only to get Distinct Row of the Code Column but I need to keep the Order of the Column to create a Comma Delimited String like this:

    A, C, B

I have tried the following code to not get an SQL Error of Msg 145, Level 15 – ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

SELECT @tlist = ISNULL(@tlist+', ','') + Code 
FROM (SELECT DISTINCT t.Code  
     FROM @Table t) 

but I get an output of A, B, C

  • 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-06-13T19:18:48+00:00Added an answer on June 13, 2026 at 7:18 pm

    So, you don’t want distinct. You want group by:

    select t.code
    from @Table t
    group by t.code
    order by min(count)
    

    You can order by columns not mentioned in the select clause.

    To get the comma delimited list as a single variable, use this technique:

    select stuff((select ', '+t.code
                  from @Table t
                  group by t.code
                  order by min(count)
                  for xml path ('')
                 ), 1, 2, '')
    

    Order by in a subquery is generally undefined. And, your method of concatenating the list together in order is not guaranteed to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a section of T-SQL code that looks something like this: select @automation_rate
I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
[using SQL Server 2005] I have a table full of users, I want to
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
I'm using SQL Server 2008 and I have a table with three columns: Length
I'm using SQL Server 2008 and I have a table that has a 'Status'
I'm using SQL Server 2008. I have a table with x amount of rows.
I am using SQL Server 2008 spatial data types. I have a table with
I am using SQL Server CE database and C# language. I have a table
I am using T/SQL in SQL Server 2008 I have a table MyTable with

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.