Re: Difficulties with a master-detail query

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: milen(at)radev(dot)net
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Difficulties with a master-detail query
Date: 2005-09-23 14:13:16
Message-ID: 20050923141316.GB2298@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Sep 19, 2005 at 20:29:40 +0300,
"Milen A. Radev" <mradev(at)gmail(dot)com> wrote:
>
> I would like to get all employees, who speak two specified languages
> (say german and french). The following query gives me that, bu I don't
> like it (see for yourself):
>
>
> SELECT
> ?.employee_id,
> ?.name,
> COUNT(s.skill_id)
> FROM
> employee AS e
> INNER JOIN
> employee_skill AS es
> ON
> e.employee_id=es.employee_id
> INNER JOIN
> skill AS s
> ON
> s.skill_id=es.skill_id AND s.skill_id IN (1, 2)
> GROUP BY
> e.employee_id,
> e.name
> HAVING
> COUNT(s.skill_id)>=2;
>
>
> Here "(1, 2)" are the IDs for those predefined two languages, got from
> the "skill" table. ?nd that two in "COUNT(s.skill_id)>=2" is there
> because the count of the languages.
>
>
> Any ideas for simpler and more universal query?

That seems like a reasonable way to do things. The other way to do this is
to join the employee_skills table once for each skill. However, I expect
the above approach to be faster.

If you want to use skill names you can join against the skill table.

I don't think is an automatic way to tie the count() comparison to the number
of different skills being checked. But if you are doing this through an app
it already has to generate the IN list, so it shouldn't be hard to make it
figure out what to use to campare the count to.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-09-23 14:40:14 Re: Where are user defined functions stored?
Previous Message Hilary Forbes 2005-09-23 14:09:52 Where are user defined functions stored?