How to convert this to LINQ - mysql

I want to convert this query to LINQ.
select sup.firstname, sup.lastname, sup.bankaccountnumber, tbl.total from (select nfcids_id, sum(purchasecost) as total
from tbl_milkpurchases
group by nfcids_id) tbl inner join tbl_nfcids nfc on tbl.nfcids_id = nfc.id inner join tbl_suppliers sup on nfc.suppliers_id = sup.id

Try this query
var obj= from a in Context.tbl_milkpurchases.GroupBy(x=>x.nfcids_id).Select(x=>new{x.nfcids_id,total=Sum(x.purchasecost)})
from b in Context.tbl_nfcids.where(x=>x.id==a.nfcids_id)
from c in Context.tbl_suppliers.where(x=>x.id==b.suppliers_id)
Select new{c.firstname, c.lastname, c.bankaccountnumber, a.total}

Related

MySQL Subquery Optimization with shared subquery

From these tables I have written this subquery and its giving results as per requirements.
Needs expert guidence to improve this query or if we can also be able to use join for these tables.
Query:
select ps,st from pac where con in (select
config from config where logi in
( select id from logicalnode where physi
in (select id from ysicalnode where mas =11)));
SELECT
payloadstr
,starttime
FROM packetdb.packet
INNER JOIN packetdb.configuration
ON packetdb.packet.configid = packetdb.configuration.idconfig
INNER JOIN packetdb.logicalnode
ON packetdb.configuration.idconfig = packetdb.logicalnode.id
INNER JOIN packetdb.physicalnode
ON packetdb.logicalnode.physicalnodeid = packetdb.physicalnode.id and packetdb.physicalnode.macaddress=117769729
You could try using exists:
select payloadstr,starttime from packetdb.packet p
where exists(select 1 from packetdb.configuration c
where p.configid = id
and exists(select 1 from packetdb.logicalnode l
where c.logicalnodeid = id
and exists(select 1 from packetdb.physicalnode
where macaddress = 117769729
and l.physicalnodeid = id)
Using Left join
select payloadstr,starttime
from packet
left join Configuration on Configuration.IDconfig = packet.configID
left join logicalnode on logicalnode.ID = Configuration.logicalnodeid
left join physicalnode on physicalnode.ID = logicalnode.physicalnodeid
where macaddress =117769729
You can try below - using JOIN
select payloadstr,starttime
from packetdb.packet a inner join packetdb.configuration b on a.configid=b.idconfig
inner join packetdb.logicalnode c on logicalnodeid=c.id
inner join packetdb.physicalnode d on physicalnodeid=d.id
where macaddress =117769729
Try this
select pa,sta
from
pack p
INNER JOIN
confi c
ON
p.confi = c.idco
INNER JOIN
logice l
ON
c.logic = l.id
INNER JOIN
physiode pn
ON
l.physicalnodeid = pn.id
WHERE macaddress =123

SQL - return rows that do not have a certain value

looking for a bit of help here if possible?
I have the following query:-
On or database we have a table called Linkfile, in this table are "Types" all beginning with "YG". I need to return those rows that do not have the type of "YG8" but just cannot seem to do it. I know ill need to use a sub query but am stuck!
This is my code and the fields I need to return. I just need to only show those that do not have the lk.type of "YG8"
select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
inner join linkfile lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016'
and lk.type like 'YG%' and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type
Use below query :
select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type,lk.parent_object_ref
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
left join (select displayname, type,parent_object_ref from linkfile where lk.type like 'YG8%' )lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016' and lk.parent_object_ref is null
and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type;
I've used left join on linkfile with type like 'YG8%' and fetching the only records which are not matched
I think you can just replace the
lk.type like 'YG%'
with the following:
(lk.type >= 'YG' and lk.type <'YG8') or (lk.type > 'YG8' and lk.type <='YGZ')
this should accomplish what you are trying to do and also avoid using "like" which is less efficient (assuming you have an index on lk.type, at least).
You may refine this a bit by knowing which are the possible values of lk.type of course. I.e. what are the extremes for the YG "subtype"? YG00-YG99? YG-YGZ?
(Be especially careful if you may have YG81 or YG87 for example, because then my clause will not work properly... on the other hand if your YG subtype can have values like YG34 it would have been better to use YG08 instead of YG8)

UPDATE table by INNER JOIN and where clause

Got 2 tables as shown above. I want to update 'leaverecord.Consumed' from 'approved.Consumed' WHERE the leaverecord.name = approved.name and leaverecord.leavetype = approved.leavetype. Tried below query by getting error of 'Invalid use of group function
UPDATE leaverecord r INNER JOIN approved a
ON r.name = a.name
SET r.Consumed = SUM(DATEDIFF(a.todate,a.fromdate))
WHERE r.leavetype = a.leavetype AND
r.name = a.name
Calculate the consumed in a subquery and INNER JOIN it with leaverecord and then do the UPDATE like below. No WHERE clause needed.
Try this:
update leaverecord r
inner join (
select name, leavetype, sum(datediff(todate,fromdate)) consumed
from approved
group by name, leavetype
) a on r.name = a.name
and r.leavetype = a.leavetype
set r.consumed = a.consumed;

Sort data in inner join query

select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
order by
Ad_Date_Created
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
I keep getting an error that multi part data can not be bound. Please help to correct query
Try this:
select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
order by
Ad_Date_Created
The syntax of your SQL statement is wrong. An ORDER BY clause should come after the JOIN's

SQl join with count group by

I have two tables
one is
Receivesms(Matchdate,Matchtime,Keyword,SmsMessage,Mobileno)
another is
QAoptios(Keyword,Question,Answeroption,Matchdate,Matchtime)
I need to find the count of SmsMessage which have the value same as Answeroption on same Matchdate,Matchtime and same Keyword. and also i need to filter on Receivesms.Matchdate='2012-12-25 00:00:00.000'
SELECT
q.AnswerOption,
COUNT(SmsMessage) TheCount
FROM Receivesms r
INNER JOIN QAoptions q ON r.Matchdate = q.Matchdate
AND r.Matchtime = q.Matchtime
AND r.Keyword = q.keyword
GROUP BY q.AnswerOption;
use can use:
select count(*) from Recievesms r,QAoptios q
where
r.Matchdate = q.Matchdate
and
r.Matchtime = q.Matchtime
and
r. Keyword =q.Keyword
and
r.SmsMessage = q.Answeroption;
or
select count(*) from from Recievesms as r
inner join
QAoptios as q
on
r.SmsMessage = q.Answeroption;
try this one
SELECT count(*) from Receivesms as a inner join QAoptios as b on a.keyword=b.keyword
and a.matchtime=b.matchtime and a.matchdate=b.matchdate and a.value=b.answeroption
SELECT COUNT(R.SmsMessage)
FROM Receivesms R
INNER JOIN QAoptios Q ON
R.Keyword=Q.Keyword AND
R.Matchdate=Q.Matchdate AND
R.Matchtime=Q.Matchtime AND
R.SmsMessage=Q.AnswerOption
Or you can add GROUP BY clause if you want to show AnswerQuestion, like this:
SELECT Q.AnswerOption, COUNT(R.SmsMessage) as TheCount
FROM Receivesms R
INNER JOIN QAoptios Q ON
R.Keyword=Q.Keyword AND
R.Matchdate=Q.Matchdate AND
R.Matchtime=Q.Matchtime AND
R.SmsMessage=Q.AnswerOption
GROUP BY Q.AnswerOption