How to achieve the equivalent in Slick?
select * from table1 where col1 = 1 AND (col2 = 2 or col3 = 3)
This doesn't work:
val action = table.filter(_.col1 === 1 && (_.col2 === 2 || _.col3 === 3)).result
You cannot use the short hand in this case. Try this:
val action = table.filter( x => x.col1 == 1 && (x.col2 == 2 || x.col3 == 3)).result
Related
I have a problem, I have a query in postgresql and I need to do a SUM, however, it returns me a total of 4 lines because the ids of my routes are different, and I need to add the "totalChecklists" and return the amount of all in one line.
select "users"."id",
count((cycles.adherence >= 100 and cycles.justified = false) or null) as "routesDone",
count((cycles.adherence < 100 and cycles.opened = false) or null) as "routesNotDone",
count((cycles.opened = true and cycles.adherence < 100) or null) as "routesOpened",
count(cycles.justified or null) as "routesJustified",
routes."totalSpots" AS "totalCheck",
count(cycles.id) as "routesOnPeriod",
(
count((cycles.adherence >= 100 and cycles.justified = false) or null) +
count((cycles.adherence < 100 and cycles.opened = false) or null) +
count((cycles.opened = true and cycles.adherence < 100) or null) +
count(cycles.justified or null)
) as "routesTotal",
0 as "routesDoneLate",
0 as "routesLate",
SUM(case when cycles.adherence >= 100 then routes."totalAssets" else 0 end) AS "coveredAssets",
SUM(case when cycles.adherence < 100 then routes."totalAssets" else 0 end) AS "uncoveredAssets"
from "routes"
inner join "cycles" on "routes"."identifier" = "cycles"."routeIdentifier"
inner join "workspaces" on "cycles"."workspaceId" = "workspaces"."id"
inner join "users" on "users"."id" = "cycles"."inspectorId"
where "routes"."deleted_at" is null
and "cycles"."deletedAt" is null
and "workspaces"."deleted" = false
and "cycleStartAt" <= '2022-07-11T02:59:59Z'
and "cycleEndAt" >= '2022-04-12T03:00:00Z'
and "users"."id" = 'b67830a7-39fc-4ad5-bf26-07a43dcd3676'
and "routes"."contextPath" like '/malicious-interaction-murder-32%'
and "routes"."deleted_at" is null
group by
users.id,
routes.id
My return:
Sorry for my bad english, I hope I managed to be very clear.
Thank you very much in advance
I would like to rewrite teradata code to spark dataframes using scala, facing an error " when() cannot be applied once otherwise() is applied ", Help is appreciated.
Teradata
CASE WHEN
CASE WHEN ID <> cid OR Group <> sg OR ead IS NULL THEN 1
ELSE
CASE WHEN HFlag <> hg AND ad + TBreak < ActivityDate THEN 1
ELSE
CASE WHEN HFlag = hg AND HFlag = 1 THEN
CASE WHEN ead + 1 < ActivityDate THEN
CASE WHEN ead + TBreak < ActivityDate THEN 1
ELSE 0 END
ELSE 0 END
WHEN HFlag = hg AND HFlag = 0 THEN
CASE WHEN ead + TBreak < ActivityDate THEN 1
ELSE 0 END
ELSE 0 END
END
END = 1 THEN Row_Number() Over(ORDER BY ID, Group, ActivityDate, HFlag)
ELSE 0 END AS arctic
I tried in the below way.
val windowRank = Window.orderBy('ID, 'Group, 'ActivityDate, 'HFlag)
df.withColumn("arctic",
when(when(col("ID") =!= col("cid") || col("Group") =!= col("sg") || col("ad").isNull, 1)
.when(col("HFlag") =!= col("hg") && (col("ad") + col("TBreak")) < col("ActivityDate"), 1)
.when(col("HFlag") === col("hg") && col("HFlag") === 1,
when((col("ad") + 1) < col("ActivityDate"),
when((col("ad") + col("TBreak")) < col("ActivityDate"), 1)
.otherwise(0))
.otherwise(0)
.when(col("HFlag") === col("hg") && col("HFlag") === 0, when((col("ad") + col("TBreak")) < col("ActivityDate"), 1))
.otherwise(0))
.otherwise(0) === 1, row_number() over windowRank)
.otherwise(0))
You have missed a closing bracket
val windowRank = Window.orderBy('ID, 'Group, 'ActivityDate, 'HFlag)
df.withColumn("arctic",
when(when(col("ID") =!= col("cid") || col("Group") =!= col("sg") || col("ad").isNull, 1)
.when(col("HFlag") =!= col("hg") && (col("ad") + col("TBreak")) < col("ActivityDate"), 1)
.when(col("HFlag") === col("hg") && col("HFlag") === 1,
when((col("ad") + 1) < col("ActivityDate"),
when((col("ad") + col("TBreak")) < col("ActivityDate"), 1)
.otherwise(0))
.otherwise(0)) // <-- missing close bracket
.when(col("HFlag") === col("hg") && col("HFlag") === 0, when((col("ad") + col("TBreak")) < col("ActivityDate"), 1))
.otherwise(0))
.otherwise(0) === 1, row_number() over windowRank)
.otherwise(0))
In Entity Framework Core I need to express the following SQL query by using the method syntax instead.
select *
from [Rule] as t1
where
(
select count(*)
from [Rule] as t2
where t1.ProductID = t2.ProductID
and t2.Priority > t1.Priority
) = 0
The query returns the records with the highest Priority for each ProductID.
Is there a way?
Thanks.
Try this :
var rules = db.Rules.Where(
a=>db.Rules.Where(
b => b.ProductId == a.ProductId && b.Priority > a.Priority
).Count() == 0
);
if you'd like use Linq syntax :
var rules = from a in db.Rules
where (
from b in db.Rules
where a.ProductId == b.ProductId
&& b.Priority > a.Priority
select b
).Count() == 0
select a;
I have this select which works in T-SQL, but need to convert to Entity Framework. How can I use case in Entity Framework?
select
*
from
Contrato c
where
c.QtdParcelasFalta > 0
and (case
when c.DiaEspecificoMarcar = 1
then c.DiaEspecifico
when c.DiaEspecificoMarcar = 0
then (select convert(int, substring(DataCobranca, 2, 2))
from Contrato
where Contrato.Id = c.Id) -
(select e.DataProcessamentoNota
from Contrato t
inner join PedidoVenda p on p.id = t.PedidoVendaId
inner join Empresas e on p.EmpresaID = e.Id
where p.id = t.PedidoVendaId
and t.id = c.Id and p.EmpresaID = 1)
end) = (SELECT DAY(GETDATE()))
I tried it like this, but it didn't work out, did not pass the correct values, because I'm not aware of pass to Entity Framework:
var contrato = db.Contrato
.Include(a => a.PedidoVenda)
.Include(a => a.Cliente)
.Where(a => a.PedidoVenda.EmpresaID == model.EmpresaID
&& a.Cancelado == false
&& a.DiaEspecificoMarcar == true
&& a.DiaEspecifico == int.Parse(DateTime.Now.ToString("dd")) ||
(int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota)
== int.Parse(DateTime.Now.ToString("dd"))
).ToList();
You are actually on the right track. You just need to add when conditions to your lef and right conditions like this:
var contrato = db.Contrato.Include(a => a.PedidoVenda).Include(a => a.Cliente).Where
(a => a.PedidoVenda.EmpresaID == model.EmpresaID && a.Cancelado == false &&
a.DiaEspecificoMarcar == false && a.DiaEspecifico == int.Parse(DateTime.Now.ToString("dd")) ||
( a.DiaEspecificoMarcar == true && int.Parse(a.DataCobranca.Substring(1, 2)) - a.PedidoVenda.Empresa.DataProcessamentoNota)
== int.Parse(DateTime.Now.ToString("dd"))
).ToList();
I also suggest you use DateTime.Now from a variable to be sure both int.Parse gets the same input
Is it possible to convert the following query into ICriteria or LINQ, and if so, how?
SELECT Test.personid
FROM
(
SELECT r.PersonId AS personid, e.ActivityId
FROM Event e
INNER JOIN Registration r ON e.Id = r.EventId
WHERE e.ActivityId IN (1, 2)
GROUP BY r.PersonId, e.ActivityId
) AS Test
GROUP BY Test.personid
HAVING COUNT(Test.ActivityId) >= 2
var result = from r in session.Query<Registration>()
where r.Event.ActivityId == 1 || r.Event.ActivityId == 2
group r by r.Person into g
where g.Min(x => x.Event.ActivityId) != g.Max(x => x.Event.ActivityId)
select g.Key
the following states, take all Persons wich have different activityIds in there groups which is equivalent to
ActivityId == 1 || ActivityId == 2
Having Count(r.Event.ActivityId) >= 2