Re: Grouped item in a subquery

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Steve Tucknott <steve(at)retsol(dot)co(dot)uk>
Cc: PostGreSQL <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Grouped item in a subquery
Date: 2005-06-23 17:18:20
Message-ID: 6497.1119547100@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Steve Tucknott <steve(at)retsol(dot)co(dot)uk> writes:
> A shorter/simpler example of what I think I'm saying:
> SELECT COUNT(*) AS count, SUBSTRING(lastName FROM 1 FOR 1) AS first,
> (SELECT COUNT(*)
> FROM productLevelDet AS pDet
> WHERE SUBSTRING(description FROM 1 FOR 1) = first) AS
> prod_count
> FROM customer
> GROUP BY first

I think you need an extra level of subselect:

SELECT ss.*,
(SELECT COUNT(*)
FROM productLevelDet AS pDet
WHERE SUBSTRING(description FROM 1 FOR 1) = ss.first) AS prod_count
FROM
(SELECT COUNT(*) AS count, SUBSTRING(lastName FROM 1 FOR 1) AS first,
FROM customer
GROUP BY first) ss;

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Jeremy Yager 2005-06-23 21:17:25 Connecting to Postgres via ADO/Data Environment in VB6
Previous Message Steve Tucknott 2005-06-23 16:45:43 Re: Grouped item in a subquery