If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Set Intersection Finding Only Exact Matches

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-28-06, 11:28
mmbosman mmbosman is offline
Registered User
 
Join Date: Apr 2004
Posts: 171
Set Intersection Finding Only Exact Matches

I have the following problem - I've got a table full of shipments and another full of shipment detail lines which are the delivery locations for each shipment. Each location has a city which is also a key to another table full of cities that we ship to.

I also have a table full of rates with rate detail lines for each city included in the rate. The rate detail lines have a key to the cities table as well.

My problem is this when a shipment goes out I need to find the matching rate. I need to find the rate with the exact list of matching cities as the shipment for a given carrier. This is what I've got so far.

SELECT distinct [Rate Details].rate_key
FROM [Rate Details]
WHERE EXISTS

(SELECT Cities.city_key
FROM Cities INNER JOIN (Locations INNER JOIN [Runs Locations Carrier Out] ON Locations.Location =
[Runs Locations Carrier Out].Location) ON Cities.city_key = Locations.City_key

WHERE [Runs Locations Carrier Out].[Run Key] =2 AND Cities.city_key = [Rate Details].city_key)

This will find the intersection of the cities and rate details but it also returns rates that have just one of single cities and not the exact match and only the exact match of the cities or city on the shipment.

Am I pursuing this in the wrong fashion? Any help appreciated. Thanks.
Reply With Quote
  #2 (permalink)  
Old 04-04-06, 08:12
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Sounds like you need to do a Relational Division.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 04-04-06, 10:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
your query says "get all the rates in the rate details table, but only if a whole bunch of rows are all connected to something called Run Key 2, otherwise don't return anything at all"

that can't be right

i might be able to help you if you will follow along with me and build up your query one step at a time

you said "when a shipment goes out I need to find ..."

so let's start with a query that gets the particular shipment you're dealing with
Code:
select foo, bar
  from shipments
 where shipments.id = ?
is this at all close to what you're doing?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 04-04-06, 13:45
mmbosman mmbosman is offline
Registered User
 
Join Date: Apr 2004
Posts: 171
The relational division examples were exactly what I needed to perform here. Thanks for the help and the great link.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On