How to JOIN more than one table from different databases? - sql

How to write a SELECT query that selects values from different tables and different databases?
e.g
dbSALES
tbSalesOrder
dbSHIPMENT
tbShipmentDetails and tbShipmentMaster.
My main aim is to JOIN all the tables

I believe this will generally work:
SELECT ...
FROM dbSALES.tbSalesOrder
JOIN dbSHIPMENT.tbShipmentDetails ON ...
JOIN dbSHIPMENT.tbShipmentMaster ON ...

Assuming that you are using SQL Server.
It will be written using a normal SELECT with JOIN, but use a fully qualified tables' names like database.schema.object_name. Like this:
SELECT
...
FROM dbSALES.tbSalesOrder
INNER JOIN dbSHIPMENT.tbShipmentDetails ...

In SQL Server you can use something along these lines:
SELECT [column-list]
FROM
dbSales.[schema].tbSalesOrder SO
JOIN dbSHIPMENT.[schema].tblShipmentDetails SD ON SD.[join_column] = SO.[join_column]
JOIN dbSHIPMENT.[schema].tbShipmentMaster SM ON SM.[join_column] = SD.[join column]

Try this.
select * from dbSales.tbSalesOrder a
join dbShipment.tbShipmentDetails b on (a.field1=b.field1 and ......)
join dbShipment.tbShipmentMaster c on (b.field1=c.field1 and ......)
where a.field1=xxxx and b.field2=yyyy

Related

How to Join two tables on two separate variables with one variable having the same name

This is what I have so far. I'm trying to join these two tables and if I do not use a using() function, then SQL tells me there is an invalid relational operator just simply using an ON. However using a "using" also throws an error that I did not end it properly. Any help would be nice, thanks.
DROP TABLE JOINED_TABLE;
CREATE TABLE JOINED_TABLE AS
SELECT *
FROM
H_SAMPLE50 H
INNER JOIN D_SAMPLE50 D
USING(ID_NBR)
AND(H.PULL_DATE = D.PREV_DATE)
You have two choices. One is where:
SELECT *
FROM H_SAMPLE50 H INNER JOIN
D_SAMPLE50 D
USING (ID_NBR)
WHERE H.PULL_DATE = D.PREV_DATE;
The other is ON:
SELECT *
FROM H_SAMPLE50 H INNER JOIN
D_SAMPLE50 D
ON H.ID_NBR = D.ID_NBR AND H.PULL_DATE = D.PREV_DATE;
Think of USING as a short-hand to handle the case where all the JOIN keys have the same name. If this is not the case, then just use ON.

How to join 3 tables without duplicate columns?

Query :
SELECT *
FROM dbo.employer_job
LEFT JOIN dbo.employer_user
ON dbo.employer_job.employer_id = dbo.employer_user.employer_user_id
LEFT JOIN dbo.company_profile
ON dbo.company_profile.company_id = dbo.employer_user.company_id
Duplicate column results :
dbo.employer_job schema :
dbo.employer_user schema :
dbo.comnpany_profile schema :
How do I remove the duplicate company_id column? My Python app won't accept duplicated columns from the database. Most suggest to use left join but that's not solving the issue.
Don't use *, but list the columns you want from each table (preferably with an alias).
SELECT EJ.job_id, EJ.employer_id, ....
FROM dbo.employer_job EJ
...
It's verbose, but how else would the database engine know what you'd like to see?
You need to list the columns explicitly -- and qualify them:
SELECT ej.*, eu.employee_user_email,
cp.company_name, . . .
FROM dbo.employer_job ej LEFT JOIN
dbo.employer_user eu
ON ej.employer_id = eu.employer_user_id LEFT JOIN
dbo.company_profile cp
ON cp.company_id = eu.company_id;
I'm not sure if the LEFT JOIN is really needed. Note that this introduces table aliases, so the query is easier to write and to read.

Simple way to join many tables with similar names in SSMS?

