Insert a value based on id from two different tables - sql

I got a huge question.
I'm trying to migrate some data from an existent table to a new one. I'm having troubles to figure out how to do the following :
In the Address table there are two columns:
AddressTable
---------------------------------------------
StateCode(nvarchar) and CountryCode(nvarchar)
Both hold a two letter code for states and countries codes.
Now in the new table we made two foreign keys
NewAddressTable
---------------------
StateId and CountryId
That correspond to two tables State and Country
StateTable has (Id,(FK)IdCountry,Name,Code)
CountryTable has (Id,Name,Code)
What I'm trying to do is based on the state and country code on the Address table how can I add replace the values from the old table with the new ones based on the state and code.
An example:
AddressTable
-------------
City StateCode PostalCode CountryCode
North Haven CT 06473 US
NewAddressTable
---------------
IdCountry IdState
236 8
CountryTable
---------------
Id Name Code
236 UNITED STATES US
StateTable
--------------
Id IdCountry Name Code
8 236 CONNECTICUT CT
Thank you.

Something like this should work.
insert into newtable
(idCountry, idState)
select country.id, state.id
from oldtable join country on oldtable.CountryCode = Country.Code
join state on oldtable.stateCode = state.code

Related

Multiple level lookup field

I have three tables, Countries, Department & Cities, Cities table contains a foreign key to Department table PK index, and Department contains a foreign key to Countries Table PK Index for example :
COUNTRIES TABLE RECORDS
PK=1 | NAME = France
DEPARTMENTS TABLE RECORDS
PK=1 | NAME=Ile de France | COUNTRYKEY=1
PK=2 | NAME=Bouches du Rhone | COUNTRYKEY=1
CITIES TABLE RECORDS
PK=1 | NAME=Paris | DEPTKEY=1
PK=2 | NAME=Marseille | DEPTKEY=2
So, i want display a city form with all details, name department country
I actually use lookup field to display the department name but what about to display also in the city form the "COUNTRY" name which is only included on Department Table as foreignkey ??? i mean is there a way to display a lookup field of a lookup field ? as a beginner a detailled code would be appreciated, Thanks to all
SELECT d.NAME AS Department_Name, ct.NAME as CITY, cn.NAME AS COUNTRY
FROM DEPARTMENTS d
LEFT JOIN CITIES ct ON d.PK = ct. DEPTKEY
LEFT JOIN COUNTRIES ON d.COUNTRYKEY = cn.PK
You might need an WHERE clause added and possibly a ORDER BY at the end, but you didn't indicate that.

SQL statement is giving me the following error message Ambiguous column name

Background
I am a bit new to the whole JOIN concept of SQL and trying to write a SQL statement which will get a country's name based on President ID which is in another table, that later points at another table that contains the country's name.
Tables
[President]
ID | PresidentID | Name | Population Governed
1 3 ME 1,000,000,000
[CountryPopulation]
ID |PresidentID| Population Number| CountryID
1 3 1,000,000,000 12
[CountryName]
ID | CountryID | CountryName
3 12 USA
Problem: I have written a SQL statement where I am trying to to get the country's name based on the president ID, however my statement is showing an error:
Ambiguous column name 'PresdientID'.
SQL statement
SELECT
PresidentID
FROM
President A
INNER JOIN
CountryPopulation B ON A.PresdientID = B.PresdientID
WHERE
B.CountryID IN (SELECT CountryName
FROM CountryName
WHERE CountryID = B.CountryID)
The field you've selected, PresidentID is ambiguous.You need to specify which table you want to supply the value for PresidentID, like this:
Select
A.PresdientID
From President A INNER JOIN
CountryPopulation B ON A.PresdientID = B.PresdientID
WHERE B.CountryID IN (SELECT CountryName FROM CountryName WHERE CountryID = B.CountryID)
EDIT
Your question states that you're trying to get the CountryName based on the PresidientID value.
If you know the PresidientID value, I would think you'd simply do this:
DECLARE #PresidientID INT = 3
SELECT cn.CountryName
FROM CountryName cn JOIN CountryPopulation cp ON cn.CountryID = cp.CountryID
WHERE cp.PresidientID = #PresidientID;
That said, it looks like your database design may be incorrect. It seems that there are quite a few "ID" fields sprinkled around. For instance, your President table contains both an ID field and a PresidientID field; also your CountryName table contains both an ID field and a CountryID field. Also, you have the same information in multiple tables (population number, population governed).
Would it not be better to put all of the country attributes into a single table named Country? Consider this:
CREATE TABLE [Country](
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(100) NOT NULL,
[Population] INT,
[PresidentID] INT -- Foreign Key to president
)
CREATE TABLE [President](
[ID] int IDENTITY(1,1) NOT NULL,
[PresidentName] nvarchar(100) NOT NULL
)
SQL Server is unsure whether you want to return A.PresidentID or B.PresidentID. You need to specify even though the values would be the same in this case.

Update in join query with 3 tables

