Solved

Make table that has every combination of OriginCountry and DestinationCountry

Posted on 2016-08-16
15
83 Views
Last Modified: 2016-09-15
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?
0
Comment
Question by:ExpressMan1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
15 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 41758777
try 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;

-- 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;

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 41758778
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;

Open in new window

0
 
LVL 2

Expert Comment

by:Kyaw Wanna
ID: 41758873
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

Open in new window

0
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 

Author Comment

by:ExpressMan1
ID: 41759711
Sharath

Msg 208, Level 16, State 1, Line 2
Invalid object name 'TempTable'.
0
 

Author Comment

by:ExpressMan1
ID: 41759776
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.AccountNumbers a, Country c1, Country c2;

-- Truncate your AccountNumbers table and insert data from Temp table
TRUNCATE TABLE RatingDatabase.AccountNumbers;

INSERT INTO RatingDatabase.AccountNumbers(ID, OriginCountry, DestinationCountry,  AccountNumber)
SELECT ID, OriginCountry, DestinationCountry,  AccountNumber FROM TempTable;
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41759786
What is the table structure of RatingDatabase.AccountNumbers?
Do you already have the columns OriginCountry and DestinationCountry in it?
0
 

Author Comment

by:ExpressMan1
ID: 41759836
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
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41759922
Do you have data in AccountNumbers?
0
 

Author Comment

by:ExpressMan1
ID: 41759955
No.  That is the table I would like to populate.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41759974
From which table, your AccountNumber is coming from?
0
 

Author Comment

by:ExpressMan1
ID: 41760113
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
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 41760123
In that case, you just need this. no need to create any temp table.
INSERT INTO AccountNumbers(OriginCountry, DestinationCountry)
SELECT c1.CountryName AS OriginCountry, 
       c2.CountryName AS DestinationCountry
  FROM Country c1, Country c2;

Open in new window

0
 

Author Comment

by:ExpressMan1
ID: 41760453
Thank You Sharath that worked perfectly!
0
 

Author Comment

by:ExpressMan1
ID: 41760454
Thank You Sharath that worked perfectly!
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41780001
Proposal is to accept ID: 41760123 and close this question.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question