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.
Related
I have data in two tables and I need in one query get all data and join getting data.
SELECT
kpip.PersonalName,
kpiT.Name,
kpiPR.KpiTarget,
kpiPR.KpiResultDate,
kpiPR.KpiResult
FROM KpiPersonalResult AS kpiPR join KpiPersonal as kpip
on kpiPR.KpiPersonal = kpip.Id join KpiType AS kpiT
on kpip.KpiType = kpiT.Id join MerchantAdministrators as merA
on kpiPR.KpiAdded = merA.Id and kpiPR.KpiResultDate between '2021-04-07' and '2021-04-08'
select
kpiP.PersonalName,
kpiT.Name,
kpiP.KpiTarget
from KpiPersonal as kpiP join KpiType as kpiT
on kpiP.KpiType = kpiT.Id
Based on the fast that the second query has 3 columns of the same name as the first query, I guess you mean to union them:
SELECT
kpip.PersonalName,
kpiT.Name,
kpiPR.KpiTarget,
kpiPR.KpiResultDate,
kpiPR.KpiResult
FROM
KpiPersonalResult AS kpiPR
join KpiPersonal as kpip on kpiPR.KpiPersonal = kpip.Id
join KpiType AS kpiT on kpip.KpiType = kpiT.Id
join MerchantAdministrators as merA on kpiPR.KpiAdded = merA.Id and kpiPR.KpiResultDate between '2021-04-07' and '2021-04-08'
UNION ALL
select
kpiP.PersonalName,
kpiT.Name,
kpiP.KpiTarget,
null, --put suitable default values for the other columns here
null
from
KpiPersonal as kpiP
join KpiType as kpiT on kpiP.KpiType = kpiT.Id
Unioned queries need the same number of columns. I've inserted NULL as default value for the two missing columns in the second query (relative to the first)
UNION makes a resultset grow taller. If you intended for it to grow wider, that is done via JOIN. A simple pattern for doing so is:
WITH query1 AS(
--query 1 here
), query2 AS (
--query2 here
)
SELECT * FROM query1 JOIN query2 ON ...
Side note on formatting and indenting - most people find SQL most readable when all operations that are related are at the same indent level e.g in a typical query, the SELECT FROM WHERE GROUP ORDER keywords are all at the same indent level, with the blocks that relate to them (the list of selected columns, or list of joined tables, list of where'd predicates etc) indented a level again. We also typically don't use as when aliasing tables but we do use it when aliasing columns in the SELECT
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
I am trying to get all the products from the Products table, and at the same time retrieve Company_Name from Company table. A common column in both my table is the Company_Id.
I am using this query:
SELECT
products.product_id,
products.product_name,
products.product_desc,
products.unit_price,
products.stock_level,
products.product_image,
products.gender,
products.type_of_acct,
products.product_cname,
products.product_cdesc,
products.company_id,
company.company_name
FROM
products
INNER JOIN
company ON products.company_id = company.company_id
However this only show all the products from a specific company.
I need to show all the products.
It seems you have an optional relationship here, so use LEFT JOIN:
....
FROM Products
LEFT JOIN Company
ON Products.Company_Id = Company.Company_Id
This retrieves all the products whether linked to a valid company or not.
I think you also need to go over your data and check if you have your foreign keys set up right and have the correct data.
You can use LEFT JOIN to get all details from Products table. LEFT JOIN is fetch all records from left table and also fetch matching records from right table.
SELECT Products.Product_ID, Products.Product_Name, Products.Product_Desc, Products.Unit_Price, Products.Stock_Level, Products.Product_Image, Products.Gender, Products.Type_Of_Acct, Products.Product_CName, Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM Products
LEFT JOIN Company ON Products.Company_Id = Company.Company_Id
Left join is best solution for you.
Or you can make one user define function and from there you can retrieve company name like below
SELECT
Products.Product_ID, Products.Product_Name,
Products.Product_Desc, Products.Unit_Price,
Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct,
Products.Product_CName, Products.Product_CDesc,
Products.Company_Id,
dbo.GetCompanyNameFromCompanyID(Products.Company_Id) AS Company_Name
FROM
Products
Try this
SELECT
Products.Product_ID, Products.Product_Name, Products.Product_Desc,
Products.Unit_Price, Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct, Products.Product_CName,
Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM
Products
LEFT JOIN
Company ON Products.Company_Id = Company.Company_Id
This will return you all the products, with its linked company if any, a NULL will be shown under Company.Company_Name otherwise
How can i merge the rows of a teachers qualification from its table to a single row and join it to another so i can search for a given condition to know if a teacher possesses the given degree/certification. Or is there a better approach other than the one i am trying to use? Thanks in advance. Second image.
SELECT
StaffTable.TeacherType,
StaffTable.StaffID,
TeacherQualificationsTable.YearOfGraduation,
TeacherQualificationsTable.SubjectMajorsCombination,
STUFF((SELECT
', ' + [DegreeObtained]
FROM
TeacherQualificationsTable FOR XML PATH('')),1,1,'') AS [DegreeObtained]
FROM
StaffTable
INNER JOIN
TeacherQualificationsTable
ON
StaffTable.StaffID = TeacherQualificationsTable.StaffID
INNER JOIN
AppointmentChronologyTable ON StaffTable.StaffID = AppointmentChronologyTable.StaffID
WHERE
(StaffTable.TeacherType = #TeacherType)
AND (StaffTable.StaffStatus = #StaffStatus)
AND DegreeObtained NOT CONTAINS ('B.Ed (Bachelor of Education)' , 'M.Ed (Master of Education)', 'G.D.E (Graduate Diploma in Education)')
ORDER BY
StaffTable.LastName,
StaffTable.FirstName,
StaffTable.MiddleName
I have a staffTable and a staffQualificationsTable (which will have multiple qualification entries for any given staff). Am required to check if some staff have some given qualifications eg 'B.Ed (Bachelor of Education)' , 'M.Ed (Master of Education)', 'G.D.E (Graduate Diploma in Education)' and return all the staff without it so they will be called to tender theirs or laid off. Please, i hope this is clearer.
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 .