locked
I need to help for sql query RRS feed

  • Question

  • I have a table as follows.

    MyValue, UserId

    12, 1

    13, 1

    13, 2

    14, 1

    15, 3

    12, 2

    I want to result as follows

    UserId, MyValues

    1, 12-13-14

    2, 12-13

    3, 15

    How do I do this?


    oguzkurtcuoglu.com



    Wednesday, June 3, 2015 11:37 AM

Answers

  • try this

    declare @Test Table(sno int,scity varchar(20))
    Insert @Test(sno,scity)
    Values
    (1,12),(1,13),(1,14)
    
    select distinct sno ,
    STUFF((Select '-'+Scity
    from @Test T1
    where T1.sno=T2.sno
    FOR XML PATH('')),1,1,'') from @Test T2


    Hope it Helps!!

    Wednesday, June 3, 2015 11:41 AM

All replies

  • try this

    declare @Test Table(sno int,scity varchar(20))
    Insert @Test(sno,scity)
    Values
    (1,12),(1,13),(1,14)
    
    select distinct sno ,
    STUFF((Select '-'+Scity
    from @Test T1
    where T1.sno=T2.sno
    FOR XML PATH('')),1,1,'') from @Test T2


    Hope it Helps!!

    Wednesday, June 3, 2015 11:41 AM
  • try this

    declare @Test Table(sno int,scity varchar(20))
    Insert @Test(sno,scity)
    Values
    (1,12),(1,13),(1,14)
    
    select distinct sno ,
    STUFF((Select '-'+Scity
    from @Test T1
    where T1.sno=T2.sno
    FOR XML PATH('')),1,1,'') from @Test T2


    Hope it Helps!!

    Very thanks @Stan210 :)

    oguzkurtcuoglu.com


    Wednesday, June 3, 2015 11:55 AM