Re: Grouped item in a subquery - Mailing list pgsql-novice

From Tom Lane
Subject Re: Grouped item in a subquery
Date
Msg-id 6497.1119547100@sss.pgh.pa.us
Whole thread Raw
In response to Re: Grouped item in a subquery  (Steve Tucknott <steve@retsol.co.uk>)
List pgsql-novice
Steve Tucknott <steve@retsol.co.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

pgsql-novice by date:

Previous
From: Steve Tucknott
Date:
Subject: Re: Grouped item in a subquery
Next
From: "Jeremy Yager"
Date:
Subject: Connecting to Postgres via ADO/Data Environment in VB6