What is the linq equivalent of this nested left join? - sql

what is the linq equivalent of nested left join below?
SELECT y.YazilimciId, y.AdSoyad, u.UygulamaId, y.KullaniciAdi, y.IsTel, y.CepTel, y.Eposta, k.KurumAdi
FROM Yazilimcilar y,
UygulamaYazilimci uy,
Uygulamalar u
left outer join
(
UygulamaKurum uk
left outer join Kurumlar k on uk.KurumId = k.KurumId
) on u.UygulamaId = uk.UygulamaId
WHERE y.YazilimciId = uy.YazilimciId
AND uy.UygulamaId = u.UygulamaId
AND u.UygulamaId = '19EA3A29-40FF-45FF-B289-03885FDC9BFC'

why do you need nested join? Actually, I've never seen syntax like this before and I'm not sure it will work, not at MS SQL Server, anyway. You can write your query as
SELECT y.YazilimciId, y.AdSoyad, u.UygulamaId, y.KullaniciAdi, y.IsTel, y.CepTel, y.Eposta, k.KurumAdi
FROM Yazilimcilar y,
UygulamaYazilimci uy,
Uygulamalar u
left outer join UygulamaKurum uk on u.UygulamaId = uk.UygulamaId
left outer join Kurumlar k on uk.KurumId = k.KurumId
WHERE y.YazilimciId = uy.YazilimciId
AND uy.UygulamaId = u.UygulamaId
AND u.UygulamaId = '19EA3A29-40FF-45FF-B289-03885FDC9BFC'
I could also suggest to rewrite all join in proper join syntax
SELECT y.YazilimciId, y.AdSoyad, u.UygulamaId, y.KullaniciAdi, y.IsTel, y.CepTel, y.Eposta, k.KurumAdi
FROM Uygulamalar u
inner join UygulamaYazilimci uy on uy.UygulamaId = u.UygulamaId
inner join Yazilimcilar y on y.YazilimciId = uy.YazilimciId
left outer join UygulamaKurum uk on u.UygulamaId = uk.UygulamaId
left outer join Kurumlar k on uk.KurumId = k.KurumId
WHERE u.UygulamaId = '19EA3A29-40FF-45FF-B289-03885FDC9BFC'
Now you can easily write LINQ :)

Related

How can I transform this Code with Joins instead of using +(=) or =

I was trying Hours to transform this code(I think It is Oracle) with joins in SQL Server instead of
using +(=)/= but somehow i dont get the same result as using += or =. Can please someone help me out ?
SELECT KNVV.MANDT, KNVV.KUNNR,
KNVV.VTWEG, KNVV.BZIRK, KNVP.KUNN2
FROM KNA1, KNVV, KNVP, CDHDR
WHERE
AND `(KNVV.MANDT = KNA1.MANDT`
AND `KNVV.KUNNR = KNA1.KUNNR)`
AND `(KNVP.MANDT (+) = KNVV.MANDT`
AND `KNVP.KUNNR (+) = KNVV.KUNNR`
AND `KNVP.VTWEG (+) = KNVV.VTWEG)`
AND `(CDHDR.MANDANT (+) = KNVV.MANDT`
AND `CDHDR.OBJECTID (+) = KNVV.KUNNR)`
AND `(KNVV.VTWEG = KNVP.VTWEG)`
AND `KNVP.PARVW (+) = 'RG'`
AND CDHDR.OBJECTCLAS (+) = 'DEBI'
Without sample data its hard to test but it appears you want:
SELECT KNVV.MANDT, KNVV.KUNNR,
KNVV.VTWEG, KNVV.BZIRK, KNVP.KUNN2
FROM KNA1
INNER JOIN KNVV
ON ( KNVV.MANDT = KNA1.MANDT
AND KNVV.KUNNR = KNA1.KUNNR
)
INNER JOIN KNVP
ON ( KNVP.MANDT = KNVV.MANDT
AND KNVP.KUNNR = KNVV.KUNNR
AND KNVP.VTWEG = KNVV.VTWEG
AND KNVV.VTWEG = KNVP.VTWEG -- This duplicated condition makes it an
-- INNER JOIN and not a LEFT OUTER JOIN as
-- you are not using (+) in Oracle's legacy
-- syntax.
AND KNVP.PARVW = 'RG'
)
LEFT OUTER JOIN CDHDR
ON ( CDHDR.MANDANT = KNVV.MANDT
AND CDHDR.OBJECTID = KNVV.KUNNR
AND CDHDR.OBJECTCLAS = 'DEBI'
)
What you probably want is to make the second join a LEFT OUTER JOIN instead of an INNER JOIN but that is not what your current query is doing as you are using (KNVV.VTWEG = KNVP.VTWEG) rather than (KNVV.VTWEG = (+) KNVP.VTWEG) in your Oracle query.
You query can be converted to:
select
KNVV.MANDT,
KNVV.KUNNR,
KNVV.VTWEG,
KNVV.BZIRK,
KNVP.KUNN2
from KNA1
join KNVV on KNVV.MANDT = KNA1.MANDT
and KNVV.KUNNR = KNA1.KUNNR
left join KNVP on KNVP.MANDT = KNVV.MANDT -- left join defeated!
and KNVP.KUNNR = KNVV.KUNNR
and KNVP.VTWEG = KNVV.VTWEG
and KNVP.PARVW = 'RG'
left join CDHDR on CDHDR.MANDANT = KNVV.MANDT
and CDHDR.OBJECTID = KNVV.KUNNR
and CDHDR.OBJECTCLAS = 'DEBI'
where KNVV.VTWEG = KNVP.VTWEG -- silently converts left join into inner join
NOTE: the original query is malformed
Even though the original query tried to perform a left join against the table KNVP this join is silently converted into an inner join by the engine. Are you sure this query is working as expected?
Did you try inner JOIN?
SELECT KNVV.MANDT, KNVV.KUNNR,
KNVV.VTWEG, KNVV.BZIRK, KNVP.KUNN2
FROM KNA1, KNVV, KNVP, CDHDR
INNER JOIN KNVV ON KNA1.MANDT=KNVV.MANDT;
Something in this sense its more convenient.
documentation bellow
https://www.w3schools.com/sql/sql_join.asp

