how can get below output?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vasavivenu
    New Member
    • Feb 2011
    • 37

    how can get below output?

    Hi,

    I have a table student having two columns name and course-Id, if one student having more then one course-Id.
    Ex:
    name course-Id
    venu c
    venu c++
    venu v.B

    then i need a o/p as
    name course-ID
    venu c,c++,V.B

    Thanks in Advance,
    Vasavivenu.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You'll have to use a cursor so that you can concatenate all the course IDs together.

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      Here, try to tweak this...

      Happy Coding!!!

      ~~ CK

      Comment

      • vasavivenu
        New Member
        • Feb 2011
        • 37

        #4
        I find my self as

        SELECT name,
        MAX( CASE no WHEN 1 THEN no ELSE '' END ) + ', ' +
        MAX( CASE no WHEN 2 THEN no ELSE '' END ) + ', ' +
        MAX( CASE no WHEN 3 THEN no ELSE '' END )

        FROM venus
        GROUP BY name ;

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          That will work if a student has no more than 3 classes. If they take more or a future student ends up taking more, it will not work.

          Comment

          Working...