Query to generate a comma separated field

  • I have a requirement where for a given student I need to generate a comma separated field of the courses he/she is taking.

    Input

    Data Course

    Student1 crs1

    Student1 crs2

    Output required is

    Student1 crs1,crs2

    Can I write this query without using functions.

  • rosemary (2/5/2008)


    I have a requirement where for a given student I need to generate a comma separated field of the courses he/she is taking.

    Input

    Data Course

    Student1 crs1

    Student1 crs2

    Output required is

    Student1 crs1,crs2

    Can I write this query without using functions.

    Under SQL Server 2000 you would need to use a function. However, on 2005 you could use SELECT FOR XML PATH in the following way:

    SELECT t1.studentID

    , STUFF(( SELECT DISTINCT TOP 100 PERCENT

    ',' + CAST(t2.Course AS NVARCHAR)

    FROM mytable AS t2

    WHERE t1.studentID = t2.studentID

    ORDER BY ',' + CAST(t2.Course AS NVARCHAR)

    FOR

    XML PATH('')

    ), 1, 1, '')

    FROM mytable AS t1

    GROUP BY t1.studentID

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • I need it in sql server 2000.

    But thanks!!!

  • The easiest way for you to solve it yourself is to look up cursors and then just build up a string as you loop over the rows. I guess performance isn't your key criteria here so that'll be ok (ie it will operate in 0.5 seconds for a typical dataset rather than 0.1 seconds - whoopee 😀 )

  • On the contrary performance is the key criteria here. I guess the data I have provided is quite simplistic to assume that. I know how to get this done without cursors (use function) . But client requirements doesnt allow me to use functions. 🙁

  • Does the client allow you to use temp tables?

    --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)

  • Nope no temp tables. I guess will have to rely on the front end to get this done.

  • When you say no functions does that include system functions as well?

    If system functions are allowed have a look at this article: Using COALESCE to Build Comma-Delimited String

  • The query used in the article is what I was planning to use in the function. My problem is that the client doesnt want to be database centric, though the current database is Sql Server 2000. As I said I guess I will have to rely on the front end to get this done at the risk of a big perfomance hit.

  • Tough client...

    --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)

  • Hi Andras

    Its more usefull for me,

    Cheers

    Sujith

  • If you know all the potential course names you could play this game...

    Use Tempdb

    if object_id('dbo.#Course') is not Null Drop table dbo.#Course

    Select 'John'[Student],'Math'[Course] into dbo.#Course

    Union All Select 'Steve','Science'

    Union All Select 'Don','Math'

    Union All Select 'Don','English'

    Union All Select 'John','English'

    Union All Select 'Steve','History'

    Select * from dbo.#Course

    Select

    Student,

    Left([Course List],Len([Course List])-1)[Course List]

    from

    (

    Select

    Student,

    Max(Case Course when 'Math' then 'Math,' else '' End)+

    Max(Case Course when 'Science' then 'Science,' else '' End)+

    Max(Case Course when 'English' then 'English,' else ''End)+

    Max(Case Course when 'History' then 'History,' else ''End)[Course List]

    from dbo.#Course

    group by Student

    ) t



    PeteK
    I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.

Viewing 12 posts - 1 through 11 (of 11 total)

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