Use query with InnerJoin

I'm trying to pass this query to inner join, but it does not know how?
This is the query I want to use InnerJoin with these values ​​where
SELECT
ticket.id_ticket,
ticket.id_rede,
historico.id_historico,
historico.id_ticket,
centro.id_centro,
eqpto.id_eqpto,
eqpto.nome,
centro.sigla_centro,
interface.id_interface,
interface.id_eqpto,
interface.desig,
tecnologia.descricao,
interface.id_tecnologia,
tecnologia.id_tecnologia,
eqpto.id_centro,
eqpto.id_rede
FROM
app_gpa_ticket.ticket,
app_gpa_ticket.historico,
dados_v3.centro,
dados_v3.eqpto,
dados_v3.interface,
dados_v3.tecnologia
WHERE
ticket.id_ticket = historico.id_ticket AND
centro.id_centro = eqpto.id_centro AND
eqpto.id_eqpto = interface.id_eqpto AND
eqpto.id_rede = ticket.id_rede AND
tecnologia.id_tecnologia = interface.id_tecnologia;
Thank you!
I think you are trying to go to the standard (explicit) syntax. What you need to do is take what would be your JOIN operators in the WHERE clause and move them near the table itself. What you need to know is what tables on the left (Before the INNER JOIN operator you are joining to the right (After the INNER JOIN operator)
SELECT
ticket.id_ticket,
ticket.id_rede,
historico.id_historico,
historico.id_ticket,
centro.id_centro,
eqpto.id_eqpto,
eqpto.nome,
centro.sigla_centro,
interface.id_interface,
interface.id_eqpto,
interface.desig,
tecnologia.descricao,
interface.id_tecnologia,
tecnologia.id_tecnologia,
eqpto.id_centro,
eqpto.id_rede
FROM app_gpa_ticket.ticket
INNER JOIN app_gpa_ticket.historico ON ticket.id_ticket = historico.id_ticket
INNER JOIN dados_v3.eqpto ON eqpto.id_rede = ticket.id_rede
INNER JOIN dados_v3.interface ON eqpto.id_eqpto = interface.id_eqpto
INNER JOIN dados_v3.centro ON centro.id_centro = eqpto.id_centro
INNER JOIN dados_v3.tecnologia ON tecnologia.id_tecnologia = interface.id_tecnologia

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
)

