Joining Different Database Tables - sql

I have two tables in Access pulling from databases. There is no primary key linking the two tables, because the databases pull from different programs.
Using SQL, I need all of the information from both tables to pull into a query, and this is where I have problems. The two tables are pulling the same data, but they column titles might not necessarily be the same. For now, I'm assuming they are. How can I get it so that the data from both tables pull into the correct column together?
Here's an example of code (I can't post the real code for certain reasons):
SELECT system1_vehiclecolor, system1_vehicleweight, system1_licenseplate, system2_vehiclecolor, system2_vehicleweight, system2_licenseplate
FROM system1, system2
To further explain this, I want the table to have a column for vehiclecolor, vehicleweight, and licenseplate that combines all of the information. Currently, the way I have it, it is making a column for each of the names in each table, which isn't what I want.

You can use 2 queries to get this done
Select col1as c1 ,col2 col as c2 into resulttable from table1
Insert into resulttable (c1,c2) select colX as c1, colY as c2 from table2
Hope this will help you

Related

How do you merge two tables with multiple unique indentifiers?

Hey I have two tables with the same rows the first table is the main table and I want to upsert the data with new unique entries from the _tmp_ table.
for example;
id, text_id, last_sent, recent_sent, updated_at, date_created
I want to merge a communicated _tmp_ table that is created from another table into the communicated table. Only if the communicated table doesn't have an identical row id, text_id, last_sent and recent_sent
The query I'm using now is posted below but doesn't work. This query inserts all the data from the _tmp_ table.
I have checked and both the types of the tables are the same. And I just don't know what I'm doing wrong.
Help much appreciated
MERGE
`project.map.communicated` CURRENT_TABLE
USING
`project.map.communicated_tmp_` NEW_OR_UPDATED
ON
(CURRENT_TABLE.id = NEW_OR_UPDATED.id
AND CURRENT_TABLE.text_id = NEW_OR_UPDATED.text_id
AND CURRENT_TABLE.last_sent = NEW_OR_UPDATED.last_sent
AND CURRENT_TABLE.recent_sent = NEW_OR_UPDATED.recent_sent)
WHEN NOT MATCHED
THEN
INSERT
(`id`,
`text_id`,
`last_sent`,
`recent_sent`,
`updated_at`,
`date_created`)
VALUES
(`id`,`text_id`,`last_sent`,`recent_sent`,`updated_at`,`date_created`)
The Merge statement uses JOIN logic to see matches. The only reason this should not work if there are rows that have NULLS in either of the fields you use for the join. Make sure to exclude the NULLS or make a composite key which works around the NULL values.

Compare a column in one table against a whole other table?

I know that SQL joins exist but that is only for one column against another column in another table. Is there any way to do something similar with one column against a whole table? I'm trying to figure out if the people that exist within one organization are a certain kind of employee. The problem is I have all the people in a organization listed within a column in one table while the classification for people is scattered throughout various columns in another table.
While I will answer this, I recommend you do one of those schoolkids tutorials on SQL. This question is at such a basic level you'll probably just get confused by the answers anyway...
From your question I would gather that the tables are probably modeled incorrectly to start with (not normalized well enough). But if you want to join a column to all the columns in another table you can do it in two ways:
SELECT COLUMN_1 FROM TABLE_1 T1 INNER JOIN TABLE_2 T2 ON T1.COLUMN_1 = T2.COLUMN_1
UNION ALL
SELECT COLUMN_1 FROM TABLE_1 T1 INNER JOIN TABLE_2 T2 ON T1.COLUMN_1 = T2.COLUMN_2
UNION ALL
... (just change the column name on each row)
(works best if you copy/paste this into Excel with a macro and a list of column names from table 2).
2) More complex: create a view or subquery where you first union all columns in table 2 one by one (hopefully they all have the same type!) and then join to the subquery, which now acts as a table with just one column.
3) Start pivoting table 2. Not going into that one, too complex for your current level.
Yes, you can Basically if you have one Table and then you want to filter out some name from there and then you can use joins
and the second part is yes you can add multiple conditions with the help of where clause.
and id you want to join on multiple conditions then also you can do with and condition

Insert with select, dependent on the values in the table inserting into EDITED

