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
Related
I am trying to write a SQL Select statement to port some data from a Sybase environment to a MongoDB environment and I'm just trying to figure out the correct syntax to involve three different tables.
Basically what I need to do is do an INNER JOIN on two tables, and then do a matching check against a 3rd table. The three table names are "ar_notes", "customer_service_xref" and "service_notes_details"
This is what I tried:
SELECT * FROM ar_notes arn
LEFT JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
WHERE arn.visit_date = service_notes_details.date_of_visit
This doesn't work. I get a correlation error.
What should the syntax look like when involving three tables like this?
You said you need an INNER JOIN among three tables but your query does a LEFT JOIN between two and tries another join in the WHERE clause without refering that table in the FROM clause.
To just fix your query:
SELECT *
FROM service_notes_details snd, ar_notes arn
INNER JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
WHERE arn.visit_date = snd.date_of_visit
This is what you should use in current SQL syntax:
SELECT *
FROM ar_notes arn
INNER JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
INNER JOIN service_notes_details ON arn.visit_date = service_notes_details.date_of_visit
To be clear, this will only return lines in ar_notes that have corresponding values in customer_service_xref (joining by customer_service_xref_id) and in service_notes_details(joining by visit_date). Your original query, using LEFT JOIN would return lines from ar_notes even if there was no matching customer_service_xref_id.
New to SQL but I want to be able to optimize my query by bringing just the right amount of data. I am doing a left join on CS Rep Name and WE, which are two columns present in both tables. I find that if I don't bring in CS Rep Name and WE in the TECDR table, the query would error. Is there a workaround to this? Since it is a left join, I don't need redundant data.
SELECT *
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN (SELECT CS_Rep_Name,
Team_Leader,
Operations_Manager,
Tenure,
WE,
FileName
FROM Tish_Email_CSAT_Dump_Roster) AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name
AND TECD.WE = TECDR.WE
When you embed a SELECT inside a query in place of a table, the result of a select (projection) behave like a table visible only inside the query.
In your case, the join is the same as if there were a table called TECDR with the columns that you select. Hence, if you leave out some columns of Tish_Email_CSAT_Dump_Roster from your SELECT, these columns would not be available for joining or selection.
However, in your case this is unnecessary: all you need to do is joining to the underlying table, like this:
SELECT
TECD.*
, TECDR.Team_Leader
, TECDR.Operations_Manager
, TECDR.Tenure
, TECDR.FileName
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN Tish_Email_CSAT_Dump_Roster AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name AND TECD.WE = TECDR.WE
select
<place the columns you want here>
from
Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE
Hope the following helps or else please share the query that errors:
select TECD.Column1, TECD.Column2, TECDR.Column1, TECDR.Column2
from Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE
thanks in advance for your help.
I have a set of tables like this.
Also, here is a semi-accurate ERD to see what's going on.
I need to list all donations made by individual Alumni, and businesses. I need the first/last name and IDs present.
The following code returns all I need about the Alumni donors:
SELECT DonationID, Donation.AlumniID, BusinessID_FK, DONATIONDATE, Value, Alumnus.FirstName, Alumnus.LastName
FROM DONATION
JOIN Alumnus
on Donation.AlumniID=Alumnus.alumniid;
And the following code returns all I need about the Business donors:
SELECT * FROM DONATION
JOIN BusinessSponser
on Donation.businessid_fk=businesssponser.businessid;
Furthermore, the following query returns all the IDs, however no First/Last names:
SELECT * FROM DONATION
LEFT JOIN BusinessSponser
on Donation.businessid_fk=businesssponser.businessid;
So my question is, how can I merge these queries together as one? My intention is to create a view that would display the final result.
Thanks in advance for your help.
The outer join is a SQL technique which allows us to join rows from one table with some rows from another table. In your case you have two optional tables so you need two left outer joins:
select donationid
, donation.donationdate
, donation.value
, donation.alumniid
, alumnus.firstname
, alumnus.lastname
, donation.businessid
, businesssponser.businessname
from donation
left outer join alumnus
on donation.alumniid=alumnus.alumniid;
left outer join businesssponser
on donation.businessid_fk=businesssponser.businessid;
I am developing a new database backend for my application for more flexibility.
Instead of one large table with many columns, I now have four tables;
Table "roms" (contains a single column 'Name')
Table "romsmeta" (contains columns 'Name','Title','Year')
Table "software" (contains columns 'System' and 'Name')
Table "softwaremeta" (contains columns 'System','Name','Title','Year')
So now I need to use a JOIN type in my queries, but my skills are a bit rusty. Basically, I would like to perform the following pseudo-query;
SELECT Title,Year from (roms INNER JOIN romsmeta,software INNER join softwaremeta) WHERE Title like '%enteredTitle%'
Obviously, this syntax is not valid.
What query would return the results I'm looking for?
Thanks in advance!
Try this:
SELECT rm.Title, rm.Year
from roms r
inner join romsmeta rm
on r.name = rm.name
inner join software s
on s.name = rm.name
inner join softwaremeta sm
on sm.name = rm.name
WHERE rm.Title like '%enteredTitle%'
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.