Solved

SQL Advanced Select

Posted on 2013-01-02
4
268 Views
Last Modified: 2013-01-02
trying to identify orders that contain a specific product (but only purchased with another product)

There are other fields in each table but are irrelevant for this example

Sample Data:

master table
----------------------
order_no | order_date | customer_id | amount

100 | 2013-01-01 | 500 | 20.00
101 | 2013-01-01 | 502 | 22.00
102 | 2013-01-01 | 504 | 21.00
103 | 2013-01-01 | 505 | 22.00

order_details table
------------------------------
orderdetails_id | order_no | product_desc

1 | 100 | prod1
2 | 100 | prod2
3 | 100 | prod3
4 | 101 | prod2
5 | 102 | prod1
6 | 103 | prod3
7 | 103 | prod2


I would like to get all records and the amount of products that contain prod2 (which was ONLY purchased with another product)

Expected results:

order_no | prod_count
100 | 3
103 | 2
0
Comment
Question by:xtreme109
  • 2
4 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 150 total points
ID: 38736850
Give this a whirl (air code, so modify to suit your needs)..

SELECT od.order_no, Count(od.product_desc) as details_count
FROM order_details od
   JOIN (SELECT DISTINCT order_no FROM order_details WHERE product_desc <> 'prod2') odc ON od.order_no = odc.order_no
WHERE product_desc = 'prod2'
GROUP BY order_no

The idea here is that you're JOINing on a table that has everything but prod2's, so if there is no prod2 in an order, the JOIN will not display the record.
0
 
LVL 13

Accepted Solution

by:
MrBullwinkle earned 350 total points
ID: 38736851
I'm not seeing that Master is needed at all?

SELECT Order_No, Count(*) As Prod_Count
FROM Order_Details
WHERE Order_No IN (SELECT Order_No FROM Order_Details WHERE Product_Desc ='prod2')
GROUP BY Order_No
HAVING Count(*)>1
0
 
LVL 13

Expert Comment

by:MrBullwinkle
ID: 38737031
Curious, to better my answers in the future, why was my solution given a B? I presume you only had to copy paste it?
0
 
LVL 2

Author Comment

by:xtreme109
ID: 38737145
I didnt realize that. sorry! The default grade was a 'b' shouldve been an A
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question