Re: Selecting count of details along with details columns - Mailing list pgsql-sql

From Richard Huxton
Subject Re: Selecting count of details along with details columns
Date
Msg-id 433BA637.7040106@archonet.com
Whole thread Raw
In response to Selecting count of details along with details columns  (Axel Rau <Axel.Rau@Chaos1.DE>)
Responses Re: Selecting count of details along with details columns
List pgsql-sql
Axel Rau wrote:
> 
> SELECT T2.T2_name, COUNT(T1.id) AS xx
>     FROM T2, T1
>     WHERE T2.id = T1.fk_t2
>     GROUP BY T2.T2_name
>     HAVING COUNT(T1.id) > 1
>     ORDER BY xx DESC;
> 
>  t2_name | xx
> ---------+----
>  T2-N2   |  3
>  T2-N3   |  2
> (2 rows)
> 
> Adding column t1_name to the result set breaks COUNT(T1.id):
> 
> SELECT T2.T2_name, T1.T1_name, COUNT(T1.id) AS xx
>     FROM T2, T1
>     WHERE T2.id = T1.fk_t2
>     GROUP BY T2.T2_name, T1.T1_name
>     HAVING COUNT(T1.id) > 1
>     ORDER BY xx DESC;
> 
>  t2_name | t1_name | xx
> ---------+---------+----
> (0 rows)
> 
> How can I do this with pg ?

Do what? You don't say what results you are expecting.

Do you want:
1. ALL values of T1_name (in which case what count do you want)?
2. The FIRST value of T1_name (in which case what do you mean by first)?

--   Richard Huxton  Archonet Ltd


pgsql-sql by date:

Previous
From: Mario Splivalo
Date:
Subject: Re: Sending function parametars within EXECUTE ''SELECT...
Next
From: Axel Rau
Date:
Subject: Re: Selecting count of details along with details columns