ExpressMan1
asked on
Make table that has every combination of OriginCountry and DestinationCountry
I have a table "AccountNumbers" In this table there a 4 fields, ID, OriginCountry, DestinationCountry, AccountNumber
I also have a table of Countries with 95 records (all the countries)
I need to populate the fields OriginCountry and DestinationCountry in table AccountNumbers with every possible combination of Country to Country. Number of records when completed should be 38025 (195 * 195)
What method should I use to accomplish this?
I also have a table of Countries with 95 records (all the countries)
I need to populate the fields OriginCountry and DestinationCountry in table AccountNumbers with every possible combination of Country to Country. Number of records when completed should be 38025 (195 * 195)
What method should I use to accomplish this?
If you do not want same country name as Origin and Destination, Add a WHERE clause like this.
--Create a temp table and insert this data into it.
INSERT INTO TempTable(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT a.ID,
c1.CountryName AS OriginCountry,
c2.CountryName AS DestinationCountry,
a.AccountNumber
FROM AccountNumbers a, Country c1, Country c2
WHERE c1.Name <> c2.Name;
-- Truncate your AccountNumbers table and insert data from Temp table
TRUNCATE TABLE AccountNumbers;
INSERT INTO AccountNumbers(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT ID, OriginCountry, DestinationCountry, AccountNumber FROM TempTable;
Please try the codes as per below :
SELECT ID, AccountNumber,[OriginCountry] ,[DestinationCountry] FROM
(select Row_Number() over ( ORDER BY c1.CountryName ) as RowIndex,
ac.ID,
ac.AccountNumber,
c1.CountryName as OriginCountry
,c2.CountryName as DestinationCountry
FROM AccountNumbers as ac,Countries AS c1 LEFT OUTER JOIN Countries AS c2 ON c1.CountryName <> c2.CountryName
) as result
ASKER
Sharath
Msg 208, Level 16, State 1, Line 2
Invalid object name 'TempTable'.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'TempTable'.
ASKER
Msg 207, Level 16, State 1, Line 20
Invalid column name 'OriginCountry'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'DestinationCountry'
--Create a temp table and insert this data into it.
CREATE TABLE TempTable
(
ID INT IDENTITY(1, 1) ,
OriginCountry NVARCHAR(100),
DestinationCountry NVARCHAR(100),
AccountNumber NVARCHAR(100)
);
INSERT INTO TempTable(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT a.ID,
c1.CountryName AS OriginCountry,
c2.CountryName AS DestinationCountry,
a.AccountNumber
FROM RatingDatabase.AccountNumb ers a, Country c1, Country c2;
-- Truncate your AccountNumbers table and insert data from Temp table
TRUNCATE TABLE RatingDatabase.AccountNumb ers;
INSERT INTO RatingDatabase.AccountNumb ers(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT ID, OriginCountry, DestinationCountry, AccountNumber FROM TempTable;
Invalid column name 'OriginCountry'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'DestinationCountry'
--Create a temp table and insert this data into it.
CREATE TABLE TempTable
(
ID INT IDENTITY(1, 1) ,
OriginCountry NVARCHAR(100),
DestinationCountry NVARCHAR(100),
AccountNumber NVARCHAR(100)
);
INSERT INTO TempTable(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT a.ID,
c1.CountryName AS OriginCountry,
c2.CountryName AS DestinationCountry,
a.AccountNumber
FROM RatingDatabase.AccountNumb
-- Truncate your AccountNumbers table and insert data from Temp table
TRUNCATE TABLE RatingDatabase.AccountNumb
INSERT INTO RatingDatabase.AccountNumb
SELECT ID, OriginCountry, DestinationCountry, AccountNumber FROM TempTable;
What is the table structure of RatingDatabase.AccountNumb ers?
Do you already have the columns OriginCountry and DestinationCountry in it?
Do you already have the columns OriginCountry and DestinationCountry in it?
ASKER
Created an new database CountryDB for testing. 2 tables in excel format attached. Not sure how to attach tables from db.
Now getting (0 row(s) affected)
--Create a temp table and insert this data into it.
USE CountryDB
CREATE TABLE TempTable
(
ID INT ,
OriginCountry NVARCHAR(100),
DestinationCountry NVARCHAR(100),
AccountNumber NVARCHAR(100)
);
INSERT INTO TempTable(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT a.ID,
c1.CountryName AS OriginCountry,
c2.CountryName AS DestinationCountry,
a.AccountNumber
FROM AccountNumbers a, Country c1, Country c2;
-- Truncate your AccountNumbers table and insert data from Temp table
TRUNCATE TABLE AccountNumbers;
INSERT INTO AccountNumbers(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT ID, OriginCountry, DestinationCountry, AccountNumber FROM TempTable;
CountryDB.xls
Now getting (0 row(s) affected)
--Create a temp table and insert this data into it.
USE CountryDB
CREATE TABLE TempTable
(
ID INT ,
OriginCountry NVARCHAR(100),
DestinationCountry NVARCHAR(100),
AccountNumber NVARCHAR(100)
);
INSERT INTO TempTable(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT a.ID,
c1.CountryName AS OriginCountry,
c2.CountryName AS DestinationCountry,
a.AccountNumber
FROM AccountNumbers a, Country c1, Country c2;
-- Truncate your AccountNumbers table and insert data from Temp table
TRUNCATE TABLE AccountNumbers;
INSERT INTO AccountNumbers(ID, OriginCountry, DestinationCountry, AccountNumber)
SELECT ID, OriginCountry, DestinationCountry, AccountNumber FROM TempTable;
CountryDB.xls
Do you have data in AccountNumbers?
ASKER
No. That is the table I would like to populate.
From which table, your AccountNumber is coming from?
ASKER
I will populate the AccountNumber field at a later time. Will have a separate table "CarrierAccountNumbers"
I am attempting to populate the OriginCountry and DestinationCountry fields in table AccountNumber with every possible combination of OriginCountry and DestinationCountry.
For example: The first 196 records would be Canada to every other DestinationCountry, next 196 United States to every other DestinationCountry, Albania to every other DestinationCountry etc
I am attempting to populate the OriginCountry and DestinationCountry fields in table AccountNumber with every possible combination of OriginCountry and DestinationCountry.
For example: The first 196 records would be Canada to every other DestinationCountry, next 196 United States to every other DestinationCountry, Albania to every other DestinationCountry etc
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank You Sharath that worked perfectly!
ASKER
Thank You Sharath that worked perfectly!
Proposal is to accept ID: 41760123 and close this question.
Open in new window