Nested SQL - Distinct Load Left Join in one statement - sql

I wanted to left join two queries:
First:
SELECT TIG_TOL.sName AS Maschine,
TIG_TOL.lTolRef,
Max(TIG_JOB.tActBegin) AS MaxvontActBegin
FROM TIG_JOB LEFT JOIN TIG_TOL ON TIG_JOB.lMacRef = TIG_TOL.lTolRef
WHERE (((TIG_JOB.sState)="Run" Or (TIG_JOB.sState)="Ready"))
GROUP BY TIG_TOL.sName, TIG_TOL.lTolRef;
Second:
SELECT TIG_JOB.sName AS Auftrag,
TIG_JOB.lJobRef,
TIG_TOL.sName AS Artikel,
TIG_TOL.sDescript AS Artikel_Bezeichnung
FROM (TIG_JOB LEFT JOIN TIG_TOL_BOK ON TIG_JOB.lJobRef = TIG_TOL_BOK.lJobRef)
LEFT JOIN TIG_TOL ON (TIG_TOL_BOK.lTolRef = TIG_TOL.lTolRef)
AND (TIG_TOL_BOK.lTolTypRef = TIG_TOL.lTolTypRef)
WHERE (((TIG_TOL.lTolTypRef)=10));
Over a left join
on First.MaxvontActBegin = Second.TIG_JOB.tActBegin
AND First.TIG_TOL.lTolRef = Second.TIG_JOB.lMacRef
Is that possible? In Access Im doing it over two queries, where the second is using the first..

I (blindly) added TIG_JOB.tActBegin and TIG_JOB.lMacRef to the 2nd SELECT (hoping they exist) in order to JOIN the two results.
I used SELECT * only because you did not specify the column selection.
SELECT *
FROM
(
SELECT TIG_TOL.sName AS Maschine,
TIG_TOL.lTolRef,
Max(TIG_JOB.tActBegin) AS MaxvontActBegin
FROM TIG_JOB LEFT JOIN TIG_TOL ON TIG_JOB.lMacRef = TIG_TOL.lTolRef
WHERE (((TIG_JOB.sState)="Run" Or (TIG_JOB.sState)="Ready"))
GROUP BY TIG_TOL.sName, TIG_TOL.lTolRef
) AS FirstTable
LEFT JOIN
(
SELECT TIG_JOB.sName AS Auftrag,
TIG_JOB.lJobRef,
TIG_TOL.sName AS Artikel,
TIG_TOL.sDescript AS Artikel_Bezeichnung,
TIG_JOB.tActBegin,
TIG_JOB.lMacRef
FROM (TIG_JOB LEFT JOIN TIG_TOL_BOK ON TIG_JOB.lJobRef = TIG_TOL_BOK.lJobRef)
LEFT JOIN TIG_TOL ON (TIG_TOL_BOK.lTolRef = TIG_TOL.lTolRef)
AND (TIG_TOL_BOK.lTolTypRef = TIG_TOL.lTolTypRef)
WHERE (((TIG_TOL.lTolTypRef)=10))
) AS SecondTable
ON FirstTable.MaxvontActBegin = SecondTable.tActBegin
AND FirstTable.lTolRef = SecondTable.lMacRef`

Related

SQL Selects combine - Second select to be in Where clause

I have 2 selects. The first one:
select purs.t_orno as purchased
from ttisfc001201 sls
inner join twhinr110201 sfc on sfc.t_orno = sls.t_pdno
inner join twhltc100201 purs on purs.t_clot=sfc.t_clot
left join twhltc220201 items on items.t_clot = sfc.t_clot
left join twhltc210201 cert_num on cert_num.t_item = items.t_item
left join twhltc200201 cert on cert.t_ltft = cert_num.t_ltft
where sls.t_cprj = 'SLS004336' and purs.t_orno like N'PUR%'
and sfc.t_koor = 1 and sfc.t_kost = 5
Is giving me these results:
PUR007833
PUR008544
PUR008698
PUR008963
PUR009048
PUR009304
PUR009611
PUR009912
PUR009913
PUR010006
PUR010110
PUR010400
PUR010465
PUR010539
PUR010664
So basically these are results I must use in the second select in where clause. A field from table in second select must be equal to one of them. To understand me better it should look like this:
select distinct fac.t_isup
from ttfacp200201 fac
inner join ttfacp250201 mid on mid.t_ityp = fac.t_ttyp and mid.t_idoc=fac.t_ninv
where mid.t_orno ='PUR010400' or mid.t_orno='PUR009912'or mid.t_orno='PUR009913'or mid.t_orno='PUR010465'or mid.t_orno='PUR008544'or mid.t_orno='PUR008963'or mid.t_orno='PUR009048'or mid.t_orno='PUR010110'or mid.t_orno='PUR007833'or mid.t_orno='PUR009304'or mid.t_orno='PUR009611'or mid.t_orno='PUR010664'or mid.t_orno='PUR010006'or mid.t_orno='PUR010539'or mid.t_orno='PUR008698'or mid.t_orno='PUR010667'
All these ORs are results from the first select. How I can combine them (the first select to go in second select where clause) so I can get results at once?
You can use the IN clause for your second query
select distinct fac.t_isup
from ttfacp200201 fac
inner join ttfacp250201 mid on mid.t_ityp = fac.t_ttyp and mid.t_idoc=fac.t_ninv
where mid.t_orno IN (
select purs.t_orno
from ttisfc001201 sls
inner join twhinr110201 sfc on sfc.t_orno = sls.t_pdno
inner join twhltc100201 purs on purs.t_clot=sfc.t_clot
left join twhltc220201 items on items.t_clot = sfc.t_clot
left join twhltc210201 cert_num on cert_num.t_item = items.t_item
left join twhltc200201 cert on cert.t_ltft = cert_num.t_ltft
where sls.t_cprj = 'SLS004336' and purs.t_orno like N'PUR%'
and sfc.t_koor = 1 and sfc.t_kost = 5
)
Something like this
;with first_query_cte(purchased) as (
select purs.t_orno
from ttisfc001201 sls
inner join twhinr110201 sfc on sfc.t_orno = sls.t_pdno
inner join twhltc100201 purs on purs.t_clot=sfc.t_clot
left join twhltc220201 items on items.t_clot = sfc.t_clot
left join twhltc210201 cert_num on cert_num.t_item = items.t_item
left join twhltc200201 cert on cert.t_ltft = cert_num.t_ltft
where sls.t_cprj = 'SLS004336' and purs.t_orno like N'PUR%'
and sfc.t_koor = 1 and sfc.t_kost = 5)
select distinct fac.t_isup
from ttfacp200201 fac
inner join ttfacp250201 mid on mid.t_ityp = fac.t_ttyp and mid.t_idoc=fac.t_ninv
inner join first_query_cte fqc on mid.t_orno=fqc.purchased;

Combine SQL statements as columns, not as rows

First Query:
SELECT OPENING.COMP_CODE, COMPANY.COMP_NAME, OPENING.QUAL_CODE, QUALIFICATION.QUAL_DESCRIPTION
FROM OPENING
LEFT JOIN COMPANY ON OPENING.COMP_CODE = COMPANY.COMP_CODE
LEFT JOIN QUALIFICATION ON OPENING.QUAL_CODE = QUALIFICATION.QUAL_CODE
Second Query:
SELECT EDUCATION.CAND_NUM, CANDIDATE.CAND_LNAME, EDUCATION.QUAL_CODE, QUALIFICATION.QUAL_DESCRIPTION
FROM EDUCATION
LEFT JOIN CANDIDATE ON EDUCATION.CAND_NUM = CANDIDATE.CAND_NUM
LEFT JOIN QUALIFICATION ON EDUCATION.QUAL_CODE = QUALIFICATION.QUAL_CODE
I tried using <Query1> UNION ALL <Query2> but it combines the results as rows. I want to combine the results as columns (side by side)
If you need to bring all the result in one row, UNION ALL / UNION wont work. Use cross apply instead. Such as,
SELECT A.* ,B.*
FROM
(
SELECT
OPENING.COMP_CODE, COMPANY.COMP_NAME, OPENING.QUAL_CODE, QUALIFICATION.QUAL_DESCRIPTION
FROM OPENING
LEFT JOIN COMPANY ON OPENING.COMP_CODE = COMPANY.COMP_CODE
LEFT JOIN QUALIFICATION ON OPENING.QUAL_CODE = QUALIFICATION.QUAL_CODE
) AS A
CROSS APPLY
(
SELECT EDUCATION.CAND_NUM, CANDIDATE.CAND_LNAME, EDUCATION.QUAL_CODE, QUALIFICATION.QUAL_DESCRIPTION
FROM EDUCATION
LEFT JOIN CANDIDATE ON EDUCATION.CAND_NUM = CANDIDATE.CAND_NUM
LEFT JOIN QUALIFICATION ON EDUCATION.QUAL_CODE = QUALIFICATION.QUAL_CODE
) AS B

Passing different column values to where clause

SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid = pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890'
which gives me the following output
Now I want to use the above output values to select the rows from the table "YesNoAnswerWithObservation"
I imagine it should look something like this Select * from YesNoAnswerWithObservation Where Id in (22,27,26,...23)
Only instead of typing the values inside IN clause I want to use the values in each column resulting from above-mentioned query.
I tried the below code but it returns all the rows in the table rather than rows mentioned inside the In
SELECT pims.yesnoanswerwithobservation.observation,
graphitegtccore.yesnoquestion.description,
pims.yesnoanswerwithobservation.id ObservationId
FROM pims.yesnoanswerwithobservation
INNER JOIN graphitegtccore.yesnoquestion
ON pims.yesnoanswerwithobservation.yesnoanswerid =
graphitegtccore.yesnoquestion.id
WHERE EXISTS (SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.pelvicorgandiseaseid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.gynocologicalscanid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid =
pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890')
Any help or a nudge in the right direction would be greatly appreciated
Presumably you want the ids from the first query:
SELECT awo.observation, ynq.description, ynq.id as ObservationId
FROM pims.yesnoanswerwithobservation awo JOIN
graphitegtccore.yesnoquestion ynq
ON awo.yesnoanswerid = ynq.id
WHERE ynq.id = (SELECT mer.id
FROM pims.pimscase c JOIN
pims.digitization d
ON c.digitizationid = d.id JOIN
pims.medicalexaminerreport mer
ON d.medicalexaminerreportid = mer.id JOIN
pims.icicimedicalexaminerreport imer
ON mer.id = imer.id JOIN
pims.icicimerfemaleapplicant ifa
ON imer.id = ifa.id
WHERE c.tiannumber = 'ICICI1234567890'
) ;
Notice that table aliases make the query much easier to write and to read.

Joining results of two queries: #1248 - Every derived table must have its own alias

I'm trying to combine the results produced by two queries on my database...
q1:
SELECT * FROM werkgevers JOIN werkgevers_branches ON werkgevers.werkgever_id = werkgevers_branches.werkgever_id JOIN plaatsen ON werkgevers.plaats_id = plaatsen.plaats_id JOIN branches ON werkgevers_branches.branche_id = branches.branche_id GROUP BY werkgevers_branches.werkgever_id
q2:
SELECT werkgever_id, COUNT(werkgever_id) AS aantalvacatures FROM vacatures GROUP BY werkgever_id
... like this:
SELECT * FROM (
SELECT * FROM werkgevers JOIN werkgevers_branches ON werkgevers.werkgever_id = werkgevers_branches.werkgever_id JOIN plaatsen ON werkgevers.plaats_id = plaatsen.plaats_id JOIN branches ON werkgevers_branches.branche_id = branches.branche_id GROUP BY werkgevers_branches.werkgever_id
) AS tbl1
LEFT OUTER JOIN
(
SELECT * FROM (
SELECT werkgever_id, COUNT(werkgever_id) AS aantalvacatures FROM vacatures GROUP BY werkgever_id
) AS tbl2
)
USING (werkgever_id)
but I keep getting the error
#1248 - Every derived table must have its own alias
I'm not sure where I should name any derived tables, any suggestions?
Your LEFT OUTER JOIN derived table needs an alias. Try this:
select *
from (
select *
from werkgevers
join werkgevers_branches on werkgevers.werkgever_id = werkgevers_branches.werkgever_id
join plaatsen on werkgevers.plaats_id = plaatsen.plaats_id
join branches on werkgevers_branches.branche_id = branches.branche_id
group by werkgevers_branches.werkgever_id
) as tbl1
left outer join (
select *
from (
select werkgever_id,
COUNT(werkgever_id) as aantalvacatures
from vacatures
group by werkgever_id
) as tbl2
) a USING (werkgever_id)
Note the alias a on the last line.

SQL with left outer join and 3 tables

I would like to add an ADD at the end of my code. Please have a look on my code and thanks for your support:
SELECT Area.org,
Supervisors.NomSup,
Supervisors.PrenomSup,
Employees.NomEmp,
Employees.PrenomEmp,
Employees.NoIdAlcanEmp,
Competencies.CodeCompetencies,
Competencies.CompetencyName,
LinkResultComp.AssNote,
LinkResultComp.AssDate
FROM ((((((
Area INNER JOIN Supervisors ON Area.IdArea = Supervisors.IdArea
)
INNER JOIN Employees ON Supervisors.IdSupervisor = Employees.IdSupervisor
)
INNER JOIN LinkProfilesEmployees ON Employees.IdEmp = LinkProfilesEmployees.IdEmp
)
INNER JOIN Profiles ON Profiles.IdProfiles = LinkProfilesEmployees.IdProfiles
)
INNER JOIN LinkProfComp ON Profiles.IdProfiles = LinkProfComp.IdProfiles
)
INNER JOIN Competencies ON Competencies.IdCompetencies = LinkProfComp.IdCompetencies
)
LEFT OUTER JOIN LinkResultComp ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
WHERE Area.org LIKE "*20*" AND Competencies.CodeCompetencies LIKE "khse2010-05"
ORDER BY Supervisors.NomSup, Employees.NomEmp;
Just remove the extra ON that you added
So change this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
------^^ This one
to this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
Of course I assume you meant different fields for the second condition