Question

I have a table view that is trying take two integer country codes (origin and destination) and replace those integer codes with actual two letter country codes while maintaining the mapping of from the 2nd table.

Here is an example of the two tables

Country Codes:

integerCode twoDigitCode   fullName
     0           US      United States
     1           BE        Belgium
     2           CN         China
     ...

Zones:

origin destination col3 col4
   0        1       x    y
   0        2       z    a
   1        2       u    b
   2        0       x    x
   2        1       i    f
   ...

What I'm trying to do is to get a result like this in the view:

origin destination col3 col4
  US        BE       x   y
  US        CN       z   a
  ...

I've tried a couple of different SQL queries like this

SELECT twoDigitCode as origin, twoDigitCode as destination
FROM country INNER JOIN Zone ON zone.destination = country.twoDigitCode 
WHERE zone.origin = country.twoDigitCode 

but it seems to just keep repeating results from both look ups.

My first question is can I even do what I'm trying to do with a SQL query? The second is there a good example or site that might explain how I can achieve a result like this.

Any help would be greatly appreciated.

Was it helpful?

Solution

You need to join to the country code table for each of the 2 lookup columns:

SELECT co.twoDigitCode as origin, cd.twoDigitCode as destination, z.col3, z.col4
FROM zones z
INNER JOIN country co ON country.integerCode = z.origin
INNER JOIN country cd ON country.integerCode = z.destination

[Note: if the origin and destination columns were nullable you would use a left join instead of an inner join]

OTHER TIPS

You simply need two separate JOINs:

SELECT c1.twoDigitCode origin, c2.twoDigitCode destination, z.col3, z.col4
FROM Zones z
JOIN Country c1 ON c1.integer.code=z.origin
JOIN Country c2 ON c2.integer.code=z.destination

Ok, you are trying to create a query that will "fill-in" each record from the Zones table by replacing each origin and destination component with its corresponding two digit country code. In other words you are decoding the origin and destination data. You can execute a Join on the Zones table twice: once to fill in the origin and a second time for the destination.

You can break it down into two queries:

  1. Decode the origin by joining the Zones table with the Country Codes table. e.g.

    SELECT *
    FROM Zones, Country Codes
    WHERE origin = integerCode
    

    This creates a temporary table where each row contains the following columns:

    (origin, destination, col3, col4, integerCode, twoDigitCode, fullName)

  2. Decode the destination using the same process. That is, Join the table created from step 1 with the Country Codes table again but this time the WHERE clause should match the destination to the integerCode.

DISCLAIMER: I am a student, not an expert. I make no claims to the efficiency, accuracy, or correctness of my solutions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top