pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Axel Rau <Axel.Rau@Chaos1.DE>
Cc: pgsql-sql@postgresql.org
Subject: Re: Selecting count of details along with details columns
Date: Thu, 29 Sep 2005 09:30:47 +0100
Message-ID: <433BA637.7040106@archonet.com> (raw)
In-Reply-To: <8365994213849c93946e61ded7d12318@Chaos1.DE>
References: <8365994213849c93946e61ded7d12318@Chaos1.DE>

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



view thread (6+ messages)  latest in thread

Message-ID: <433BA637.7040106@archonet.com>
Permalink:  ../433BA637.7040106@archonet.com/
Also on:    postgresql.org/message-id/433BA637.7040106@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, Axel.Rau@Chaos1.DE
  Subject: Re: Selecting count of details along with details columns
  In-Reply-To: <433BA637.7040106@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox