Re: IN vs EXIIST

From: Jochem van Dieten <jochemd(at)oli(dot)tudelft(dot)nl>
To: Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: IN vs EXIIST
Date: 2002-09-19 11:58:58
Message-ID: 1032436738.3d89bc0256a9a@webmail.oli.tudelft.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Quoting Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp>:
>
> Jochem van Dieten wrote:
> >
> > SELECT key1, Min(CASE WHEN x THEN 1 ELSE 0 END) AS isTrue
> > FROM table
> > GROUP BY key1
> > HAVING isTrue = 1
>
> I tried this but got an error:
> psql TMP -c "select invoice_id, min(case when received then 1 else 0
> end) as ok from invoice_li group by invoice_id having ok = 1"
> ERROR: Attribute 'ok' not found

Sorry, not behind the console at the moment. Try either:

SELECT invoice_id
FROM (
SELECT invoice_id, Min(CASE WHEN received THEN 1 ELSE 0 END) AS ok
FROM invoice_li
GROUP BY invoice_id
) agg_invoices
WHERE ok = 1

or:

SELECT invoice_id, Min(CASE WHEN received THEN 1 ELSE 0 END) AS ok
FROM invoice_li
GROUP BY invoice_id
HAVING Min(CASE WHEN received THEN 1 ELSE 0 END) = 1

Make sure you have an index on invoice_id. I am not sure if it is
faster because of the case that needs to be done on each row, but I
think it is worth a shot.

Jochem

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ben-Nes Michael 2002-09-19 12:06:42 pg_dump in 7.1.3 and migration to 7.2.2
Previous Message Jean-Christian Imbeault 2002-09-19 11:35:39 Re: IN vs EXIIST