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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:07:56+00:00 2026-05-23T19:07:56+00:00

I have the following table: create table #prd ( ver varchar(20), fam varchar(20), mm

  • 0

I have the following table:

create table #prd
(
  ver varchar(20),
  fam varchar(20),
  mm varchar(20),
)

insert into #prd values('dt','fam1','111')
insert into #prd values('dt','fam1','222')
insert into #prd values('mb','fam1','333')
insert into #prd values('mb','fam2','444')

I need the output as below using CTE:

ver       family       MM
-------   ---------    ----------    
DT          fam1         111,222  
mb          fam1         333  
mb          fam2         444

Please help!!

  • 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-23T19:07:57+00:00Added an answer on May 23, 2026 at 7:07 pm

    A CTE version that does not use for xml path('')

    ;with cte1 as
    (
      select *,
             row_number() over(partition by ver, fam order by mm) as rn
      from #prd       
    ),
    cte2 as
    (
      select ver,
             fam,
             cast(mm as varchar(max)) as mm,
             rn
      from cte1
      where rn = 1
      union all
      select c1.ver,
             c1.fam,
             c2.mm+','+c1.mm,
             c1.rn
      from cte1 as c1
        inner join cte2 as c2
          on c1.rn = c2.rn + 1 and
             c1.ver = c2.ver and
             c1.fam = c2.fam
    ),
    cte3 as
    (
      select ver,
             fam,
             mm,
             row_number() over(partition by ver, fam order by rn desc) as rn 
      from cte2
    )
    select ver,
           fam,
           mm
    from cte3
    where rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table: CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1)) INSERT INTO
I have the following table CREATE TABLE `prod_prices` ( `id` varchar(32) COLLATE utf8_unicode_ci NOT
I have the following table and values: create table TreeTable ( ID int primary
I have the following table: $sql = CREATE TABLE received_queries ( sender_screen_name varchar(50), text
I have the following table: CREATE TABLE [dbo].[TableB]( [id] [int] NULL, [FileName] [varchar](20) NULL
Let's assume that I have the following table: CREATE TABLE [dbo].[PricesHist] ( [Product] [varchar](6)
I have the following table: create table tbl ( id int identity(1,1), val varchar(100)
I have the following table: CREATE TABLE cities ( city_id INT, city_name VARCHAR(255), --
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have the following table/indexes - CREATE TABLE test ( coords geography(Point,4326), user_id varchar(50),

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.