Converting SQL to LINQ (left join on two fields) - sql

I need help to convert this expression to LINQ.
In this example:
TableA[IDTABLE_A, NAME]
TableA[IDTABLE_B, IDTABLE_A, REL]
SELECT *
FROM TableA a
LEFT JOIN TableB b
ON a.IDTABLE_A = b.IDTABLE_A
AND b.IDTABLE_B = 3
Thanks in advance.

Try this:-
var query = from a in data1
join b in data2.Where(x => x.BID == 3)
on a.AID equals b.AID into ab
from c in ab.DefaultIfEmpty()
select new
{
AID = a.AID,
AName = a.AName,
BName = c == null ? "No Records" : c.BName
};
Complete Working Fiddle Here.

Related

Bigquery not picking up a column name to join tables

Why am i getting an error saying that SA2_NAME_2016 is not found when that column is indeed in table c?
select a.Avg__O_Sample_A_5G, a.Avg__O_Sample_A_All_5G, a.Avg__O_Sample_iOS_5G, a.Avg__O_Sample_iOS_All_5G,b.*, c.geomtry
from o-a-c-prd.R_A_K_SCORE_BREAKDOWN.Churn_May as a
left join
(select * from o-a-c-prd.R_A_K_SCORE_BREAKDOWN.A_K_MAY where Month = '2022-05') as b
on
a.SA2 = b.SA2_NAME_2016
JOIN
(select geometry from o-a-c-prd.R_P_Polygons.SA2_GEO_PUB_T ) as c
on
a.SA2 = c.SA2_NAME_2016
The ONLY column available in c is geometry!! So, the error you see makes total sense
Use below instead
select a.Avg__O_Sample_A_5G,
a.Avg__O_Sample_A_All_5G,
a.Avg__O_Sample_iOS_5G,
a.Avg__O_Sample_iOS_All_5G,
b.*,
c.geometry
from o-a-c-prd.R_A_K_SCORE_BREAKDOWN.Churn_May as a
left join
(select * from o-a-c-prd.R_A_K_SCORE_BREAKDOWN.A_K_MAY where Month = '2022-05') as b
on a.SA2 = b.SA2_NAME_2016
JOIN
(select SA2_NAME_2016, geometry from o-a-c-prd.R_P_Polygons.SA2_GEO_PUB_T ) as c
on a.SA2 = c.SA2_NAME_2016

How to find a value if we have 2 same columns in 2 different tables?

As you can see on the photo. I have 2 tables(A and B). KlantId is common in 2 tables. What I want to achieve is, if I provide the Email then I can get the reservatieNummer.
select b.reservatieID
from tablea as a
inner join tableb as b on a.KlantId = b.KlantId
where a.email="whatever"
and you specify the email in the where condition
To use linq:
var ReservatieID = tableA.Join(tableB, ta =>ta.KlantId, tb =>tb.KlantId, (ta,tb) =>new{tableA = ta, tableB = tb})
.Where(a=> a.email == "whatever#domain.com").Select(a=> new{a.reservatieID});

Update with two table ,I had a example for one row,but I must update 168432 rows,used sqlite

I want to update the sqlite table, the following is the example for updating one row:
update tpecroad set tnode = (SELECT b.nodeid FROM "TPECRoad" as a
join tpecnode as b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry and a.pk =1)
where pk=1
but there are 168432 rows to be updated, is there any faster way to update the large amount of the data?
It's like changed a.pk=1~168432 and pk=1~168432
Thanks a lots!!
update tpecroad as c
set tnode = ( SELECT b.nodeid
FROM "TPECRoad" as a join tpecnode as b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry and a.pk = c.pk);
If I understand the question right, this query may help:
update tpecroad a set tnode = (
select b.nodeid from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
)
where exists (
select 1 from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
);
Edit: If a.pk = b.pk must verify to perform the update, the query will look as follows:
update tpecroad a set tnode = (
select b.nodeid from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
and a.pk = b.pk
)
where exists (
select 1 from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
and a.pk = b.pk
);
Try this:
Update tpecroad a
set tnode = (SELECT b.nodeid
FROM tpecnode as b
where pointn(tpecroad.geometry,numpoints(tpecroad.geometry)) = b.geometry)

SQL query get record table A based on parentid Table B

