We have 2 tables Application and Address, Application table has columns like home_address,office_address and delivery_address and all these 3 column is of number type having values like say 111,112 and 113 for a row of it.It has many such rows. In a separate table we have say column like id and address having lot of records with different id's. we need to get output like say all above 3 columns plus 3 more columns like say full_homeaddress, full_officeaddress , full_deliveryaddress in every row of output.
If it was only one column like home_address in Application table, i would have easily done the join with primary key of Address table and one foreign key of Application table but here we need to display full address of 3 different fields simultaneosly in each row of output.
I think for one row of output, i will have to get records from 1 row of Application table and 3 different rows of Address table to display in every row of output. For these 3 columns of Application table , required addresses are in 3 different rows of Address table.
Kindly help how to write join for it
SELECT app.*,
home.address_description,
office.address_description,
delivery.address_description
FROM application app,
Address home,
Address office,
Address delivery
WHERE
app.homeaddressId = home.addressId(+)
AND app.officeAddressId = office.addressId(+)
AND app.deliveryAddressId = delivery.addressId(+)
Related
Good day everyone. I'm sure this is a fairly common issue but I trawled the internet and I cannot find an answer that works in my case. I have 2 tables which both have 2 fields and both field names are the same. I need to select all records in table 1 where the 1st field matches table 2 1st field and the 2nd field in table 2 does not match field 2 in table 1. Sorry if I'm not explaining this very well. Table 1 has around 10K records with client-name and country. table 2 is a unique list of client names (around 1K) with client-name and country fields. so I need to select all the records from table 1 with same client-name but where country in table 2 is not the same as in table 1. I've been on this for several hours now so any help would be most welcome.
The data is in 1 Access Database and I'm using ADO connection. What I have is:-
SELECT [client-name], [country] FROM [table1] LEFT JOIN [table2] ON [table1].[client-name]
= [table2].[client-name] WHERE NOT [table1].[country] = [table2].[country];
I need to write a query, and I'm not sure where to start... A join won't work since I'm basically querying for everything from both tables (for one column).
I have 2 tables, Each table different fields. But each table has 1 field in common i.e. Domains, I need to pull a list of all domains between both of them and return it in 1 column.
So table 1's domain field, and table 2's domain field showing in 1 column below eachother.
Also removing duplicates where there are. So if there's 2 domains that both are the same, I only need to show 1.
Is something like this possible in a SQL Query?
I have 3 tables that work, let's say, as levels for this purpose. Everyone of them has 2 columns, id and name. And, they combined, result on posibilities that matches the table 4.
How can I create the relationships between the first set (3 tables), and the last one with the possible results after the combination?
I did this in the past just with 2 tables, back then I created a third one having 2 fields, 2 FKs against the original tables. But this time, I have a set of 3 tables to match with a fourth, and that's what's making me wonder.
Should I simply create a 5th table with 4 fields having 4 FKs or is there another way?
Use a 5th table as an assignment table, then using a nested query with joins you can access the data in your results table
note, the 5th table would have 3 FK columns linked to the 3 other tables, and a 4th for a row id.
I have a database table with 8 fields say Table(a,b,c,d,e,f,g,h).For some rows one of the field is different(say 4 different a values) while all other field values(b-h) in the schema are same.I have to make a table selecting just one rows from such rows with different a's but same b-h.That is I can select any one of the different a's and keep b-h same which they are and display it in table as 1 single row instead of 4.
SELECT MIN(a) a,b,c,d,e,f,g,h
FROM mytable
GROUP BY b,c,d,e,f,g,h
Two customers are going to merge. They are both using my application, with their own database. About a few weeks they are merging (they become one organisation). So they want to have all the data in 1 database.
So the two database structures are identical. The problem is with the data. For example, I have Table Locations and persons (these are just two tables of 50):
Database 1:
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
1 Location A
2 Location B
Persons:
Id LocationId Name etc...
1 1 Mark
2 2 Ashley
3 1 Ben
We see that person is related to location (column locationId). Note that I have more tables that is referring to the location table and persons table.
The databases contains their own locations and persons, but the Id's can be the same. In case, when I want to import everything to DB2 then the locations of DB1 should be inserted to DB2 with the ids 3 and 4. The the persons from DB1 should have new Id 4,5,6 and the locations in the person table also has to be changed to the ids 4,5,6.
My solution for this problem is to write a query which handle everything, but I don't know where to begin.
What is the best way (in a query) to renumber the Id fields also having a cascade to the childs? The databases does not containing referential integrity and foreign keys (foreign keys are NOT defined in the database). Creating FKeys and Cascading is not an option.
I'm using sql server 2005.
You say that both customers are using your application, so I assume that it's some kind of "shrink-wrap" software that is used by more customers than just these two, correct?
If yes, adding special columns to the tables or anything like this probably will cause pain in the future, because you either would have to maintain a special version for these two customers that can deal with the additional columns. Or you would have to introduce these columns to your main codebase, which means that all your other customers would get them as well.
I can think of an easier way to do this without changing any of your tables or adding any columns.
In order for this to work, you need to find out the largest ID that exists in both databases together (no matter in which table or in which database it is).
This may require some copy & paste to get a lot of queries that look like this:
select max(id) as maxlocationid from locations
select max(id) as maxpersonid from persons
-- and so on... (one query for each table)
When you find the largest ID after running the query in both databases, take a number that's larger than that ID, and add it to all IDs in all tables in the second database.
It's very important that the number needs to be larger than the largest ID that already exists in both databases!
It's a bit difficult to explain, so here's an example:
Let's say that the largest ID in any table in both databases is 8000.
Then you run some SQL that adds 10000 to every ID in every table in the second database:
update Locations set Id = Id + 10000
update Persons set Id = Id + 10000, LocationId = LocationId + 10000
-- and so on, for each table
The queries are relatively simple, but this is the most work because you have to build a query like this manually for each table in the database, with the correct names of all the ID columns.
After running the query on the second database, the example data from your question will look like this:
Database 1: (exactly like before)
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
10001 Location A
10002 Location B
Persons:
Id LocationId Name etc...
10001 10001 Mark
10002 10002 Ashley
10003 10001 Ben
And that's it! Now you can import the data from one database into the other, without getting any primary key violations at all.
If this were my problem, I would probably add some columns to the tables in the database I was going to keep. These would be used to store the pk values from the other db. Then I would insert records from the other tables. For the ones with foreign keys, I would use a known value. Then I would update as required and drop the columns I added.