I have many tables that are all named similarly (like "table1" "table2" "table3" etc.) and I need to use all of them in a query. They all contain the same two variables ("ID" and "date") that they are joined on.
There are at least 25 tables of this sort and I have read-only access to the database so I can't combine them or even create a view that would do so.
My question is: Is there a simple shortcut I can use to join all these tables? If this were SAS I would create a macro, but I'm using Microsoft SQL Server Management Studio 2012.
Instead of having to do this:
select *
from table1 a
join table2 b on a.id=b.id and a.date=b.date
join table3 c on b.id=c.id and b.date=c.date
join ....
join ....
join table25 y on x.id=y.id and x.date=y.date
I'd like to do something like:
select *
from merge(table1 - table25) using(id, date)
Replacing the "merge" statement above with whatever is appropriate. Is such a thing possible?
As pointed out in the comments, the succinct syntax you are looking for doesn't exist.
The only way to shorten the SQL that takes advantage of the fact that the joining columns are all named the same would involve using the using keyword:
select *
from table1 a
join table2 b using (id, date)
join table3 c using (id, date)
join ....
join ....
join table25 y using (id, date)
But sadly, even that won't work for you, because the using keyword is not recognized in SQL Server. It does work in other popular databases though.

How can I prevent Duplicates from this SQL Statement?

I have two tables 1. tdppackages and 2. tpdstop and I do a SQL SELECT INNER JOIN to create a TableAdapter with some info from both and I want to NOT add duplicate records. Here is my SQL Statement:
SELECT tdppackages.trackno,
tdppackages.shpmentno,
tpdstop.custname,
tpdstop.address,
tpdstop.city,
tdppackages.amtdue,
tpdstop.pkgs,
tpdstop.ndx
FROM tpdstop
INNER JOIN tdppackages ON tpdstop.ndx = tdppackages.stopkey
Change SELECT to SELECT DISTINCT is the fastest way.
I think you would be having composite key which you should include in on clause.
like
INNER JOIN tdppackages ON tpdstop.ndx = tdppackages.stopkey
And tpdstop.col2 = tdppachages.col2
Do LEFT JOIN instead. Knowing the differences of each join would be very helpful moving forward.

How do combine tables and views?

How do you combine tables and views into one query? I have two Select statements one is inner join already I would like to combine the table and views in the second select and make one query.
SELECT PROJECT.PROJ_ID,
PROJECT.HULL_NUM_ID,
PROJECT.SHIP_TYPE_CD,
PROJECT.PROJ_NM,
ICP_SUMMARY.MOD_STOP_DT
FROM BAIM.ICP_SUMMARY ICP_SUMMARY
INNER JOIN BAIM.PROJECT PROJECT
ON ICP_SUMMARY.PROJ_ID = PROJECT.PROJ_ID
SELECT ICP_SUMMARY_JS_VW.PROJ_ID,
ICP_SUMMARY_JS_VW.LBR_EST_MANDAYS,
ICP_SUMMARY_JS_VW.LBR_ICP_MANDAYS,
ICP_SUMMARY_JS_VW.MATL_EST_COST,
ICP_SUMMARY_JS_VW.MATL_ICP_COST,
ICP_SUMMARY_JS_VW.TOTAL_EST_COST,
ICP_SUMMARY_JS_VW.TOTAL_ICP
FROM BAIM.ICP_SUMMARY_JS_VW ICP_SUMMARY_JS_VW
You treat views just like you would a table. You can perform joins on views, and you can have as many views as you need in the select statement.
You just need to JOIN on the table and the view, similar to this:
SELECT PROJECT.PROJ_ID,
PROJECT.HULL_NUM_ID,
PROJECT.SHIP_TYPE_CD,
PROJECT.PROJ_NM,
ICP_SUMMARY.MOD_STOP_DT,
ICP_SUMMARY_JS_VW.PROJ_ID,
ICP_SUMMARY_JS_VW.LBR_EST_MANDAYS,
ICP_SUMMARY_JS_VW.LBR_ICP_MANDAYS,
ICP_SUMMARY_JS_VW.MATL_EST_COST,
ICP_SUMMARY_JS_VW.MATL_ICP_COST,
ICP_SUMMARY_JS_VW.TOTAL_EST_COST,
ICP_SUMMARY_JS_VW.TOTAL_ICP
FROM BAIM.ICP_SUMMARY ICP_SUMMARY
INNER JOIN BAIM.PROJECT PROJECT
ON ICP_SUMMARY.PROJ_ID = PROJECT.PROJ_ID
INNER JOIN BAIM.ICP_SUMMARY_JS_VW ICP_SUMMARY_JS_VW -- do JOIN Here
ON ICP_SUMMARY.PROJ_ID = ICP_SUMMARY_JS_VW.PROJ_ID -- select the field to JOIN on