select *
from impact
where (usedin&1)=1
and impactid not in
(select impactid
from responsetime
where scontractid= scontractid )
I am learning LINQ.
I want to write this query in LINQ.
I'm told that a SQL query like this will be faster.
select *
from impact i
left outer join responsetime r on i.impactid = r.impactid
where (usedin&1)=1
and scontractid= scontractid
and r.impact is null
Converting that to linq, we get:
from i in impact
from r in responsetime.Where(rr=>i.impactid = rr.impactid).DefaultIfEmpty()
where (i.usedin&1)=1
and i.scontractid= scontractid
and r.impact is null
select i
Related
I have this SQL query and I need to convert it SqlKata
SELECT VVIAGGIO AS VIAGGIO, ISNULL(AEAN,'') AS EAN
FROM
SALDI V INNER JOIN ARTICOLI A ON VARTI=AARTI
INNER JOIN ORDI OT ON VSTAB=OTSTAB AND VMAGA=OTMAGA AND VAGG=OTRAGG
WHERE VORDI ='21'
AND VHOST ='68'
ORDER BY VPROG, AARTI
I don't know how to structure it because here is ISNULL(), INNER JOIN..
I already checked SqlKata select instruction.
Any suggestions on how to optimize and convert this query in SqlKata?
here is what you need:
var query = new Query("SALDI as V")
.Join("ARTICOLI as A","A.ARTI","V.ARTI")
.Join("ORDI as OT",j => j.On("OT.STAB","V.STAB")
.On("V.MAGA","OT.MAGA")
.On("V.AGG","OT.RAGG")
)
.Where("V.ORDI","21")
.Where("V.HOST","68")
.Select("V.VIAGGIO")
.SelectRaw("ISNULL(V.AEAN,'') as EAN")
.OrderBy("V.PROG", "A.ARTI")
SELECT Musteriler.MusteriAdiSoyadi,Sum(Odemeler.OdemeTutari) AS ToplamOdeme FROM Odemeler
Right JOIN Musteriler ON Odemeler.OdeyenId = Musteriler.Id
GROUP BY Musteriler.MusteriAdiSoyadi,Musteriler.KayitTarihi ORDER BY Musteriler.MusteriAdiSoyadi;
How do I convert the running sql query to linq
I am new to building sql queries and could use some help. I built a query that works fine as a standalone query. The problem is I need to use it in a report using ExecuteScalar function and nested queries are not allowed, I tried to rebuild using joins but I seem to be lost.
Can anyone help me "un-nest" this query?
SELECT
StockType2Job.Loaded
FROM
StockType2Job
WHERE
StockType2Job.IdStockType =
(SELECT StockType.IdStockType
FROM StockType
WHERE StockType.Number = '1001716.00')
AND
StockType2Job.IdStockType2JobGroup =
(SELECT StockType2JobGroup.IdStockType2JobGroup
FROM StockType2JobGroup
WHERE StockType2JobGroup.IdJob =
(SELECT Job.IdJob
FROM Job
WHERE Job.Number = '18-0085.02'
AND StockType2JobGroup.Caption = 'Breakout Room 1'))
Any help appreciated. Thanks
this query should work(on Oracle DB):
SELECT
StockType2Job.Loaded
FROM
(((StockType2Job a JOIN StockType b ON a.IdStockType=b.IdStockType)
JOIN StockType2JobGroup c ON a.IdStockType2JobGroup=c.IdStockType2JobGroup)
JOIN Job d ON c.IdJob=d.IdJob)
WHERE
b.Number = '1001716.00' AND
d.Number = '18-0085.02' AND
c.Caption = 'Breakout Room 1'
I am writing my sub query using linq in my application. I wrote my sql sub-query in sql server. When I execute this query in SQL Server the result give perfect and the sql query will be like
My SQL Server:
select Row_Number() Over(OrderBy Mmname) as Sno,Mmname,Mmcardno,Mmdob,MmEmail,(Select SUM(MSSRNETAMT) from MSCAS where MSCAS.MSSFORMNO = MSMEM.MmCardno and MSCAS.MSSRBILLDT between '01/01/2016' and '30/12/2016') Billopen,(Select SUM(MSSRNETAMT) from MSCAS where MSCAS.MSSFORMNO = MSMEM.MmCardno and MSCAS.MSSRBILLDT between '01/01/2016' and '30/12/2016') BillForm,Mmredpv,Msdval,Mmcontact,Mmdate,Mmaddr,MmCuser,MmCuserdt,Mmusercd,
Mmuser,Mmuserdt,Cast(Mmcntrn as int ) as Mmcntr from MSMEM inner join MSSCHEDET on MSMEM.Mschuid = MSSCHEDET.MSDID
My linq query
Var query = (from Ms in db.msmems join Mss in db. Msschedet on Ms. Mechuid equals mss. MSDID select new
{
name = Ms. Mmname,
Billopen = (from mc in db. MSCAS where Ms. Mmcardno = mc. Mssformno) select mc.MSSRNETAMT);
}):
This only I tried but I am getting error in bill open it is amount in that I am getting error
This is my SQL query. I tried to convert this sql query in linq. But I am failed many times, I got an error.
Could you try something like this? Without understanding your table structure better, I took a shot at optimizing this. Is there no reason that you couldn't do a second join to your MSCAS table?
var query = (from Ms in db.msmems
join Mss in db.Msschedet on Ms.Mechuid equals mss.MSDID
join Mc in db.MSCAS on Ms.Mmcardno equals Mc.Mssformno
select new {
name = Ms.Mmname,
Billopen = Mc.where(x=>x.MSSRBILLDT >= '01-01-2016' and x.MSSRBILLDT <= '12-30-2016').Sum(z=>z.MSSRNETAMT)
}
);
I am currently helping out a Rails 3.2 project.
Got this complex query that I would like to be in parameterized format.
project.supporters.connection.select_all( "SELECT t2.* FROM
(SELECT MAX(id) AS max_supporter_id FROM supporters WHERE
supporters.project_id = ? GROUP BY supporters.supporter_id) AS
t1 INNER JOIN supporters AS t2 ON t1.max_supporter_id =
t2.id", 1).
Above query is what I would like to be like but I know it doesn't work because select_all doesn't do parameterized query.
So far this is what I got:
project.supporters.group(:supporter_id).maximum(:id) which will
generate this sql: SELECT MAX("supporters"."id") AS maximum_id,
supporter_id AS supporter_id FROM "supporters" WHERE
"supporters"."project_id" = 1 GROUP BY supporter_id
But I haven't made significant progress to do the inner join and finally have the outer SELECT.
Can anyone help?
project_id = 1
supporters = Arel::Table.new(:supporters)
max_id = supporters.project(supporters[:id].maximum.as('max_supporters_id')).group(supporters[:supporter_id])
query = supporters.project(max_id)
.join(supporters[:project_id])
.on(supporters[:max_supporter_id].eq(supporters[:id]))
.where(supporters[:project_id].eq(project_id))
query.to_sql
# => "SELECT (SELECT MAX(\"supporters\".\"id\") AS max_supporters_id FROM \"supporters\" GROUP BY \"supporters\".\"supporter_id\") FROM \"supporters\" INNER JOIN \"supporters\".\"project_id\" ON \"supporters\".\"max_supporter_id\" = \"supporters\".\"id\" WHERE \"supporters\".\"project_id\" = 1"
query.to_a # executes the query and returns records
For more informations about building the complex queries in rails, you could read there https://github.com/rails/arel
I wish it helps.