![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Database consists of the following 4 tables with respective attributes: CUSTOMER(C#, CUSTOMER NAME, ADDRESS), the key is [C#] ITEM(I#, ITEM NAME, MANUFACTURER, YEAR), the key is [I#] BOUGHT(C#, I#, DATE, QUANTITY), the key is [C#, I#,DATE] PREFER(I#, C#), the key is [I#, C#] I'm trying to construct the following query (in SQL) List of customers that bought all the items that John prefers. I can get the list of all the items that John prefers, but I'm not sure how to check that list against customers who bought ALL those items. I'm assuming it's either a division or some sort of subtraction but I'm not sure how to formulate the SQL query. |
#3
| |||
| |||
|
|
(tizmagik (AT) gmail (DOT) com) writes: Database consists of the following 4 tables with respective attributes: CUSTOMER(C#, CUSTOMER NAME, ADDRESS), the key is [C#] ITEM(I#, ITEM NAME, MANUFACTURER, YEAR), the key is [I#] BOUGHT(C#, I#, DATE, QUANTITY), the key is [C#, I#,DATE] PREFER(I#, C#), the key is [I#, C#] I'm trying to construct the following query (in SQL) List of customers that bought all the items that John prefers. I can get the list of all the items that John prefers, but I'm not sure how to check that list against customers who bought ALL those items. I'm assuming it's either a division or some sort of subtraction but I'm not sure how to formulate the SQL query. This smells of class assignment, but OK, let's go for it anyway. If memory serves this is something they for some reason I've never understood call relational division. In less occluded terms, a HAVING clause can shortcut the need for a couple of EXISTS and NOT EXISTS. SELKCT C.C#, C.CUSTOMER_NAME FROM CUSTOMER C JOIN (SELECT B.C# FROM BOUGHT B GROUP BY B.C# HAVING COUNT(DISTINCT B.I#) = (SELECT COUNT(*) FROM PREFER P JOIN CUSTOMER C ON P.C# = C.C# WHERE C.CUSTOMER_NAME = 'John')) AS res ON C.C# = res.C# |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
Thank you Ed, seems to be what I'm looking for, it's interesting, I never even though of setting up a Count, but now that I look at it, it's hard to imagine any other way of doing it. |
#6
| |||
| |||
|
|
Erland: Why would it matter if it's a class assignment or not? |
|
Is not the purpose of a Usenet group to share and learn from each other? |
|
What relevance is it what the knowledge will be used for? Thank you for your attempt anyway, but Ed's answer seems more in line with what the query is intended to do. Thank you Ed, seems to be what I'm looking for, it's interesting, I never even though of setting up a Count, but now that I look at it, it's hard to imagine any other way of doing it. Thanks again ![]() |
#7
| |||
| |||
|
|
Why would it matter if it's a class assignment or not? Is not the purpose of a Usenet group to share and learn from each other? |
#8
| |||
| |||
|
|
Why would it matter if it's a class assignment or not? Is not the purpose of a Usenet group to share and learn from each other? In most university systems having someone else do your homework gets you kicked out of school. It is academic fraud. I know. I have had two kids expelled from schools in New Zealand and Australia for doing this. An old friend of mine got a "social engineer" taken out of Georgia Tech; etc. |
#9
| |||
| |||
|
|
Erland: Why would it matter if it's a class assignment or not? Is not the purpose of a Usenet group to share and learn from each other? |
|
Thank you Ed, seems to be what I'm looking for, it's interesting, I never even though of setting up a Count, but now that I look at it, it's hard to imagine any other way of doing it. |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |