SQL with left outer join and 3 tables - sql

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

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;

Access Query - Group by and Max

I have been working on this for a few hours now and just can not figure it out.
How would I go about sorting this query by enco_id and only having the max of each clpr_id show up. for example:
Here is my MSAccess Sql Code
SELECT dbo_Client.med_rec_no, dbo_Encounter.episode_number, dbo_client_program.enco_id, dbo_client_program.clpr_id, dbo_client_program.prle_id, dbo_PROGRAM_LEVEL.prle_name, dbo_Client.fname, dbo_Client.lname, dbo_DISCHARGE_STATUS.dist_name, DS2.dist_name
FROM (((((dbo_Client INNER JOIN dbo_Encounter ON dbo_Client.client_id = dbo_Encounter.client_id) INNER JOIN dbo_Episode_info ON dbo_Encounter.enco_id = dbo_Episode_info.enco_id) INNER JOIN dbo_DISCHARGE_STATUS ON dbo_Episode_info.dist_id = dbo_DISCHARGE_STATUS.dist_id) INNER JOIN dbo_client_program ON dbo_Encounter.enco_id = dbo_client_program.enco_id) INNER JOIN dbo_DISCHARGE_STATUS AS DS2 ON dbo_client_program.dist_id = DS2.dist_id) INNER JOIN dbo_PROGRAM_LEVEL ON dbo_client_program.prle_id = dbo_PROGRAM_LEVEL.prle_id;
Also here is what my query looks like
EDIT:
This is the code I am trying to use now and it still is not working
SELECT dbo_Client.client_id, dbo_Client.med_rec_no, dbo_Encounter.episode_number, dbo_Encounter.start_date, dbo_Encounter.end_date, dbo_client_program.clpr_id, dbo_client_program.enco_id, dbo_PROGRAM_LEVEL.prle_name, dbo_Client.fname, dbo_Client.lname, dbo_DISCHARGE_STATUS.dist_name, DS2.dist_name
FROM (((((dbo_Client INNER JOIN dbo_Encounter ON dbo_Client.client_id = dbo_Encounter.client_id) INNER JOIN dbo_Episode_info ON dbo_Encounter.enco_id = dbo_Episode_info.enco_id) INNER JOIN dbo_DISCHARGE_STATUS ON dbo_Episode_info.dist_id = dbo_DISCHARGE_STATUS.dist_id) INNER JOIN dbo_client_program ON dbo_Encounter.enco_id = dbo_client_program.enco_id) INNER JOIN dbo_DISCHARGE_STATUS AS DS2 ON dbo_client_program.dist_id = DS2.dist_id) INNER JOIN dbo_PROGRAM_LEVEL ON dbo_client_program.prle_id = dbo_PROGRAM_LEVEL.prle_id
WHERE dbo_client_program.clpr_id IN
(SELECT TOP 1 clpr_id FROM dbo_client_program as New
WHERE New.clpr_id = dbo_client_program.clpr_id
ORDER by dbo_client_program.clpr_id DESC) AND (dbo_DISCHARGE_STATUS.dist_name <> DS2.dist_name) AND dbo_Encounter.start_date > #1/1/2020# AND dbo_Encounter.end_date > #1/1/2020#
ORDER BY dbo_Encounter.episode_number;
As you are still understanding the aggregate queries my suggestion is:
1-Create a query where you group by all fields and max(clpr_id). Save this as "Query1"
2-Create another query based on "Query1" and sort by "enco_id "

