Re: Converting join'ed rows into a comma or space delimited list

From: Mike Christensen <mike(at)kitchenpc(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Converting join'ed rows into a comma or space delimited list
Date: 2010-08-12 08:16:31
Message-ID: AANLkTin7Avi16oCRSFsuv10kzVGKYtcXicX8PPwPf0HM@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Damn I'm the master at posting stuff then figuring it out like 5
seconds later.. Is there an approach better than this?

select RecipeId, Rating, array_to_string(ARRAY(select Tag from
RecipeTags where RecipeId = R.RecipeId), ' ') from Recipes R;

On Thu, Aug 12, 2010 at 1:12 AM, Mike Christensen <mike(at)kitchenpc(dot)com> wrote:
> I have the table recipes (ID/Name):
>
> 1 - Pancakes
> 2 - Chicken dish
> 3 - Tacos
>
> Then I have the table RecipeTags (RecipeID/Tag)
>
> 1 - A
> 1 - C
> 2 - A
> 2 - D
> 3 - E
> 3 - F
> 3 - G
>
> I want to query for all recipes, but join in the recipe tags.
> However, rather than having a row for each recipe tag, I want to
> convert the list of tags into a varchar string with a delimiter
> character.  Something like this:
>
> 1 - Pancakes - A C
> 2 - Chicken dish - A D
> 3 - Tacos - E F G
>
> Is this possible?  The query doesn't have to be fast, since it will
> only be run when my website loads for the first time, but I'm guessing
> SQL will do it at least as fast as I could do it on the middle tier.
> Thanks!
>
> Mike
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thom Brown 2010-08-12 08:37:17 Re: Converting join'ed rows into a comma or space delimited list
Previous Message Mike Christensen 2010-08-12 08:12:38 Converting join'ed rows into a comma or space delimited list