I have problem with updating query in Access. I have 3 tables:
City
ID | CityName | CountryID|
Country
ID | CountryName
CityImport
City | Country
I'm not sure if it's the right design, but it's of a lesser importance now.
I want to update my db with Excel data. I decided to create CityImport table to make the process clearer. I put city information in this table and I want fill City table with it. When I run a query like UPDATE (CityImport INNER JOIN Country ON CityImport.Country = Country.CountryName) LEFT JOIN City ON City.CityName = CityImport.City AND City.CountryID = Country.ID SET City.CityName = CityImport.City, City.CountryID = Country.ID WHERE CityImport.City IS NOT NULL, I get JOIN expression not supported error.
I thought it was a problem with my syntax, but if I remove one condition from JOIN, and leave it as UPDATE (CityImport INNER JOIN Country ON CityImport.Country = Country.CountryName) LEFT JOIN City ON City.CityName = CityImport.City SET City.CityName = CityImport.City, City.CountryID = Country.ID WHERE CityImport.City IS NOT NULL, it works fine. The problem is that it ignores cities with the same name in different countries.
Is it possible to make such join work properly somehow? Or is it incorrect by definition? It requires to join one one table with another join results on two columns from different tables. I could probably work around it somehow in this case, but I want to use the same method for more, more complicated tables.
I played around with different takes on this query for few hours, googled hundred times, but still no success.
The first problem I can see is that you're using UPDATE to insert data into a table. You should be using INSERT INTO.
Starting with this table:
You'll need to insert the unique Country names into the country table first:
INSERT INTO Country (Country)
SELECT DISTINCT Country
FROM CityImport
This will give you this table:
Now you need to populate the City table with city names and the ID's from the Country table:
INSERT INTO City (CityName, CountryID)
SELECT City, Country.ID
FROM CityImport INNER JOIN Country ON CityImport.Country = Country.Country
This returns this table:
Edit:
Table Structure:
CityImport
City - Text
Country - Text
Country
ID - AutoNumber
Country - Text
Primary Key: ID
City
ID - AutoNumber
CityName - Text
CountryID - Number
Primary Key: ID
Foreign Key CountryID References Country

User to location mapping with country state and city in the same table

I have a user table that has among others the fields CityId, StateId, CountryId. I was wondering if it was a good idea to store them[City, State, Country] in separate tables and put their respective ids in the User table or put all the three entities in one table.
While the former is conventional, I am concerned about the extra tables to join and so would want to store all these three different location types in one table like so
RowId - unique row id
LocationType - 1 for City, 2 for state, etc
ActualLocation - Can be a city name if the locationType is 1 and so on..
RowId LocationType ActualLocation
1 1 Waltham
2 1 Yokohama
3 2 Delaware
4 2 Wyoming
5 3 US
6 3 Japan
the problem is I am only able to get the city name for all three fields using a join like this
select L.ActualLocation as CityName,
L.ActualLocation as StateName,
L.ActualLocation as CountryName
from UserTable U,
AllLocations L
WHERE
(L.ID = U.City and L.LocationType= 1)
AND
(L.ID = U.State and L.LocationType = 2)
What worked best for us was to have a country table (totally separate table, which can store other country related information, a state table (ditto), and then the city table with ID's to the other tables.
CREATE TABLE Country (CountryID int, Name varchar(50))
CERATE TABLE State (StateID int, CountryID int, Name varchar(50))
CREATE TABLE City (CityID int, StateID int, Name varchar(50))
This way you can enforce referential integrity using standard database functions and add additional information about each entity without having a bunch of blank columns or 'special' values.
You actually need to select from your location table three times - so you will still have the joins:
select L1.ActualLocation as CityName,
L2.ActualLocation as StateName,
L3.ActualLocation as CountryName
from UserTable U,
AllLocations L1,
AllLocations L2,
AllLocations L3
WHERE
(L1.ID = U.City and L1.LocationType= 1)
AND
(L2.ID = U.State and L2.LocationType = 2)
AND
(L3.ID = U.Country and L3.LocationType = 3)
HOWEVER
Depending what you want to do with this, you might want to think about the model... You probably want a separate table that would contain the location "Springfield Missouri" and "Springfield Illinois" - depending how "well" you want to manage this data, you would need to manage the states and countries as separate inter-related reference data (see, for example, ISO 3361 part 2). Most likely overkill for you though, and it might be easiest just to store the text of the location with the user - not "pure" modeling, but much simplified for simple needs... just pulling the "word" out into a separate table doesn't really give you much other than complex queries

Joining Three tables without a matching column

I want to create a geography dimension using ssis 2008.I have 3 table sources.
Here is the explanation
Table 1 = Country: country code and country name
Table 2 = Post code: post code and city name
Table 3 = Territory : Territory code and Territory name
Here is how data looks
[Table 1= Country]
code name
------------------
US | United states
CA | Canada
[Table 2= post code]
Code city
---------------
1000 | Paris
2000 | Niece
[Table 3= Territory]
Code name
----------------
N | North
S | south
As you can see there is no single common column, I want to group these 3 tables in the same geography dimension.
So, how can I do it ?
Also,The use of this geography dim will be when another dimension for example customer dimension.we want to know the revenue of client according to his geography or the the top salespersons in some city.
and in both customer and salesperson tables you can find the those 3 as foreign keys.
You don't need a "common column" shared by all three tables.
You do need a "column column" between each pair of tables. How else are you going to link them???
Q: Is there any column that links "Country" to "City"? You should have a "country code" column in "city".
Q: Is there any way to link "Territory" with either "post code" or "country"? If "Yes": problem solved. Please list the fields. If "No" ... then you need to change your schema.
Based on you comment to paulsm4 you then want to use those tables that hold the linking information to join to each of the above 3 tables.
On the other hand if you really want to join just those three tables
select * from Country
full outer join [Post code]
on 'a' = 'a'
full outer join Territory
on 'b' = 'b'
create table dim.geography (geoID int,citycode int, countrycode char(2),territorycode char(1))
insert into dim.geography (select city as citycode,country as countrycode, territory as territorycode from Customer union select city, country,territory from salesperson)
Assuming here that Customer and salesperson tables hold the codes and not the values for country,territory, and country.
The code above will build a dimension for the geography you want. Of course if you add any additional unique city,country,territory codes into the customer/salesperson tables you will need to add it to your dimension. This is just an initial load. You may also need to modify the code to account for nulls in any of the three qualifiers.