Nested SQL - Distinct Load Left Join in one statement

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`

SQL multiple left join in subquery?

Any idea on how to resolve this query dependency issue? Would subquery help? Database I'm using is sql server 2012.
FROM [Scheduling].[studentsection] AS [table027]
Left JOIN [Grading].[StudentGradeBucket] AS [table028]
ON ([table028].[StudentSectionID] = [table027].[StudentSectionID])
And (#0 = [table002].[label])
Left JOIN [Grading].[GradingPeriodGradeBucket] AS [table029]
ON [table028].[GradingPeriodGradeBucketID] = [table029].[GradingPeriodGradeBucketID]
Left JOIN [Grading].[GradeBucket] AS [table002]
ON [table029].[GradeBucketID] = [table002].[GradeBucketID]
Left JOIN [Grading].[GradeBucketType] AS [table001]
ON [table002].[GradeBucketTypeID] = [table001].[GradeBucketTypeID]
Left JOIN [Grading].[GradeMark] AS [table022]
ON [table028].[GradeMarkID] = [table022].[GradeMarkID]
The dependency issue I have is with this:
#0 = [table002].[label] //#0 is a string variable
Like the join hasn't been created yet but I need to use it to create the join for relationship [Grading].[StudentGradeBucket] or [table028]
That is weird circular join logic you are using and these table alias's you have chosen are making it more confusing. Alias's should simplify, not confuse. That said, I think it can just be moved to the where clause:
...
Left JOIN [Grading].[GradeMark] AS [table022] ON [table028].[GradeMarkID] = [table022].[GradeMarkID]
where #0 = [table002].[label]
If that fails, you may need to redefine your logic...it seems a bit circular to me.
Try this:
declare #0 nvarchar(max) = 'label value'
select *
from [Scheduling].[studentsection] as ss
left join [Grading].[StudentGradeBucket] as sgb
on sgb.[StudentSectionID] = ss.[StudentSectionID]
inner join [Grading].[GradingPeriodGradeBucket] as gpgb
on gpgb.[GradingPeriodGradeBucketID] = sgb.[GradingPeriodGradeBucketID]
inner join [Grading].[GradeBucket] as gb
on gb.[GradeBucketID] = gpgb.[GradeBucketID]
and gb.[label] = #0
left join [Grading].[GradeBucketType] as gbt
on gbt.[GradeBucketTypeID] = gb.[GradeBucketTypeID]
left join [Grading].[GradeMark] as gm
on gm.[GradeMarkID] = sgb.[GradeMarkID]
Or this if you really want left outer joins:
declare #0 nvarchar(max) = 'label value'
select *
from [Scheduling].[studentsection] as ss
left join [Grading].[StudentGradeBucket] as sgb
on sgb.[StudentSectionID] = ss.[StudentSectionID]
left join [Grading].[GradingPeriodGradeBucket] as gpgb
on gpgb.[GradingPeriodGradeBucketID] = sgb.[GradingPeriodGradeBucketID]
left join [Grading].[GradeBucket] as gb
on gb.[GradeBucketID] = gpgb.[GradeBucketID]
and gb.[label] = #0
left join [Grading].[GradeBucketType] as gbt
on gbt.[GradeBucketTypeID] = gb.[GradeBucketTypeID]
left join [Grading].[GradeMark] as gm
on gm.[GradeMarkID] = sgb.[GradeMarkID]
Or if you need the logic of only returning results from the StudentGradeBucket when there's a matching GradeBucket record; this:
declare #0 nvarchar(max) = 'label value'
select *
from [Scheduling].[studentsection] as ss
left join [Grading].[StudentGradeBucket] as sgb
on sgb.[StudentSectionID] = ss.[StudentSectionID]
left join [Grading].[GradingPeriodGradeBucket] as gpgb
on gpgb.[GradingPeriodGradeBucketID] = sgb.[GradingPeriodGradeBucketID]
left join [Grading].[GradeBucket] as gb
on gb.[GradeBucketID] = gpgb.[GradeBucketID]
left join [Grading].[GradeBucketType] as gbt
on gbt.[GradeBucketTypeID] = gb.[GradeBucketTypeID]
left join [Grading].[GradeMark] as gm
on gm.[GradeMarkID] = sgb.[GradeMarkID]
where
( --filter on the gb label only if there's a result from the sgb table;
sgb.[StudentSectionID] is null
or and gb.[label] = #0
)

SQL Server update with joins

I am trying to update a date in a table, based off of a MAX(date) in another table. To get the correct data to link up, I have to do 2 inner joins and 2 left outer joins.
I can select the correct data, it returns a Guid (PersonId) and the Date.
I have to use this information to update my original table. I am having trouble getting this to work, I still getting syntax errors.
update tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
set assign.dtmQAPCLed = dtmTaken
from
(
select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken from tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as types on types.intcerttypeid = certs.intcerttypeid
where types.intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid as data
)
where data.guidpersonid = team.guidpersonid
Assuming you are using SQL Server for this, then this should work:
UPDATE A
SET A.dtmQAPCLed = dtmTaken
FROM tblqualityassignments AS A
INNER JOIN tblrequirementteams as T
ON A.guidmemberid = T.guidmemberid
INNER JOIN (select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken
from tblqualityassignments as assign
inner join tblrequirementteams as team
on assign.guidmemberid = team.guidmemberid
inner join tblregisteredusercerts as reg
on team.guidpersonid = reg.guidpersonid
left outer join tblcerttaken as certs
on certs.guidcertid = reg.guidcertid
left outer join tblCodesCertType as [types]
on [types].intcerttypeid = certs.intcerttypeid
where [types].intcerttypeid = 1
and assign.guidmemberid = team.guidmemberid
group by reg.guidpersonid) data
ON T.guidpersonid = data.guidpersonid