So I need to figure out how to insert into a table, from another table, with a where clause that requires me to access the table that I am inserting into. I tried an alias from the table I am inserting into, but I quickly found out that you cannot do that. Basically, what I want to check is that the values that I am inserting into the table match a particular field within the table that I am inserting into. Here is what I've tried:
INSERT INTO "USER"."TABLE1" AS A1
SELECT *
FROM "USER"."TABLE2" AS A2
WHERE A2."HIERARCHYLEVEL" = 2
AND A2."PARENT" = A1."INSTANCE"
Obviously, this was to no avail. I've tried a couple other queries, but they didn't me anywhere, either. Any help would be much appreciated.
EDIT:
I would like to add rows to this table, not add columns to the table. The two tables are of the exact same structure -- in fact, I extracted the data already in table1 from table2. What I have in table1 currently is a bunch of records who have NO PARENT, but an instance. What I want to add is all the records who have a parent in table2 that are equal to the instance in table 1.
Currently there is no way to join on a table when inserting. The solution with the subselect where you select from the table, is the correct.
Aliasing the table you want to change is only possible with UPDATE, UPSERT and MERGE. For these operations it makes sense, as you need to match a column and then decide if you need to update it or insert something instead. In your example the line from table1 that you match is not relevant, as you don't want to change it, so from the statement point of view it is not really relevant that the table you use in your subselect is the same that the one you insert into.
As alternative, I can suggest you following solution, which is equivalent with yours:
INSERT INTO "user"."table1"
SELECT
A1."ROOT",
A1."INSTANCE",
A1."PARENT",
A1."HIERARCHYLEVEL"
FROM "user"."table2" AS A1
WHERE A1."INSTANCE" in (select "PARENT" from "user"."table1")
AND A2."HIERARCHYLEVEL" = 2
This gave me the answer I was looking for, although I am sure there is an easier -- or more efficient -- way to do it.
INSERT INTO "user"."table1"
SELECT
A1."ROOT",
A1."INSTANCE",
A1."PARENT",
A1."HIERARCHYLEVEL"
FROM "user"."table2" AS A1,
"user"."table1" AS A2
WHERE A1."INSTANCE" = A2."PARENT"
AND A2."HIERARCHYLEVEL" = 2

Compare two tables look for gaps, append them in new table

This is related to MS Access 2007. I'm in bit of a pickle and anyone who can solve this for me I will be greatly indebted to.
I have two tables: Actual and Schedule. My job is to compare these two tables, look for gaps, and fill them in a new table (append). Important point: tbnum corresponds to timeband. It's something I came up with to identify gaps easily. I have created these sample tables:
http://i39.tinypic.com/2mhesxs.jpg
There are 2 scenarios:
JFKATL: there is a match in Actual table, so bring the original record for JFKATL from Schedule as well as from Actual for the missing JFKATL record into NewTable
ORDSLC: there is no match in Actual table, but there is an apparent "gap" in timeband. So extend the timeband for the unmatched record and paste it in NewTable
Edit: Sorry, there was a small issue i noticed in the original image. Posted new one.
I may not understand your problem completely or correctly, but it appears that an UNION operator will work for you. UNION will combine the unique records between two queries and merge them into one recordset. UNION ALL will include duplicates.
SELECT c1, c2
FROM table1
UNION
SELECT c1, c2
FROM table2;

Comparing the data of two tables in the same database in sqlserver

I need to compare the two table data with in one database.match the data using some columns form table.
Stored this extra rows data into another table called "relationaldata".
while I am searched ,found some solutin.
But it's not working to me
http://weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx
can any one help how to do this.
How compare two table data with in one database using redgate(Tool)?
Red Gate SQL Data Compare lets you map together two tables in the same database, provided the columns are compatible datatypes. You just put the same database in the source and target, then go to the Object Mapping tab, unmap the two tables, and map them together.
Data Compare used to use UNION ALL, but it was filling up tempdb, which is what will happen if the table has a high row count. It does all the "joins" on local hard disk now using a data cache.
I think you can use Except clause in sql server
INSERT INTO tableC
(
Col1
, col2
, col3
)
select Col1,col2,col3from tableA
Except
select Col1,col2,col3 from tableB
Please refer for more information
http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
Hope this helps