Convert advanced SQL query with nested joins to Linq-to-sql

I have ran into a snag with my Linq-to-Sql.
I have a sql query that runs the way I want and usually I use Linqer to convert to Linq to see the general idea. But this time my SQL query seems to advanced for Linqer. :/
I think the problem is the INNER JOINS that are nested in the LEFT OUTER JOIN. Unfortunately I have never ran into this before and don't know how to solve it using Linq.
My SQL query looks like this:
SELECT c.[Company], c.[Name_First], c.[Name_Last], ort.[IDOriginatorRoleType],
ort.[RoleType] AS [OriginatorRoleType], o.[IDOriginator], o.[IDWork],
o.[IDContact], m.[IDMedia], m.[IDWork], m.[FileName], m.[FileNameOnDisk],
m.[DateAdded], w.[IDWork] AS [IDWork2], w.[ArticleNumber], w.[Title],
w.[FrontPageLow], w.[FrontPageLowOnDisk], w.[FrontPageHigh],
w.[FrontPageHighOnDisk]
FROM [dbo].[tblSubscriptionsWorks] AS sw
INNER JOIN [dbo].[tblWorks] AS w ON sw.[IDWork] = w.[IDWork]
LEFT OUTER JOIN [dbo].[tblMedias] AS m ON m.[IDWork] = w.[IDWork]
LEFT OUTER JOIN ([dbo].[tblOriginators] AS o
INNER JOIN [dbo].[tblOriginatorRoles] AS ors ON
o.[IDOriginatorRole] = ors.[IDOriginatorRole]
INNER JOIN [dbo].[tblOriginatorRoleTypes] AS ort ON
ors.[IDOriginatorRoleType] = ort.[IDOriginatorRoleType]
INNER JOIN [dbo].[tblContacts] AS c ON
o.[IDContact] = c.[IDContact]) ON
(o.[IDWork] = w.[IDWork]) AND (ort.[IDOriginatorRoleType] = 1)
WHERE sw.[IDWork_Subscription] = 9942
The left outer join is not a problem what I can see. You just have to divide the statement
LEFT OUTER JOIN ([dbo].[tblOriginators] AS o
INNER JOIN [dbo].[tblOriginatorRoles] AS ors ON
o.[IDOriginatorRole] = ors.[IDOriginatorRole]
INNER JOIN [dbo].[tblOriginatorRoleTypes] AS ort ON
ors.[IDOriginatorRoleType] = ort.[IDOriginatorRoleType]
INNER JOIN [dbo].[tblContacts] AS c ON
o.[IDContact] = c.[IDContact]) ON
(o.[IDWork] = w.[IDWork]) AND (ort.[IDOriginatorRoleType] = 1)
into another IQueryable list. In the example the variable db is the datacontext. Here is a suggestion to a solution:
//selects all the columns that is just in the select from the left join
var leftJoin=
(
from o in db.tblOriginators
join ors in db.tblOriginatorRoles
on o.IDOriginatorRole equals ors.IDOriginatorRole
join ort in db.tblOriginatorRoleTypes
on ors.IDOriginatorRoleType equals ort.IDOriginatorRoleType
join c in db.tblContacts
on o.IDContact equals c.IDContact
where ort.IDOriginatorRoleType==1
select new
{
o.IDWork,
c.Company,
c.Name_First,
c.Name_Last,
ort.IDOriginatorRoleType,
ort.RoleType,
o.IDOriginator,
o.IDContact
}
);
var output=(
from sw in db.tblSubscriptionsWorks
join w in db.tblWorks
on sw.IDWork equals w.IDWork
from m in db.tblMedias
.Where(x=>x.IDWork==w.IDWork).DefaultIfEmpty()
//Left join with the IQueryable list
from org in leftJoin
.Where(x =>x.IDWork==w.IDWork).DefaultIfEmpty()
where
sw.IDWork_Subscription == 9942
select new
{
org.Company,
org.Name_First,
org.Name_Last,
org.IDOriginatorRoleType,
OriginatorRoleType=org.RoleType,
org.IDOriginator,
org.IDWork,
m.IDMedia,
m.IDWork,
m.FileName,
m.FileNameOnDisk,
w.FrontPageLow,
w.FrontPageLowOnDisk,
w.FrontPageHigh,
w.FrontPageHighOnDisk
}
);

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