Concat column values

  • I have following table

    DepartmentID Employee

    ------------- ---------

    1 John

    1 Michael

    2 Jenson

    2 Lily

    2 Lara

    I want the output in following format

    DepartmentID Employees

    ------------- ----------

    1 John,Michael

    2 Jenson,Lily,Lara

    Please provide examples

  • With the idea of "teaching a man to fish" in full swing, please see the following article.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    For future posts, please see the article at the first link in "Helpful Links" in my signature line below.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (9/15/2013)


    With the idea of "teaching a man to fish" in full swing, please see the following article.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    For future posts, please see the article at the first link in "Helpful Links" in my signature line below.

    Excellent advice Jeff! All you have to do is look at my avatar to see what it means to learn to fish correctly (as you have often taught me).


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (9/15/2013)


    Jeff Moden (9/15/2013)


    With the idea of "teaching a man to fish" in full swing, please see the following article.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    For future posts, please see the article at the first link in "Helpful Links" in my signature line below.

    Excellent advice Jeff! All you have to do is look at my avatar to see what it means to learn to fish correctly (as you have often taught me).

    Learning how to fish also increases the scalability of code. 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (9/15/2013)


    dwain.c (9/15/2013)


    Jeff Moden (9/15/2013)


    With the idea of "teaching a man to fish" in full swing, please see the following article.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    For future posts, please see the article at the first link in "Helpful Links" in my signature line below.

    Excellent advice Jeff! All you have to do is look at my avatar to see what it means to learn to fish correctly (as you have often taught me).

    Learning how to fish also increases the scalability of code. 😛

    And greatly reduces hunger too. 😉

    _______________________________________________________________

    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/

  • Sean Lange (9/16/2013)


    Jeff Moden (9/15/2013)


    dwain.c (9/15/2013)


    Jeff Moden (9/15/2013)


    With the idea of "teaching a man to fish" in full swing, please see the following article.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    For future posts, please see the article at the first link in "Helpful Links" in my signature line below.

    Excellent advice Jeff! All you have to do is look at my avatar to see what it means to learn to fish correctly (as you have often taught me).

    Learning how to fish also increases the scalability of code. 😛

    And greatly reduces hunger too. 😉

    I had a finny feeling you'd say that.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply