Hi all,
I am learning SQL, and am working on a practice database here at work. The orders table in this database has two fields I am currently working with; Order ID and Shopper ID. For a given Order ID, there may be multiple records (multiple items ordered), but each record should have the same Shopper ID.
I was able to go through and force some incorrect Shopper ID's into some records for a few order ID's. Now I want to write a query that will locate each of these Order ID's and associated Shopper ID's.
I am unsure of how to proceed. I guess I have to evaluate every Order ID in the database, find the min(shopper_id) and max(shopper_id) and compare those two values. When they are the same, ignore this order ID. If the two values are not identical, I need to return this order_id and all shopper_id's associated with it.
Can someone help me finish my statement to do this? The statement below will return a list of all Order_ID's, and the Min and Max Shopper_ID for each Order_ID, on the date of 05/25/2004. Now, how do I get the SQL statement to compare the Min and Max values and only return records where they don't match?
Code:
SELECT Order_Date, Order_ID, min(Shopper_ID) as Min_ID, max(Shopper_ID) as Max_ID
FROM Order_Detail
WHERE (Order_Date = {d '2004-05-25'})
GROUP BY Order_Date, Order_ID