Re: Selecting count of details along with details columns

From: Axel Rau <Axel(dot)Rau(at)Chaos1(dot)DE>
To: Richard Huxton <dev(at)archonet(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting count of details along with details columns
Date: 2005-09-29 19:08:15
Message-ID: 409f1e679d5f8744e1609d5c447d5f15@Chaos1.DE
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 29.09.2005 um 12:03 schrieb Richard Huxton:

> Axel Rau wrote:
>> Am 29.09.2005 um 10:30 schrieb Richard Huxton:
>>> Axel Rau wrote:
>>>
>>>>
...
> Ah - this is two questions:
> 1. What are the unique (t2_name,t1_name) pairings?
> 2. How many different (t1.id) values are there for each t2.
>
> So - something like:
>
> SELECT names.T2_name, names.T1_name, counts.num_t2
> FROM
> (
> SELECT DISTINCT T2.T2_name, T1.T1_name
> FROM T2,T1
> WHERE T2.id = T1.fk_t2
> ) AS names,
> (
> SELECT T2.T2_name, COUNT(T1.id) AS num_t2
> FROM T2, T1
> WHERE T2.id = T1.fk_t2
> GROUP BY T2.T2_name
> HAVING COUNT(T1.id) > 1
> ) AS counts
> WHERE
> names.T2_name = counts.T2_name
> ;
>
> You could write the "names" sub-query with a GROUP BY if you wanted of
> course.

Exactly, that query works as I expected. Thank you.
Can you answer this question as well:
> Looking for a workaround, I learned that aggregate functions are not
> allowed in WHERE clauses.
> Question: Is this conform with the standard?

Axel

Axel Rau, Frankfurt, Germany +49-69-951418-0
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iQEVAwUBQzw7n8Fz9+6bacTRAQIqnAf9EW7TS7K+cCf95fosagOcNhgQFuUvlyUr
yJpkXrv83+oKJ6kw6OcJxaEAkuiyRIiGQAlsVfc86itgKUQLfq6qpXEjeMD459kb
wIO01LV37akn9y3420h4Pmi1SDaZ63oUWJn48DhlUuuh5B7LHNyiOSMUKLU8ptLd
ZQ875uPo235bdqb15ibmZtwAuMGdsf3PPySBYMzvHzk7uZ+68b50QTmTPSU7VuPd
XtbZWdTK8q6+R3mhgz6k7DFaqTlTqzMimQevmwb1ADZZGVOOC0i77M1axYsCHarB
i2RT1CAcnNCX8MYc2nt8HS4j5KXpq7POFk3vdyAmVMwZ8WHNWJP2/w==
=CJSg
-----END PGP SIGNATURE-----

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Scott Marlowe 2005-09-29 19:29:57 Re: Selecting count of details along with details columns
Previous Message Mario Splivalo 2005-09-29 18:16:44 EXECUTE with SELECT INTO variable, or alternatives