I have a query, in this query I need to get a number of classes (class count) in each district. In here it comes only those who have classes. So I need to show the district's it doesn't have a classes as well.
My District table consist 25 values. and my class coverage table has 5 values to 3 districts. I need to show all the districts.
Code
SELECT
CC.DistrictId, D.DistrictName,
COUNT(D.DistrictId) AS DistrictCount
FROM
TBL_T_ClassCoverage CC
LEFT JOIN
[dbo].[TBL_M_District] D ON CC.DistrictId = D.DistrictId
WHERE
CC.IsActive = 1
GROUP BY
CC.DistrictId, D.DistrictName
ORDER BY
DistrictId ASC
You need to join your tables the other way, from districts to classes, to ensure that all district values are in the output:
SELECT D.DistrictId, D.DistrictName, COUNT(CC.DistrictId) AS DistrictCount
FROM [dbo].[TBL_M_District] D
LEFT JOIN TBL_T_ClassCoverage CC
ON CC.DistrictId = D.DistrictId AND CC.IsActive = 1
GROUP BY D.DistrictId, D.DistrictName
ORDER BY D.DistrictId ASC
Basically you need right join not left join here.
SELECT CC.DistrictId, D.DistrictName, Count(D.DistrictId) AS DistrictCount
FROM TBL_T_ClassCoverage CC
RIGHT JOIN [dbo].[TBL_M_District] D ON CC.DistrictId = D.DistrictId
WHERE CC.IsActive = 1
GROUP BY CC.DistrictId, D.DistrictName
ORDER BY DistrictId ASC
Related
I'd like to group by state and sum up the total pledge amount, but if the pledge status is equal to canceled sum up the total paid. I'm not sure the best way to do this?
Thank you for feedback and help!
SELECT
dbo.FoundationCompanies.companyState,
CASE WHEN dbo.FondationOrder.pledgeStatus = 'canceled' THEN
(
SELECT SUM(paid) AS total
FROM dbo.FoundationInvoice
WHERE (orderId = dbo.FondationOrder.orderId)
)
ELSE
dbo.FondationOrder.pledgeAmount
END AS pledgeAmount
FROM
dbo.FoundationCompanies
INNER JOIN
dbo.FondationOrder
ON
dbo.FoundationCompanies.compId = dbo.FondationOrder.compId
GROUP BY dbo.FoundationCompanies.companyState
ORDER BY dbo.FoundationCompanies.companyState
Example data:
Algonquin 2500.00
Atlanta 500000.00
Batesville 10000.00
Batesville 25000.00
I would like the results to return as:
Amount |State
$100,000.00|AL
$145,000.00|AZ
Q : I'm not sure the best way to do this?
A : no,it'll use multi slow query ,you can use left join subquery to avoid it,like below script :
SELECT
companyState,
CASE WHEN FondationOrder.pledgeStatus = 'canceled' THEN
T.total
ELSE
FondationOrder.pledgeAmount
END AS pledgeAmount
FROM FoundationCompanies
INNER JOIN FoundationContacts ON FoundationCompanies.companyId = FoundationContacts.companyId
INNER JOIN FondationOrder ON FoundationContacts.contactId = FondationOrder.contactId
LEFT JOIN (
SELECT orderId,SUM(paid) AS total
FROM FoundationInvoice
GROUP BY orderId
) T on T.orderId = FondationOrder.orderId
GROUP BY FoundationCompanies.companyState
ORDER BY FoundationCompanies.companyState
Add Order table to your join then use the aggregate function :
SELECT
dbo.FoundationCompanies.companyState,
SUM(IIF(dbo.FondationOrder.pledgeStatus = 'canceled', dbo.FoundationInvoice.paid, dbo.FondationOrder.pledgeAmount)) AS pledgeAmount
FROM dbo.FoundationCompanies
INNER JOIN dbo.FoundationContacts ON dbo.FoundationCompanies.companyId = dbo.FoundationContacts.companyId
INNER JOIN dbo.FondationOrder ON dbo.FoundationContacts.contactId = dbo.FondationOrder.contactId
LEFT JOIN dbo.FoundationInvoice ON dbo.FondationOrder.orderId = dbo.FoundationInvoice.orderId
GROUP BY dbo.FoundationCompanies.companyState
i am new to LINQ and joins, so Please forgive me if I am asking it wrong.
I have two tables
Table1
id name date
1 Mike 20-10-15
2 John 21-10-15
3 Sam 23-10-15
Table2
id name date
1 Ashle 19-10-15
2 Lily 21-10-15
3 Jeni 22-10-15
4 April 23-10-15
I need 5 records using Joins and should be orderby Date, most recent records.
Can you guys help me, I really need to figure out how Joins works with orderby.
Thanks
EDIT:
They are two different tables so no foreign key, so I think I can't use Join, so so far what I have done is like this
var combinddata = (from t1 in db.Table1
select t1.id)
.Concat(from t2 in db.Table2
select t2.id);
I don't know how to get only 5 records how to compare records from both tables on DateTime base.
Output should be
Sam
April
Jeni
John
Lily
You can concatenate equal anonymous types from different tables. If you also select the dates, you can sort by them, in descending order, and take the first 5 records:
Table1.Select (t1 =>
new
{
Id = t1.Id,
Name = t1.Name,
Date = t1.Date
}
).Concat(
Table2.Select (t2 =>
new
{
Id = t2.Id,
Name = t2.Name,
Date = t2.Date
}
))
.OrderByDescending (x => x.Date).Take(5)
Note that this gives precedence to items in Table1. If item 5 and 6 in the concatenated result are on the same date, but from Table1 and Table2, respectively, you only get the item from Table1.
If you want, you can select only the names from this result, but I assume that your output only shows the intended order of record, not the exact expected result.
var query =
from Table1 in table1
join Table2 in table2 on table1.id equals table2.id
orderby table1.date ascending
select table1.date;
Try this way
var combinddata = (from t1 in db.Table1
select t1.Name)
.Concat(from t2 in db.Table2
select t2.Name).OrderByDescending(x => x.date).Take(5);
Please i have my two tables, i recently learnt to outer join the two in linq. My problem now is i want to use group by a particular column for a i want to return a sum as well. Below is how i join the tables.
var result = from l1 in repository.Locations
join l2 in repository.Rooms on l1.LocationName equals l2.Location
into l3
from tempLocations in l3.DefaultIfEmpty()
select new
{
l1.LocationName,
total = (tempLocations == null ? String.Empty : tempLocations.Location)
};
My problem is the column total, intend to group by l1.LocationName and the count() as total. Please any help would be appreciated.
UPDATE
I want to group by the Location Column and LocationName column from the l1 and l2 tables. I want to return something like.
The LocationNAme is from Locations Table , LOcation Column is from Rooms Table , i want to outer join the two tables where LocationNAme = Location and count existence of LocationName in Room Table .
I need to have two tables.
First table (tblStudent) has fields student_id,firstname,lastname,class,rollno.
Second table(feeType) has fields class,admissionFee, tuitionFee etc.
I want to show the different fee for each class.
Like class 5 pays 600 as tuition fee where as class 6 will pay 700.
Eg. If class is 5 in first table then only the class 5 row in feeType should be shown ? How do I design tables as such? How do I join two tables? I will be using sql server 2008 and c# as frontend. Thanks from a novice.
This is the simplest type of INNER JOIN:
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class
Your INNER JOIN would only return matching records,for non-matching records you could use a FULL JOIN and WHERE criteria to check.
SELECT *
FROM tblStudent AS s
INNER JOIN feeType as f ON s.class = f.class where s.class=null;
Visit here
This will list all the class names and their fees.
SELECT
s.class,
f.admissionFee as admission_fee,
f.tuitionFee as tuition_fee
FROM
tblStudent as s
JOIN feeType as f ON s.class = f.class
GROUP BY
s.class;
I hope this will help you to get the exact result set.
I am trying to query the 3rd level table ef_staff table 3 times to get 3 diff staff objects for each row. How to translate this in LINQ?
SELECT a.a_appraisalid, a.a_year, c.s_staffName, c2.s_staffName, c3.s_staffName
FROM ef_appraisal a, idp_application b, ef_staff c, ef_staff c2, ef_staff c3
WHERE a.a_appraisalid = b.a_appraisalid AND
a.a_staffid = c.s_staffid AND
a.a_appraisedby = c2.s_staffid AND
a.a_reviewedby = c3.s_staffid
I have been trying many ways but there is still an error 'Type Inference Failed' in the 2nd & 3rd joining of Staff. What am I missing here?
from application in applications
join appraisal in pmsEntities.ef_appraisal on application.a_appraisalid equals appraisal.a_appraisalid
join staff in pmsEntities.ef_staff on appraisal.a_staffid equals staff.s_staffid
join appraiser in pmsEntities.ef_staff on staff.s_appraisedby equals appraiser.s_staffid into ap
from appraiser in ap.DefaultIfEmpty()
join reviewer in pmsEntities.ef_staff on staff.s_reviewedby equals reviewer.s_staffid into rv
from reviewer in rv.DefaultIfEmpty()
join company in pmsEntities.ef_company on appraisal.a_companyid equals company.c_companyid into jc
from company in jc.DefaultIfEmpty()
select appraisal, staff.staffName, appraiser.staffName, reviewer.staffName, company.compName
I fixed the mistake. The first level joining of staff object should be linked to the 2nd level of staff object as below:
join staff in pmsEntities.ef_staff on appraisal.a_staffid equals staff.s_staffid into staffj
from staff1 in staffj
join appraiser in pmsEntities.ef_staff on staff1.s_appraisedby equals appraiser.s_icno into staff2
`
Hope it helps