A GROUP BY question

  • I have data that looks like this:

    artID title catid

    53Maximixing Building Performance18

    53Maximixing Building Performance22

    53Maximixing Building Performance25

    53Maximixing Building Performance69

    53Maximixing Building Performance101

    53Maximixing Building Performance110

    53Maximixing Building Performance117

    53Maximixing Building Performance207

    82Total Energy Retrofit 24

    82Total Energy Retrofit 27

    82Total Energy Retrofit 100

    82Total Energy Retrofit 101

    82Total Energy Retrofit 102

    82Total Energy Retrofit 111

    I want to process this data and have it look like this:

    artID title catid

    53Maximixing Building Performance18, 22, 25, 69, 101, 110, 117, 207

    82Total Energy Retrofit 24, 27, 100, 101, 102, 111

    Any tips, hints, or suggestions would be appreciated. Thank you.

  • Reply removed since it did not answer the question properly ... sorry about that

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • I think this thread will provide you with what you need to solve your problem.

    Let us know if it helps.

  • I do not know your table structure, but you can do something like the following.

    --function to concatenate categories into comma seperated list by title

    CREATE FUNCTION dbo.fn_GetCategoriesForTitle (@TitleId int )

    RETURNS varchar(MAX)

    AS

    BEGIN

    DECLARE @Categories varchar(max)

    SELECT @Categories = COALESCE(@Categories + ', ', '') + R.CatId

    FROM (SELECT DISTINCT

    CAST(CatId as varchar) as CatId

    FROM dbo.TitleCategories

    WHERE TitleId = @TitleId ) R

    RETURN @Categories

    END

    GO

    --return titles and comma seperated list of categories

    SELECT

    T.ArtId,

    T.Title,

    dbo.fn_GetCategoriesForTitle(T.TitleId) as CatId

    FROM dbo.Titles

  • Thanks this function worked. Of course, I had to change it a bit but it is one of those lessons learned for future use. Again, Thank you.

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

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