473,809 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

should be easy query question

Tim
hi all,

I have a table of customers.

I have a table of products they have ordered.

How can I find all customers who have ordered productA and productB at
any time.

It sounds so easy, but I can't quite get my head around it!

thanks

Tim

Aug 18 '05 #1
7 1312
Jim
In article <11************ **********@g44g 2000cwa.googleg roups.com>, Tim wrote:
hi all,

I have a table of customers.

I have a table of products they have ordered.

How can I find all customers who have ordered productA and productB at
any time.


Do you mean productA AND productB, or productA OR productB?

In other words, do you want clients who have ordered both products, or
all customer who have ordered either product?

One's easy, the other is...less so.

Jim
--
Find me at http://www.ursaMinorBeta.co.uk
"There's no 'I' in team, but there is a 'me' if you jumble
the letters up a bit." - Dr. House.
Aug 18 '05 #2
Tim
not surprisingly, it's the difficult option I'm after where I want to
return all customers who have bought productA and productB.

thanks

Aug 18 '05 #3
Jim
In article <11************ **********@z14g 2000cwz.googleg roups.com>, Tim wrote:
not surprisingly, it's the difficult option I'm after where I want to
return all customers who have bought productA and productB.


Try something like:

select custid from table where product='Produc tA' and custid in
(select custid from table where product='Produc tB') order by custid

Can't promise it will work but a few tests of the results should prove
it one way or the other.

Jim
--
Find me at http://www.ursaMinorBeta.co.uk
"There's no 'I' in team, but there is a 'me' if you jumble
the letters up a bit." - Dr. House.
Aug 18 '05 #4
Stu
Without your table structures, this is only a guess;

SELECT *
FROM Customers
WHERE CustomerID IN (SELECT a.CustomerID
FROM (SELECT *
FROM products
WHERE p.Type = 'A') a
JOIN
(SELECT *
FROM products
WHERE p.Type = 'B') b
ON a.CustomerID = b.CustomerID)

is one way to do it.

Stu

Aug 18 '05 #5
Tim
yes I can see that would work. thanks.
but just for fun, how would that look if I wanted to compare 3, 4 or
even 10 products?!
does it just turn into a nasty mess?

I was half wondering whether to run a script now and then to flatten
the tables into a new table to string all products purchased into 1
field, and use the instring function to find what I'm looking for.

Aug 18 '05 #6
On 18 Aug 2005 08:27:30 -0700, Tim wrote:
hi all,

I have a table of customers.

I have a table of products they have ordered.

How can I find all customers who have ordered productA and productB at
any time.

It sounds so easy, but I can't quite get my head around it!

thanks

Tim


Hi Tim,

Here's a version that's both shorter and more expandable than Jim's
suggestion:

SELECT Customer
FROM Orders
WHERE Product IN ('A', 'B')
GROUP BY Customer
HAVING COUNT(*) = 2

Easy to expand to three, four, ... products. And if you ahve the
products you want to search for in a seperate table, the query changes
to one of the following two (they are equivalent, but might have
different execution plans - test both if speed matters)

SELECT Customer
FROM Orders
WHERE Product IN (SELECT Product FROM ProductsToFind)
GROUP BY Customer
HAVING COUNT(*) = (SELECT COUNT(*) FROM ProductsToFind)

SELECT Customer
FROM Orders
WHERE EXISTS
(SELECT *
FROM ProductsToFind
WHERE ProductsToFind. Product = Orders.Product)
GROUP BY Customer
HAVING COUNT(*) = (SELECT COUNT(*) FROM ProductsToFind)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Aug 18 '05 #7
Stu
Hugo,

That's awesome. I knew there had to be a better way to do that than my
lousy suggestion, and that's really clean.

Stu

Aug 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1374
by: | last post by:
I don't know SQL at all, but I have a problem now because I must use SQL in my PHP scripts. So please help me! I have 2 tables: id | name ----------- 1 | thing1 2 | thing2 3 | thing3 4 | thing4
3
2206
by: Andy Visniewski | last post by:
Should be easy, but I've been trying to figure this out for about half an hour with no luck. There is a table 'Cybex' which has all the Cybex products we sell, and a table 'Datasheets' which contains a list of the datasheets available for products. Both tables have a PartNumber column. On with the problem. If i do SELECT * FROM Cybex, it lists the 1000 or so products. and then SELECT * FROM DataSheets, it lists the 800 datasheets we...
4
2732
by: Andy Proctor | last post by:
I hope there is an answer out there.... I have a simple database structured like this (non relevant tables and fields omitted) Members table memberID memberFname memberLname memberNokID
36
6582
by: No Spam | last post by:
Dear fellow Access 2003 Users, Is there a way to trim all of the fields in a table in one swoop using VBA (preferred) or a query? Right now, I am using an update query and updating EACH field to it's trimmed counterpart. Any ideas? Thanks! Kevin
2
2314
by: Salsa_Dancer | last post by:
Hi forum, I have this constructor inside a class that derives from webcontrol public example() { for(int i=0;int<num;i++) { //MyGroup is an array of TextBoxes Property inside
5
1535
by: LedZep | last post by:
What up, All I need to do is enter a last name in a text box, query a MSAccess database and display the name with the corresponding columns. This is no problem, but when there are more than one records with the same last name, I need to click a command button to display the next record with that name. I cant find it in any of my books and it sounds like an easy enough question. Any help is definately appreciated. TIA,
8
2922
by: bsimmons01 | last post by:
Hi All, I'm completely green to DB2, so please pardon my ignorance if this is an extremely easy question. I'm converting some queries in our application and it's choking on the Group-By clause. Here's the query (part of it): SELECT a.org_lc || b.oros_rsrc || 'M' as ref_no, b.activity_desc,
3
1159
by: Chad Dokmanovich | last post by:
As someone who's had a "fair" amount of web development experience, I shocked that I can't answer this question myself: Q: How does one maintain state and pass data across web pages and do so in a secured manner? Before you answer, let me assure you that I familiar with the normal methods 1- Passing data in the Query String 2 - Hidden form fields
4
2026
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class, a random number of students arrive, each of which has completed a random number of lessons taken at random from the curriculum.
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.