tags:

views:

64

answers:

2

Suppose I have a log of Customers who come in on particular Days, like:

Cs  day
--  ---
01  Tue
02  Tue
03  Wed
01  Wed
04  Thu
02  Thu

I need a query that returns only the #s of those Customers who were in both on Tue and on Wed. In this case, only Cs # 01. Thank you.

+6  A: 
select distinct c1.Cs
from Customers c1 
inner join Customers c2 on c2.Cs=c1.Cs
where c2.day='Tue' and c1.day='Wed'
ʞɔıu
I threw a distinct in there, just in case Cs/day entries aren't unique.
Jeremy Stein
Excellent, thanks!
Nonny
A: 

And using subqueries...

Select distinct cs From Customers
Where Exists (Select * from Customers
              Where day = 'Tue')
  And Exists (Select * from Customers
              Where day = 'Wed')
Charles Bretana
You are missing one more condition with users id. Your query will return all records from the table.
Lukasz Lysik