I have a regular task which involves exporting two tables from one database and importing the data into another database whose corresponding tables are not empty. One of the tables in question has a column which refers to the ID of the first. Let's call them Customer and Customer_Address where Customer_Address has a column Cust_ID referring to ID in the Customer table.
I need to create an import script for these which will add the Customer records to the other, non-empty DB with ID = max(id)+1 for each row while not breaking the link to the other table. The Customer_Address table likewise has its own ID column which needs to be incremented in the same manner.
I don't strictly need to have the first inserted record be one higher than the existing highest ID, but it would be best. I've managed in the past to manually check the highest ID in the target DB and add this number using search and replace and a variable in the import script but it's laborious and fails if any records are added to the target DB in the interim.
I have another method which involves selecting, copying, pasting and SQL-wrapping selected all columns but ID from the source tables using Excel and using select(max) instead of the ID but again it's rather tedious.
Edit with data:
Sample script to create source and destination tables (in the same/temdb database for convenience) is here:
http://pastebin.com/C64wFtsP
Should give output as follows:
select * from customer
id Last First
1 Johnson James
2 Kelly Karl
3 Lawlor Liam
select * from customer2
id Last First
1 Adams Ann
2 Byrne Bressie
3 Casey Charlene
select * from customeraddress
id idclient street city county country
1 1 65 North St. Marcoussin Jojoba Flatland
2 2 42 South St. Marcoussin Jojoba Flatland
3 3 12 West St. Marcoussin Jojoba Flatland
4 1 17 East St. Marcoussin Jojoba Flatland
5 1 75 Centre St. Marcoussin Jojoba Flatland
select * from customeraddress2
id idclient street city county country
1 1 99 North St. Marcoussin Jojoba Flatland
2 2 88 South St. Marcoussin Jojoba Flatland
3 3 88 West St. Marcoussin Jojoba Flatland
4 1 66 East St. Marcoussin Jojoba Flatland
5 1 55 Centre St. Marcoussin Jojoba Flatland
What I'm looking for is a way to script an import of the data in both source tables to the corresponding destination tables, while preserving the link between the idclient in customeraddress and the id in customer.
If you can stage data in your destination database, just do this:
Stage both tables in Destination, all fields.
Insert users into new Destination table.
Write a query like this
Note that you'll need to identify a natural key for your customers (the thing that makes them distinct entities in Customer1 that also exists in Customer2) - firstName, lastName probably isn't the best as they're highly unlikely to be distinct across your customer base, but it's what you've got in your sample data.
INSERT INTO customeraddress2
SELECT
customer2.id,
staged_customeraddress1.street,
staged_customeraddress1.city,
staged_customeraddress1.county,
staged_customeraddress1.country
FROM customer2
JOIN staged_customer1
ON staged_customer1.firstName = customer2.firstName
AND staged_customer1.lastName = customer2.lastName
JOIN staged_customeraddress1
ON staged_customeraddress1.idclient = staged_customer1.id
Related
This question already exists:
Use left join and inner join with more than 2 tables
Closed 4 months ago.
Here you can see I have three tables
Student table
Stud_id Name Br_id Email City_id
1001 Ankit 101 ankit#bmail.com 1
1002 Pranav 105 pranav#bmail.com 2
1003 Raj 102 raj#bmail.com 2
1004 Shyam 112 shyam#bmail.com 4
1005 Duke 112 duke#bmail.com 2
1006 Jhon 102 jhon#bmail.com 3
1007 Aman 101 aman#bmail.com 4
1008 Pavan 111 pavan#bmail.com 13
1009 Virat 112 virat#bmail.com 12
Branch Table
Br_id Br_name HOD Contact
101 CSE SH Rao 22345
102 MECH AP Sharma 28210
103 EXTC VK Reddy 34152
104 CHEM SK Mehta 45612
105 IT VL Shelke 22521
106 AI KH Verma 12332
107 PROD PG Kakde 90876
Address Table
City_id City Pincode
1 Mumbai 400121
2 Pune 450011
3 Lucknow 553001
4 Delhi 443221
5 Kolkata 213445
6 Chennai 345432
7 Nagpur 323451
8 Sri Nagar 321321
I am using here three tables first one is a student table and the second one is a Branch table and the third one is an Address table
So I am writing a query like this here you can see below
select [Name], Br_name, City
from student
inner join Branch on student.Br_id = Branch.Br_id
left join [Address] on student.City_id = [Address].City_id
I have three tables I want to show here the student's name and branch name city name, but I want to show the student who has their branch only. I also want to show the student who has their city as well as show a student who does not have any city.
I wrote the query above but here I am not getting the result. Here student's name who does not have any city what's wrong here in my above join SQL query?
Here as you can see I am getting this result which I do not want to show
Name Br_name City
Ankit CSE Mumbai
Pranav IT Pune
Raj MECH Pune
Jhon MECH Lucknow
Aman CSE Delhi
Why can I not get the students who do not have a city and who have a city? What's wrong here in my join query what I am missing here?
Please let me know what's wrong in my above join query what should I do to get proper result? I have given three tables above
You can see that and why I am not able to get the result what I want to show what is the wrong in the above my join query please let me know
I hope someone will help me. Thank you so much.
This request might be asked many times but I have done a search last night to figure out but I came up with nothing.
I have three tables
Table A
ID
City
1
LA
2
NY
3
LV
Table B
ID
Job
11
Programmer
22
Engineer
33
Database Administrator
44
Cyber Security Analyst
Table C
ID
Job level
111
Junior
222
Associate
333
Senior
444
Director
Final table
ID
EmployeeName
City_ID
Job_ID
Level_ID
1000
Susie
1
11
333
1001
Nora
2
11
222
1002
Jackie
2
22
111
1003
Mackey
1
11
444
1004
Noah
1
11
111
I’d like to have a crosstab query using Microsoft Access that returns the following result ( based on city )
LA Table
Jobs
Junior
Associate
Senior
Director
Programmer
1
-
1
1
Engineer
-
-
-
-
Database Administrator
-
-
-
-
Cyber Security Analyst
-
-
-
-
How can I do it?
The best approach for this is always:
Create a "base" query that joins the base tables and returns all data columns that you will need for the crosstab query.
Run the crosstab query wizard using the "base" query as input.
I have two tables A and B. Table B has 4 columns(ID,NAME,CITY,COUNTRY), 3 columns has values and one column (ID) has NULLS. I want to insert max value from table A column ID to table B where the ID field in B should be in increasing order.
Screenshot
TABLE A
ID NAME
------- -------
231 Bred
134 Mick
133 Tom
233 Helly
232 Kathy
TABLE B
ID NAME CITY COUNTRY
------- ------- ---------- -----------
(NULL) Alex NY USA
(NULL) Jon TOKYO JAPAN
(NULL) Jeff TORONTO CANADA
(NULL) Jerry PARIS FRANCE
(NULL) Vicky LONDON ENGLAND
ID in column in B should be populated as MAX(ID) +1 from table A. The output should look like this:
TABLE B
ID NAME CITY COUNTRY
------ -------- ---------- -----------
234 Alex NY USA
235 Jon TOKYO JAPAN
236 Jeff TORONTO CANADA
237 Jerry PARIS FRANCE
238 Vicky LONDON ENGLAND
Perhaps the simplest method is to create a one-time sequence for the update:
create sequence temp_b_seq;
update b
set id = (select max(id) from a) + temp_b_seq.nextval;
drop sequence temp_b_seq;
You could actually initialize the sequence with the maximum value from a, but that requires dynamic SQL, so this seems like the simplest approach. Oracle should be smart enough to run the subquery only once.
I have one table:
Person Name Country code
Andrew 1
Philip 2
John 1
Daniel 2
and a lookup table:
Country code Country name
1 USA
2 UK
I added them to powerpivot, created a relationship between the country code fields, then I created a pivot table. I expect to get the following:
Person Name Country code
Andrew USA
Philip UK
John USA
Daniel UK
But what I actually get is:
Person Name Country code
Andrew USA
Andrew UK
Philip USA
Philip UK
John USA
John UK
Daniel USA
Daniel UK
Couple of options:
Add a column to your main table that uses a formula to pull in the Country Name from your LookUp Table e.g.
=RELATED(LookUpTable[Country Name])
If you drag in any measure that references the main table you will get your desired result e.g. =COUNTROWS('MainTable') You then hide the results column if you had to.
I have a MS-Access database with two tables which I would like to query from, the basic table schema is shown below. I am looking to pull out the details for the earliest parish church in each parish – and in the instance that there is no church with ‘parish’ in the name; I would like to pull out the earliest church.
SITEDETAIL:
Site
Reference No. | Civil Parish | Site Name | NGR East | NGR North
1 Assynt Old Parish Church 6137 3172
2 Assynt St. Marys 6097 3870
3 Assynt New Parish Church 6249 3490
4 Bower Grimbister 2095 4067
5 Bower St. Andrews 2304 3194
6 Halkirk Firth Parish Church 7136 3450
7 Holm Strath Parish Church 4586 2045
8 Holm St Nicholas Parish 4132 3146
SITEDATES:
Site
Reference No. | Date
1 1812
2 1300
3 1900
4 1760
5 1750
6 1838
7 1619
8 1774
I have written a query that pulls out all the instances of ‘parish’:
SELECT SITEDETAIL.SITEREFNO, SITEDETAIL.CIVPARBUR_CDE, SITEDETAIL.SITENAME, SITEDETAIL.NGRE, SITEDETAIL.NGRN, SITEDATES.DATE
FROM SITEDETAIL INNER JOIN SITEDATES ON SITEDETAIL.SITEREFNO = SITEDATES.SITEREFNO
WHERE (((SITEDETAIL.SITENAME) Like "par*"));
However, this does not take into account the instances of multiple/no churches with ‘par*’ in the name.
Is it possible to create an SQL query that runs through each civil parish and selects the earliest ‘parish’ or earliest church, or is it necessary to write a perl script to run through them? Is this possible using DBI?
Desired output:
Site
Reference No. | Civil Parish | Site Name | NGR East | NGR North | Date
1 Assynt Old Parish Church 6137 3172 1812
5 Bower St. Andrews 2304 3194 1750
6 Halkirk Firth Parish Church 7136 3450 1838
7 Holm Strath Parish Church 4586 2045 1619
NB:In the case of Assynt, 'Old Parish Church' is selected despite being older because of having 'parish' in the name.
The following query should get you what you need. It's a little long, but it does the trick:
`select LIST.Civil_Parish, SD.Site_name, LIST.MSite_Date
from
(
select Civil_Parish, min(Site_date) as MSite_date
from SiteDetail
where Boolean = 1
group by Civil_Parish
union
select Civil_parish, min(Site_date) as MSite_date
from SiteDetail
where Civil_parish not in
(select Civil_parish
from SiteDetail
where Boolean = 1)
group by Civil_Parish) as LIST
left join sitedetail SD on LIST.Civil_Parish = SD.Civil_Parish and LIST.MSite_Date = SD.Site_Date`
Please note the following:
1) I am using PowerUser's boolean suggestion. If the Boolean column has value 1, then the row is a Parish Church, and 0 if it is not.
2) I combined the tables "SiteDates" and "SiteDetails" for the purpose of this example, as they are 1 to 1.
The core of the query is A) finding the oldest Parish church in a Parish, then B) find Parishes without Parish Churches.
The code for A) is as follows:
'select Civil_Parish, min(Site_date) as MSite_date
from SiteDetail
where Boolean = 1
group by Civil_Parish'
We then union that with the oldest churches in parishes that do not have a parish church:
'select Civil_parish, min(Site_date) as MSite_date
from SiteDetail
where Civil_parish not in
(select Civil_parish
from SiteDetail
where Boolean = 1)
group by Civil_Parish'
We then join the union query (named "LIST" here) with our original "SITEDETAIL" table on Parish and Date to bring in the church name.