I have a query in Oracle which i am trying to convert into linq. I think I am almost there. Here is the query in Oracle. I had quick problem with left outer joins in the query. Please consider this too in the question. My main problem is I can't write using this count, distinct for different columns in the table.
SELECT COUNT(DISTINCT claimant_id || rqst_wk_dt || claim_id) AS no_of_weeks_compensated
, SUM(pmt_am) AS total_payments
, COUNT(DISTINCT claimant_id || claim_id)
FROM (SELECT c.claimant_id
, c.claim_id
, c.rqst_wk_dt
, a.pmt_am
FROM ui_mon_hdr d
INNER JOIN ui_rqst_wk_ctrl c
ON d.claimant_id = c.claimant_id
AND d.claim_id = c.claim_id
LEFT OUTER JOIN ui_dstb_pmt a
ON c.claimant_id = a.claimant_id
AND c.claim_id = a.claim_id
AND c.rqst_wk_dt = a.rqst_wk_dt
AND a.rcpnt_id = 'CLMNT'
LEFT OUTER JOIN ui_claim_pmt b
ON c.claimant_id = b.claimant_id
AND c.claim_id = b.claim_id
AND warrant_dt BETWEEN '1 June 2011' AND '30 June 2011'
AND b.status_cd = 'PAID'
AND a.rcpnt_id = b.rcpnt_id --AND A.PMT_NU = B.PMT_NU
LEFT OUTER JOIN ui_auth_pmt e
ON c.claimant_id = e.claimant_id
AND c.claim_id = e.claimant_id
AND c.rqst_wk_dt = e.rqst_wk_dt
AND d.mon_seq_nu = e.mon_seq_nu
WHERE c.rqst_wk_dt BETWEEN '1 June 2011' AND '30 June 2011'
AND d.bspd_type_cd = 'ALTR')
Above is the query which I have in Oracle and run in TOAD. Below is the query in LINQ. I have just done internal select statement and I wonder how to implement the select count and distinct for the query:
var enddate = Convert.ToDateTime("6/30/2011");
var Altquery = from D in UiMonHdr
join C in UiRqstWkCtrl on new {D.ClaimantId, D.ClaimId} equals new {C.ClaimantId, C.ClaimId}
join A in UiDstbPmt on new {C.ClaimantId, C.ClaimId , C.RqstWkDt} equals new {A.ClaimantId, A.ClaimId, A.RqstWkDt}
where A.RcpntId.Contains("CLMNT")
join B in UiClaimPmt on new {C.ClaimantId, C.ClaimId, A.RcpntId} equals new {B.ClaimantId, B.ClaimId, B.RcpntId}
where B.StatusCd.Contains("PAID") && B.WarrantDt >= startdate && B.WarrantDt <= enddate
join E in UiAuthPmt on new {C.ClaimantId, C.ClaimId , C.RqstWkDt} equals new {E.ClaimantId, E.ClaimId, E.RqstWkDt}
where C.RqstWkDt >= startdate && C.RqstWkDt <= enddate && D.BspdTypeCd.Contains("ALTR")
select new {ClaimantId = C.ClaimantId, ClaimId = C.ClaimId, PmtAmt = A.PmtAm, RqstWkDt = C.RqstWkDt}; Altquery.Dump();
Related
Case1:
var query = (from p in db.TblPost where (from q in db.TblThread
where q.LocationLocationid == locationID && q.CategoriesCategoryid == categoryID select q.Threadid).Contains(p.ThreadThreadid)
join r in db.TblThread on p.ThreadThreadid equals r.Threadid join s in db.TblUser on p.UserUserid equals s.Userid join t
in db.TblCategories on r.CategoriesCategoryid equals t.Categoryid join u in db.TblLocation on r.LocationLocationid
equals u.Locationid orderby r.CreatedTime descending
select new { p, r.Subject, r.EventAddress, r.EventClosetime, r.EventDate, r.EventDuration, r.EventStarttime,
r.EventTitle, r.IseventAllday, r.TargetUsers, r.CreatedTime, s.FirstName, s.MiddleName, s.LastName, t.Name,
u.Locationname, r.Isreadonly }).ToList();
OR
Case 2:
List<TblPost> _tblPost = new List<TblPost>();
_tblPost = (from p in db.TblPost select p).ToList();
List<TblThread> _tblThread = new List<TblThread>();
_tblThread = (from p in db.TblThread select p).ToList();
List<TblUser> _tblUser = new List<TblUser>();
_tblUser = (from p in db.TblUser select p).ToList();
List<TblLocation> _tblLocation = new List<TblLocation>();
_tblLocation = (from p in db.TblLocation select p).ToList();
List<TblCategories> _tblCategory = new List<TblCategories>();
_tblCategory = (from p in db.TblCategories select p).ToList();
var query = (from p in _tblPost where (from q in _tblThread where q.LocationLocationid == locationID
&& q.CategoriesCategoryid == categoryID select q.Threadid).Contains(p.ThreadThreadid) join r in _tblThread on p.ThreadThreadid
equals r.Threadid join s in _tblUser on p.UserUserid equals s.Userid join t in _tblCategory on r.CategoriesCategoryid equals
t.Categoryid join u in _tblLocation on r.LocationLocationid equals u.Locationid orderby r.CreatedTime descending
select new { p, r.Subject, r.EventAddress, r.EventClosetime, r.EventDate, r.EventDuration, r.EventStarttime,
r.EventTitle, r.IseventAllday, r.TargetUsers, r.CreatedTime, s.FirstName, s.MiddleName, s.LastName,
t.Name, u.Locationname, r.Isreadonly }).ToList();
I've these two codes which results the same. But, in case 2 i'm getting the result some quickly as compared to case 1. Which one should i need to follow?
Shall i follow case 2? is it right way to get huge data in Web.API?
I am trying to write LINQ query to generate below SQL query. I know it looks like assignment, but tried few syntax which generated wrong query.
select pm.Profile_Number,PD.Line_Abbrev,PD.Group_Code from Profile_Detail PD
INNER JOIN Profile_Master PM ON pd.profile_id = pm.profile_id
LEFT JOIN
(
SELECT Field_Abbr,Group_Code FROM vw_Group_Code
where ((US=1 AND Group_US_Obsolete <> 1) OR (CA=1 AND Group_CA_Obsolete <> 1) OR (MX=1 AND Group_MX_Obsolete <> 1))
)gcv ON PD.Line_Abbrev = gcv.Field_Abbr AND PD.Group_Code = gcv.Group_Code
WHERE PD.Profile_Id IN(42) AND gcv.Field_Abbr IS NULL
Tried version :
(from pd in _context.ProfileDetail
join pm in _context.ProfileMaster on pd.ProfileId equals pm.ProfileId
join vla in _context.VwLineAbbrvs on pd.LineAbbrev equals vla.LineAbbrev into gc
from vla in gc.DefaultIfEmpty()
where profileNumber.Contains(pd.ProfileId.ToString()) &&
((vla.IsUS && vla.USObsolete) || (vla.IsCA && !vla.CAObsolete) || (vla.IsMX && !vla.MXObsolete))
select new ObsoleteLineDetail
{
LineAbbrv = pd.LineAbbrev,
GroupCode = pd.GroupCode,
ProfileNumber = pm.ProfileNumber
}).ToList();
How to convert this below query to equivalent teradate query. I tried but results varies a lot.
select il.domainN as listname, il.SourceID, ns.sourcename, cbo.customerid, cu.username, hv.domainN as HTname
, nvl((select 1
from mydb.customerPP cpp
where cbo.customerid = cpp.customerid
and NOT EXISTS (select 1 from mydb.customerPI cpt where cpp.customerid = cpt.customerid)
and trunc(cpp.startdate) <= sysdate
group by cpp.customerid),0) as BBID
from mydb.customerBO cbo
join mydb.customers cu on cbo.customerid = cu.customerid
join mydb.inv il on cbo.domainN = il.domainN
join mydb.Sources ns on il.SourceID = ns.SourceID
left join mydb2.HT hv on (il.domainN = hv.domainN
and hv.sDate+1 >= il.dDate
and il.dDate+1 >= hv.sDate)
where cbo.customerBOID = 1
and cu.statusid = 1
and il.sourceTID = 2
and il.joinbydate >= cbo.cDate
and trunc(il.dDate) = trunc(sysdate)
Thanks.
Use coalesce instead nvl and compare explain statements to see whether you have left outer joins converted to inner joins for nvl on Teradata.
Trying to convert a SQL Query to Linq:
SELECT GrossInvoiceAmount, C.SumOfPayments
FROM invoice
OUTER APPLY(
SELECT SUM(SumOfPayments) AS SumOfPayments FROM vw_sumOfPayments
WHERE vw_sumOfPayments.PaymentDate = '01/01/2010 00:00:00'
AND vw_sumOfPayments.InvoiceId = invoice.InvoiceId
) C
WHERE LastTransmitDate = '01/01/2010 00:00:00'
I have this in my C# code. It runs and gives results, but the invoice.GrossInvoiceAmount amount is wrong. Any ideas? Thanks.
var InvoiceQuery = (from invoice in this.Context.Invoices
join payment in this.Context.vw_sumOfPayments.Where(pay => DbFunctions.TruncateTime(pay.PaymentDate) >= startdate
&& DbFunctions.TruncateTime(pay.PaymentDate) <= enddate)
on invoice.InvoiceID equals payment.InvoiceID into pays
select new InvoiceModel{
InvoiceTypeId = invoice.InvoiceTypeId,
InvoiceNumber = invoice.InvoiceNumber,
InvoiceDate = invoice.InvoiceDate,
InvoiceAmount = invoice.GrossInvoiceAmount,
PaymentAmount = pays.AsEnumerable().Sum(o => o.SumOfPayments)
});
I am unable to execute the below linq.
var items( from p in Patients
join q in MURWorksheets on p.PatientId equals q.PatientId into step1
from s in step1.DefaultIfEmpty()
join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2
from s2 in step2.DefaultIfEmpty()
select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();
Here is the sql for the same for your reference.
select a.FirstName,b.MURDate,c.MURQuestionnaireId,c.MURWorksheetAnswersID from Patients as a
left join MURWorksheet as b on a.PatientId = b.PatientId
left join MURWorksheetAnswers as c on b.MURWorksheetId = c.MURWorksheetId
You are missing an equals sign in your pasted code:
var items = (from p in Patients
join q in MURWorksheets on p.PatientId equals q.PatientId into step1
from s in step1.DefaultIfEmpty()
join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2
from s2 in step2.DefaultIfEmpty()
select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();