Re: Using AND in query

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Thomas Kellerer <spam_eater(at)gmx(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Using AND in query
Date: 2010-08-08 08:46:18
Message-ID: 46145F14-AB33-48DF-8E95-2290AC366E5F@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7 Aug 2010, at 23:18, Thomas Kellerer wrote:

> Or as an alternative:
>
> SELECT tid, purchase_date
> FROM orders
> WHERE item in ('Laptop', 'Desktop')
> GROUP BY tid, purchase_date
> HAVING count(*) = 2

This one is incorrect, it will also find people who bought two laptops or two desktops on the same date.

I was going to suggest:

SELECT tid, "date", 001::bit as type
FROM orders
WHERE item = 'Laptop'
UNION ALL
SELECT tid, "date", 010::bit as type
FROM orders
WHERE item = 'Desktop'
GROUP BY tid, "date"
HAVING type & 011::bit = 011::bit;

But I think David's solution is more readable, as it leaves the item names in tact.

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4c5e6ee4286211665369939!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John R Pierce 2010-08-08 08:55:19 Re: Using AND in query
Previous Message Thom Brown 2010-08-08 01:44:09 Re: Howto only select secific lines from a result?