[SOLVED: thanks to bluefeet and Brian I got to build this working query (turned out I also needed a fourth table / view)]"
SELECT A.SalesLineID, A.ArticleResultID, B.ID, C.ID, D.Value
FROM VIEWA AS A
INNER JOIN TABLEB AS B ON A.ArticleResultID = B.ArticleResultID
AND B.Name = N'Collect' AND DAPR.Value = 1
INNER JOIN TABLEC AS C ON B.ArticleResultID = C.ID
AND C.Name='Assemble'
AND C.Value = 1
INNER JOIN TABLED AS D ON D.ArticleResultID = C.ParentId
AND D.Name = 'IndexY'
WHERE (A.SalesID = #SalesID)
[UPDATE: I've made a mistake assuming IndexY/Color and ProdId where fields Table A is some kind of parameter / property table with only 5 columns ID - NAME - VALUE - ARTICLERESULTID - PRODID. IndexY is a value of the Name field..]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
ID Name Value ArticleResultID ProdID Color
1 Operation Collect 110 10 BLACK
2 IndexY 10 110 10 -
3 Operation Collect 101 11 WHITE
Table B
ID ParentID Name Value
101 110 Assemble 1
101 100 Assemble 0
Steps:
Find record in A with Name = Operation and Value = Collect and ProdId = 11 AS ORG_A
Find record in B With B.ID = ORG_A.ArticleResultId AND B.NAME = 'Assemble'AND B.VALUE = 1 AS B
Find record in A With A.ArticleResultID = B.ParentID as NEW_A
In the above scenario thats ORG_A.ArticleResultID = 11 --> B.ParentID = 110 --> NEW_A.ARTICLERESULTID = 110 --> (IndexY - Value - 10)
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
[OLD DESCRIPTION:]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
Name Value ArticleResultID IndexY Color ProdID
Operation Collect 110 10 - 0
Operation Collect 101 _ Black 100
Table B
ID ParentID Name Value Dx Dy
101 110 Assemble 1 1000 500
101 100 Assemble 0 400 300
I want to fetch all records from A where NAME equals 'operation', VALUE equals 'Collect' and PRODID = '100' but I also want (here's my problem) the IndexY value of the record in Table A with the PARENTID in Table B which joins on TABLE B.ID = A.ArticleResultID AND Name = 'Assemble' and VALUE = '1'
In the above scenario thats ParentID 110 which gives me the record in Table A with ArticleResultID 110 with IndexY (10).
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
Since your requirements are not the clearest. How about this:
SELECT a1.*, a2.indexy as additionalIndexY
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
Here is a sqlfiddle with a working version
If you just want the record with the 110 articleresultid, then:
SELECT a2.*
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN join ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
see the sqlfiddle for the second example
Does this work for you?
SELECT a.*,c.IndexY
FROM [TableA] a
JOIN [TableB] b ON a.ArticleResultId = b.id
AND b.Name ='Assemble'
AND b.Value = 1
JOIN [TableA] c ON b.ParentId = c.ArticleResultID
WHERE a.name = 'Operation'
AND a.Value = 'Collect'
AND a.ProdId = 100

Nested Select Count Query with Criteria

say that we have 2 entity EntityA and EntityB , with the related tables, TableA and TableB.
i have to implement this query:
select a.Id , (select count(b.Id) from TableB b where b.FKa = a.Id and b.AnotherField > 0) as TheCount
from TableA a
i'm very close to that since i wrote this code:
var subCrit = DetachedCriteria.For<EntityB>
.Add<EntityB>(e => e.AnotherField > 0)
.SetProjection(LambdaProjection.Count<EntityB>(e => e.Id).As("TheCount"));
var crit = Session.CreateCriteria<EntityA>
.SetProjection(LambdaProjection.GroupProperty<EntityA>(e => e.Id).As("Id),
Projections.SubQuery(subCrit));
if i execute this criteria i obtain the following SQL:
select a.Id as Id , (select count(b.Id) from TableB b where b.AnotherField > 0) as TheCount from TableA a
as u can see , it's very close to what i'm trying to achieve...the problem (and it's definetely a big problem :D)
is that theres no link between the subquery and the entities of TableA ( where b.FKa = a.Id ).
I cant find a way to correlate the subquery to the external query via criteria.
Any suggestions ?
Ta a lot
Alessandro
EDIT:
changing the point of view i also could do something like that:
var crit = Session.CreateCriteria<EntityA>()
.CreateAlias<EntityB>(a => a.B, () => b);
.SetProjection(LambdaProjection.Count<A>(a => b.Id).As("TheCount"),
.SetProjection(LambdaProjection.GroupProperty<EntityA>(a => a.Id));
and this is generating the following sql:
select count(b.Id) as TheCount, a.Id as IDa
from TableA a left outer join TableB b
on a.Id = b.FKa
group by a.Id
but here you can see that the additional where clause b.AnotherField > 0 is missing , and i dont know how to insert it just for the count.
Hope it's clear , thanks again
Here is the solution:
var condition = Expression.Gt("b.AnotherField",0);
var conditionalProjection = Projections.Conditional(condition, Projections.Constant(1), Projections.Constant(0));
crit = Session.CreateCriteria<EntityA>()
.CreateAlias<EntityB>(a => a.B, () => b);
.SetProjection(Projections.Count(conditionalProjection).As("TheCount"),
(LambdaProjection.GroupProperty<EntityA>(a => a.Id));
and this is the sql generated:
select count(case b.AnotherField > 0 then 1 else 0 end) as TheCount , a.Id
from TableA a inner join TableB b
hope it can be useful
cheers
Alessandro
enter code here
My suggestion is to change the SQL Statement
SELECT a.is, count(a.id)
FROM TableA a
JOIN TableB b ON a.id = b.fka AND b.af > 0
GROUP by a.ID
And create for it easy criteria.