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 7571257
In Process

The Archive Base Latest Questions

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

This is linked to my previous Qn Table A: ID Rank Name 1 100

  • 0

This is linked to my previous Qn

Table A: 
ID  Rank Name 
1   100  Name1 
1    45  Name2 
2    60  Name3 
2    42  Name4 
2    88 Name5 
3    50 name6
3    50 name7

Table B: 
ID FileName 
1  fn1 
2  fn2 
3  fn3

What I want is

1 fn1 name1 
2 fn2 name5 
3 fn3  name6,name7

Here is my code that doesnt deal with the duplciate ranks above, so I get two rows for value 3.

select B.*,A.Name
LEFT JOIN ( SELECT A.Id, MAX(A.Rank)as Rank 
    FROM A 
    GROUP BY A.Id
    ) AS NewA 
            JOIN A 
                on A.Rank = NewA.Rank
                    AND A.ID = NewA.Id
    on NewA.ID = B.ID 

How do I modify this to give me A.name seperated by comma when the ranks are same for a Id?

Thanks

  • 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-30T15:37:53+00:00Added an answer on May 30, 2026 at 3:37 pm

    You will need to use a CTE to flatten this out.

    This should plug directly into SQL and work with the data given as an example. Obviously, you will need to update the queries for your real data since this is probably not your real schema

    create table #tableA (ID int, [rank] int, name varchar(max))
    insert into #tableA values(1,100,'Name1')
    insert into #tableA values(1,45,'Name2')
    insert into #tableA values(2,60,'Name3')
    insert into #tableA values(2,42,'Name4')
    insert into #tableA values(2,88,'Name5')
    insert into #tableA values(3,50,'Name6')
    insert into #tableA values(3,50,'Name7')
    
    create table #tableB (ID int, [FileName] varchar(max))
    insert into #tableB values(1,'fn1')
    insert into #tableB values(2,'fn2')
    insert into #tableB values(3,'fn3')
    
    SELECT B.*,A.Name, ROW_NUMBER() OVER (PARTITION BY A.ID ORDER BY A.[rank]) AS RowNum
    INTO #RankedTable
    FROM #tableB as B
        LEFT JOIN ( 
            SELECT A.Id, MAX(A.Rank)as Rank 
            FROM #tableA AS A 
            GROUP BY A.Id
            ) AS NewA 
        JOIN #tableA AS A
            on A.Rank = NewA.Rank
                AND A.ID = NewA.Id
        on NewA.ID = B.ID 
    
    ;WITH ConcatenationCTE (ID, [FileName], Name, RowNum)
    AS
    (
      SELECT ID, [FileName], Name, RowNum 
      FROM #RankedTable
      WHERE RowNum = 1
      UNION ALL
      SELECT #RankedTable.ID, #RankedTable.[FileName],
              ConcatenationCTE.Name + ',' + #RankedTable.Name AS Name, 
              #RankedTable.RowNum 
      FROM #RankedTable
          JOIN ConcatenationCTE 
              ON ConcatenationCTE.ID = #RankedTable.ID 
                  AND ConcatenationCTE.RowNum +1 = #RankedTable.RowNum
    )
    SELECT ConcatenationCTE.ID, [FileName], Name 
    FROM ConcatenationCTE 
            JOIN 
         (SELECT ID, MAX(RowNum) AS RowNum 
          FROM ConcatenationCTE GROUP BY ID) AS FinalValues 
            ON FinalValues.ID = ConcatenationCTE.ID 
                AND FinalValues.RowNum = ConcatenationCTE.RowNum 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm storing linked lists of data in records that look like this: CREATE TABLE
This question is linked to my previous one ( posted as an anonymous user
This is linked to my previous post Wher I created a Struct : struct
This is linked to my other question when to move from a spreadsheet to
This is linked to a question I asked yesterday. Briefly, the problem I'm having
I have an exam tomorrow and I was trying to understand this doubly linked
This chart has been linked a number of times on blogs that I follow
This code is a linked list implementation of a stack that solves postfix problems
This method right below reverses a doubly linked list with n elements. I dont
This is the C code that frees memory of a singly linked list. It

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.