i want to join two tables to get the Doctors name instead of the ID
SELECT Doctors.doct_name,
Shifts.shift_date,
Shifts.shift_day,
Shifts.shift_start,
Shifts.shift_end
FROM SHIFTS
JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
the Column are shown but without any Results ,, no data from the database ?
your query looks ok.
try two seperate queries to validate the basic data:
select * from Doctors order by doct_id;
and
select * from Shifts order by doct_id;
see if you can manually spot the data issue:
Try a LEFT JOIN:
SELECT Doctors.doct_name,Shifts.shift_date,Shifts.shift_day,Shifts.shift_start,Shifts.shift_end
FROM SHIFTS
LEFT JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
Try using left join and also try using Where clause with an id to check if there is really Doctors exist in both tables
SELECT Doctors.doct_name,Shifts.shift_date,Shifts.shift_day,Shifts.shift_start,Shifts.shift_end
FROM SHIFTS
LEFT JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
OR
SELECT Doctors.doct_name,Shifts.shift_date,Shifts.shift_day,Shifts.shift_start,Shifts.shift_end
FROM SHIFTS
LEFT JOIN Doctors ON Doctors.doct_id = Shifts.doct_id
WHERE Doctors.doct_id = [insert doctors id here - doctors_id]
Related
I am trying to get all data from the join table to data review, but what I learn is just to specify the attributes one by one in the SELECT statement as following code:
var query = (from inv in db.tblinventories
join sup in db.tblsuppliers on inv.SupplierID equals sup.SupID into left
from sup in left.DefaultIfEmpty()
select new { inv.Invnum,inv.Model, sup.name }).ToList();
this.dgvPOS.DataSource = query;```
Is this the only way to fetch all data? Because in ORACLE SQL the way is quite simple by using "*" to represent all attributes like this:
SELECT *
FROM tblinventories
LEFT JOIN tblsuppliers
ON tblinventories.SupplierID = tblsuppliers.SupID;
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
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 got two tables called: EmployeeTable & TaskAssignmentTable.
They look like this :
TaskAssignmentTable shows tasks assigned to employees. In order to assign new tasks to employees i want to have count of tasks assigned to different people and then assign task to people who have least tasks assigned.
Problem: using normal count() on TaskAssignmentTable results in this table:
But what i want is some sort of join between tables which shows count of rows which are present in first table and absent in 2nd table with count equal to 0 like this one:
So what would be the SQL query to join tables and do such thing? (Optional: Since I'm using C# Linq-2-SQL i would be grateful if someone can write LINQ syntax for this).
You need a LEFT OUTER JOIN based upon your statement that you want rows that are present in the first table but not the second:
SELECT EmployeeID, Name, Count(TaskID) as CNT
FROM EmployeeTable e
LEFT JOIN TaskAssignmentTable t
ON e.employeeID = t.FKEmployeeID
GROUP BY EmployeeID, Name
Try
SELECT EmployeeID, Name, Count(TaskID) as CNT
FROM EmployeeTable emp
LEFT JOIN TaskAssignmentTable task on emp.employeeID = task.FKEmployeeID
GROUP BY EmployeeID, Name
For that you have to use Left Outer Join.
SELECT EmployeeID, Name, Count(TaskID) as CNT
FROM EmployeeTable emp
LEFT OUTER JOIN TaskAssignmentTable task on emp.employeeID = task.FKEmployeeID
GROUP BY EmployeeID, Name
And LINQ Version of this query look like this
var employees = from emp in dbContext.Employees
join task in dbContext.TaskAssignmentTable
on emp.employeeID equals task.FKEmployeeID
into tEmpWithTask
from tEmp in tEmpWithTask.DefaultIfEmpty()
group tEmp by new { emp.EmployeeID, emp.Name } into grp
select new {
grp.Key.EmployeeID,
grp.Key.Name,
grp.Count(t=>t.TaskID != null)
};
You need to OUTER JOIN the two table (in your case a LEFT JOIN):
SELECT EmployeeID, Name, Count(TaskID) as CNT
FROM EmployeeTable emp
LEFT JOIN TaskAssignmentTable task on emp.employeeID = task.FKEmployeeID
GROUP BY EmployeeID, Name
My recent project I have a requirements to print receivable summary. I need to return entire rows from the OpeningBalance table and matching rows from VoucherHeader and Customers.
My SQL query is this
SELECT
OpeningBalance.OpenID, Sum(OpeningBalance.Amount) AS SumOfAmount,
Sum(VoucherHeader.Debit) AS SumOfDebit, Sum(VoucherHeader.Credit) AS SumOfCredit,
Customers.CustomerID, Customers.CustomerName
FROM
(OpeningBalance
LEFT OUTER JOIN
VoucherHeader ON OpeningBalance.OpenID = VoucherHeader.LedgerID)
INNER JOIN
Customers ON OpeningBalance.OpenID = Customers.CustomerID
WHERE
(((Customers.CustomerType)='Debtor')
AND ((VoucherHeader.VoucherDate)<#2013/06/02#))
GROUP BY
OpeningBalance.OpenID, Customers.CustomerID, Customers.CustomerName,
VoucherHeader.LedgerID
ORDER BY
Customers.CustomerName;
Please help.
SELECT OB.OpenID, Sum(OB.Amount) AS SumOfAmount,
Sum(VB.Debit) AS SumOfDebit, Sum(VB.Credit) AS SumOfCredit, CS.CustomerID,
CS.CustomerName FROM OpeningBalance OB
LEFT OUTER JOIN VoucherHeader VB ON OB.OpenID = VB.LedgerID
LEFT OUTER JOIN Customers CS ON OB.OpenID = CS.CustomerID
WHERE (((CS.CustomerType)='Debtor') AND ((VB.VoucherDate)<#2013/06/02#))
GROUP BY OB.OpenID, CS.CustomerID, CS.CustomerName, VB.LedgerID
ORDER BY CS.CustomerName;