Re: SQL Intersect like problem - Mailing list pgsql-novice

From Bruno Wolff III
Subject Re: SQL Intersect like problem
Date
Msg-id 20030205163951.GA2254@wolff.to
Whole thread Raw
In response to SQL Intersect like problem  ("Dinesh V" <dinesh@trailblazingsolutions.com>)
Responses Re: SQL Intersect like problem
List pgsql-novice
On Wed, Feb 05, 2003 at 10:17:09 -0500,
  Dinesh Visweswaraiah <dinesh@trailblazingsolutions.com> wrote:
> Bruno,
>
> I am sorry for being very clear.  The need is to identify an InvoiceId
> from a *set* of ProductId and Quantity values.  A simple join will
> not work because it is not one value of ProductId and one value of
> Quantity.

If you want all invoices that have those quantities of products, even if
there are other products, then you can just use intersect.
select invoiceid from invoicetable where productid = 'PID1' and
  quantity = 'Q1'
intersect
select invoiceid from invoicetable where productid = 'PID2' and
  quantity = 'Q2'
intersect
select invoiceid from invoicetable where productid = 'PID3' and
  quantity = 'Q3';

If you need an exact match than you can use set difference to rule out
invoices with other product and quantity tuples.
For example, you could append the following to the above query.
except
select invoiceid from invoicetable group by invoiceid having count(*) > 3;

P.S. You normally want to keep the list copied on discussions related to your
question, rather than just replying invidiually to people that try to answer
your question.

pgsql-novice by date:

Previous
From: Wim
Date:
Subject: Postgres performace with large tables.
Next
From: Petre Scheie
Date:
Subject: Re: PL/Perl on HPUX