Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 who got cake, who got ice cream, who got both?

Author  Topic 

pyrrhus_finch
Yak Posting Veteran

51 Posts

Posted - 2005-01-25 : 19:05:47
Please help, I'm stumped as to how to write these querys.

Crude Example Data

CustomerID|Purchase
1.........|ice cream
2.........|ice cream
3.........|cake
1.........|cake
1.........|cake
3.........|cake
2.........|ice cream

I need to know the following

# Customers who bought only ice cream:
# Customers who bought only cake:
# Customers who bought Ice cream and bought cake too:


How would I write this Query?
I haven't the fiantest idea.

Thanks very much.



robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-01-25 : 19:21:47
SELECT CustomerID, CASE WHEN Min(Purchase)=Max(Purchase) THEN Max(Purchase) + ' only' ELSE 'Both' END
FROM myTable
GROUP BY CustomerID
Go to Top of Page

pyrrhus_finch
Yak Posting Veteran

51 Posts

Posted - 2005-01-25 : 20:08:21
Ugh. I didn't mention I was using Access - keep forgetting its so different. Any idea how to write this in Access? thanks
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-01-25 : 20:34:03
Well, it's easier if you post Access questions in the MS Access forum, and (especially) NOT in the Transact-SQL forum.

SELECT CustomerID, Iif(Max(Purchase)=Min(Purchase), Max(Purchase) & " only", "Both") AS Purch
FROM myTable
GROUP BY CustomerID
Go to Top of Page

pyrrhus_finch
Yak Posting Veteran

51 Posts

Posted - 2005-01-26 : 12:17:37
Hey thanks a ton! Learned something really usefull today of SQL and AccessQL.. ha! Really, thanks again! Your the Bomb!
Go to Top of Page
   

- Advertisement -