Home Forums SQL Server 2008 T-SQL (SS2K8) how to merge one column values with same primary key RE: how to merge one column values with same primary key

  • Something like this should work.

    Notice how I posted ddl and sample data in a consumable format? That is something you should do in the future. It makes it easy for us volunteers to work on your issue. It will also help eliminate the need for you to beg us for help. 😎

    create table #Something

    (

    Model_No int,

    ModelName varchar(25)

    )

    insert #Something

    select 100, 'Zirrard' union all

    select 100, 'Modached'

    select Model_No, COUNT(*), case when COUNT(*) > 1 then 'Multi Model' else 'Single Model' end as ModelType,

    STUFF((select ', ' + ModelName

    from #Something s2

    where s2.Model_No = s.Model_No

    order by s2.ModelName

    for XML PATH('')), 1, 1, ' ')

    from #Something s

    group by Model_No

    drop table #Something

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/