Re: Dumb question about count()

From: Ezequiel Tolnay <mail(at)etolnay(dot)com(dot)ar>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Dumb question about count()
Date: 2005-07-22 08:49:00
Message-ID: dbqbqj$j0p$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Benjamin Smith wrote:
> Now, I want to get a result like:
>
> classroom | students | seats
> 101A 0 25
> 101B 22 30
> 102A 11 0
> ... etc.
>
> Something somewhat akin to
>
> select classroom.title,
> count(students.id) AS students,
> count(seats.id) AS seats
> from classrooms, students, seats
> where classrooms.id=students.classrooms_id
> and classrooms.id=seats.id

try

select classroom.title,
(select count(*) from students
where classrooms_id = c.id) AS students,
(select count(*) from seats
where classrooms_id = c.id) AS students,
count(seats.id) AS seats
from classrooms c

Cheers,

Eze

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2005-07-22 08:51:39 Re: Bad locking with MS-Access
Previous Message Kilian Hagemann 2005-07-22 08:46:39 Re: Query planner refuses to use index