LINQ Join and performing aggregate functions - linq

I am facing issue in writing LINQ query to perform join on three tables and then performing aggregate functions on the rows. Kindly do provide some help.
I have three tables
Table 1: Students (Id, Name)
Table 2: Subject (SubID, Title, Id)
Table 3: Grade (Id, SubID, marks)
I have to write LINQ query to get the results as following
Count of Students table rows
Count of Grade table rows
Sum of
marks of all rows in Grade table
I am writing query as following but it is not up to the mark as i feel it is not correct.
var _Count = from student in _context.Students
join subject in _context.Subject on student.Id equals subject.Id
join grade in _context.Grade on subject.SubID equals grade.SubID
// How to group them?
select new { //How to take and return the counts?};

Related

oracle select query based on other table row level condition

I want to find the orders number from table#orders where DelivaryDateRevision less than max revisions from each country(table#maxrevisions). Countrycode is not the foreign key to the other table.
Can I fetch the orders table records if the country code is missing in the maxrevisions table.
Table: orders
OrderNumber | CountryCode | DelivaryDateRevision
123--------------- IN-------------------9
234--------------- US-------------------3
238-------------- IN------------------ 3
table: maxrevisions
CountryCode| MaxRevision
IN ---------------6
US--------------- 4
My query:
SELECT distinct o.ordernumber,o.countrycode
FROM orders o
left outer join maxrevisions m
on o.CountryCode=m.CountryCode
and
o.DelivaryDateRevision<rs.MaxRevision;
but I am getting the wrong result. Can I get any help here?
Your major omission seems to be a WHERE clause which compares the two revisions:
SELECT
o.ordernumber,
o.countrycode
FROM orders o
LEFT JOIN maxrevisions m
ON o.CountryCode = m.CountryCode
WHERE
o.DelivaryDateRevision < m.MaxRevision OR m.MaxRevision IS NULL;
Demo
Select
ordernumber,
countrycode,
deliverydateversion
from orders o
where deliverydateversion >
(
select max(revision)
from maxrevisiontab
where countrycode= o.countrycode
)
Please change the table names and column names as per your structure.

Display all the fields associated with the record using Impala

Suppose, I have a student table with some fields in impala. Imagine there is a field called total_mark and I should find the student details with maximum mark from each branch.
My table is like this :-
In this table I have to get the details of student with maximum marks from each department.
My query will be like this :-
select id,max(total_marks) from student_details group by department;
But using this query I can get only the id and total_marks. Provided there can be students with same name,age I can't group with fields like age,name .
So how should I query the table to get all the details of top student from each department ??
Thanks in advance.
You can make use of the JOIN concept
select stu.*
from student_details stu
join
( select department,max(total_marks) as max
from student_details
group by department
) rank
on stu.department=rank.department and stu.total_marks=rank.max;

joining two tables with one table in oracle

I have a table pa_master_details and lov_details.
In the pa_master_details table, I have role_comp_emp_final_rating and role_comp_lm_final_rating columns.
In the lov_details table I have a column rating lov_value and lov_text_en.
I need to join role_comp_emp_final_rating with the lov_value column to get lov_text_en
Based on this lov_value, I need to show lov_text_en from the lov_details table.
So I wrote a query like this and am getting the result for emp rating:
SELECT p.employee_number,
p.role_comp_emp_final_rating,
lov_text_en,
p.role_comp_lm_final_rating
FROM pa_master_details P, lov_details L
WHERE p.role_comp_emp_final_rating = l.lov_value
AND p.employee_number = 34570
Similarly, I need to show lov_text_en for role_comp_lm_final_rating from the lov_details table by joining
role_comp_lm_final_rating with lov_value in the same query.
How do I do it?
I think this is what you're asking for:
SELECT p.employee_number,
p.role_comp_emp_final_rating,
lemp.lov_text_en AS "emp_lov_text",
llm.lov_text_en AS "lm_lov_text",
p.role_comp_lm_final_rating
FROM pa_master_details p
JOIN (SELECT lov_text FROM lov_details) lemp ON p.role_comp_emp_final_rating = lemp.lov_value
JOIN (SELECT lov_text FROM lov_details) llm ON p.role_comp_lm_final_rating = llm.lov_value
WHERE p.employee_number = 34570

checking values from two tables?

I had two tables
table1 contains a list of customer and their info and there is a customer id for each one
table2 contains a list of request from all with and customer id for each request as foreign key
and i want to check the customer who has request and who is not
plz how should that be done logical is it possible to eqaul between two queries ?
thanks
You have two tables, the first with one row per customer, the other with zero to many rows per customer. So you need two techniques: one to reduce the second table to one row per customer and the other to join that result set to the first table. These techniques are an aggregating sub-query and an outer join respectively.
select c.customer_id
, nvl(r.req_count, 0) as no_of_reqs
from customer c
left outer join ( select customer_id
, count(*) as req_count
from customer_req
group by customer_id ) r
on c.customer_id = r.customer_id

How to return values from query when joining multiple tables in Linq?

I have a Linq queries that have tables join and couple of tables inner join together. Sometimes I got an error from the query when table is empty. What I trying to do is I am tryting to get a value from table even if other table is empty.
Thanks in Advance.
You need to do left join
Assuming left join between customer and order table.
var query =
from customer in dc.Customers
from order
in dc.Orders
.Where(o => customer.CustomerId == o.CustomerId)
.DefaultIfEmpty()
select new { Customer = customer, Order = order }
Also refer below link
http://forums.asp.net/t/1792428.aspx/1

Resources