简体   繁体   中英

I need to group 2 rows two 1 and concatenate values of one column in SQL SERVER 2008

so this is the situation,i have this table with multiple records but a few records have the same values with only one column being different

ID  NAME    SELCODE RANGE
111 DANIEL  123123  YES
111 DANIEL  123123  NO

i want to merge this difference into one row but with both values from the RANGE column, as follows.

ID  NAME    SELCODE RANGE
111 DANIEL  123123  YES/NO

There are definitely a lot of examples of this around but here is one way you can do it:

SELECT
    DISTINCT
    t.ID
    ,t.Name
    ,t.SELCODE
    ,STUFF(
        (SELECT '/' + RANGE
        FROM
            Table t2
        WHERE
          t.Id = t2.ID
          AND t.Name = t2.Name
          AND t.SELCODE = t2.SELCODE
        FOR XML PATH(''))

        ,1,1,'') as RANGE
FROM
    Table t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM