Re: Query Output Formatting - Y/N Depending On Count

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: "Cameron B(dot) Prince" <cprince(at)rideware(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Query Output Formatting - Y/N Depending On Count
Date: 2003-05-24 01:09:22
Message-ID: 20030524010922.GA9578@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, May 23, 2003 at 03:06:13PM -0500, Cameron B. Prince wrote:
> Hi,
>
> I need to output a Y or N depending on a count being greater than 0 or not.
> I think I've seen a co-worker at a pervious job do something like this, but
> I am unable to find any examples on the list or in the docs.

Use a CASE statement:

CASE WHEN COUNT(DISTINCT b.col) = 0 THEN 'N' ELSE 'Y' END

> Here's my query:
>
> SELECT a.col, COUNT(DISTINCT b.col) AS col_count
> FROM table1 a
> LEFT OUTER JOIN table2 b
> ON a.col=b.col
> GROUP BY a.col
>
> So what I'm looking for is col_count to contain a Y if the count is greater
> than 0, else an N.
>
> Anyone know how to do this?
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> "the West won the world not by the superiority of its ideas or values or
> religion but rather by its superiority in applying organized violence.
> Westerners often forget this fact, non-Westerners never do."
> - Samuel P. Huntington

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Erik Ronström 2003-05-24 01:09:48 Error parsing
Previous Message Ed L. 2003-05-23 22:57:53 Building 7.3.2 on HP-UX w/out zlib/readline