Retrieving query data from four tables using SQL Server 2008? - c#

I have four tables:
Customer (ID, CustomerName, City)
Product (pid, pname, sprice)
Orders (OrderNum, CustomerID, EmpID, orderDate)
Sales (OrderNum, pid, qty, totalAmmount, payed, credit, CreditEndDate)
I want to retrieve data from above four tables using the following query, but I encountered a problem where I can alias the sales table?
SELECT
Cs.CustomerName, Cs.City, Crs.totalAmount, p.pname, Crs.qty,
crs.totalAmount, crs.payed, Crs.credit, ord.orderDate,
Crs.CreditEndDate
FROM
Customer Cs
INNER JOIN
Orders ord ON Cs.ID = ord.CustomerID
INNER JOIN
Product p ON p.pid = Sales Crs.pid
WHERE
ord.OrderDate BETWEEN '01/01/2014' AND '01/01/2016'
ORDER BY
[CustomerName]
Is there any help? Thanks in advance.

You need to join Sales table
SELECT Cs.CustomerName,
Cs.City,
s.totalAmount,
p.pname,
s.qty,
s.totalAmount,
s.payed,
s.credit,
ord.orderDate,
s.CreditEndDate
FROM Customer Cs
INNER JOIN Orders ord
ON Cs.ID = ord.CustomerID
INNER JOIN Sales s
ON s.OrderNum = ord.OrderNum
INNER JOIN Product p
ON p.pid = s.pid
WHERE ord.OrderDate BETWEEN '01/01/2014' AND '01/01/2016'
ORDER BY [CustomerName]

Related

How can i join 1 id fields from a table to 2 different table colums

I'm making a school menagement program in which i want to connect multiple tables to the Students table, i want to use the Cities ID field for both of the Stundents BirthPlace and their actual Address in the table but i can't figure it out how to do it.
SELECT Students.StudentID, Students.Name, Students.Birthday, Students.MothersName, Classes.ClassName, Cities.Name, Cities.Name,
PostalCode.PostalCode, Street.StreetName, Students.Number
FROM Students
INNER JOIN Classes ON Students.ClassID = Classes.ClassID
INNER JOIN Cities ON Students.BirthPlaceID = Cities.CityID
INNER JOIN Cities ON Students.CityID = Cities.CityID
INNER JOIN PostalCode ON Students.PostalCodeID = PostalCode.PostalCodeID
INNER JOIN Utca ON Students.StreetID = Streets.StreetID
i've tried this way but it only results in error.
You create an alias for the table in your select and then you can access the same table with 2 different identifies. I did the same (created alias) for the values in your select statement to identify which city is which.
SELECT Students.StudentID, Students.Name, Students.Birthday, Students.MothersName, Classes.ClassName, StudentBirthCities.Name as StudentBirthCityName, StudentCity.Name as StudentCityName,
PostalCode.PostalCode, Street.StreetName, Students.Number
FROM Students
INNER JOIN Classes ON Students.ClassID = Classes.ClassID
INNER JOIN Cities StudentBirthCities ON Students.BirthPlaceID = StudentBirthCities.CityID
INNER JOIN Cities StudentCity ON Students.CityID = StudentCity.CityID
INNER JOIN PostalCode ON Students.PostalCodeID = PostalCode.PostalCodeID
INNER JOIN Utca ON Students.StreetID = Streets.StreetID

SQL: Count rows which are not present in 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

Why this error occurs: Subquery returned more than 1 value

My Sql Query
SELECT BOOKING_TIME,
Contact_No,
(FName+LName)AS NAME ,
E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
INNER JOIN BOOKINGS ON ZM.Zone_ID = BOOKINGS.Zone_ID)AS ZONE_NAME,
City,
Addr_1,
Addr_2,
PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
INNER JOIN BOOKINGS ON PROJECTS.PROJECT_ID=BOOKINGS.PROJECT_ID)AS PROJECT_NAME
FROM BOOKINGS
You're getting that error because of your subqueries:
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
INNER JOIN BOOKINGS ON ZM.Zone_ID = BOOKINGS.Zone_ID) AS ZONE_NAME
And:
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
INNER JOIN BOOKINGS ON PROJECTS.PROJECT_ID = BOOKINGS.PROJECT_ID) AS PROJECT_NAME
You're getting multiple records back and trying to store them in a single field.
Here is your query:
SELECT BOOKING_TIME, Contact_No,(FName+LName)AS NAME, E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM INNER JOIN
BOOKINGS
ON ZM.Zone_ID = BOOKINGS.Zone_ID
) AS ZONE_NAME,
City, Addr_1, Addr_2, PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS INNER JOIN
BOOKINGS
ON PROJECTS.PROJECT_ID=BOOKINGS.PROJECT_ID
) AS PROJECT_NAME
FROM BOOKINGS;
Either subquery could be returning more than one row. In a subselect in the select clause, you can only return one value. I think there is an easy fix. You probably want correlated subuqeries, so just remove the BOOKINGS table from each subquery:
SELECT BOOKING_TIME, Contact_No, (FName+LName)AS NAME, E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
WHERE ZM.Zone_ID = BOOKINGS.Zone_ID
) AS ZONE_NAME,
City,Addr_1,Addr_2,PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
WHERE PROJECTS.PROJECT_ID = BOOKINGS.PROJECT_ID
)AS PROJECT_NAME
FROM BOOKINGS;
These are now "correlated subqueries". In this case, they should each return at most one row.
Another way to express this query is using join syntax:
SELECT BOOKING_TIME, Contact_No, (FName+LName)AS NAME, E_MAIL,
ZM.ZONE_NAME,
City, Addr_1, Addr_2, PIN,
p.PROJECT_NAME
FROM BOOKINGS b LEFT OUTER JOIN
Zone_Master zm
on ZM.Zone_ID = BOOKINGS.Zone_ID LEFT OUTER JOIN
PROJECTS p
on p.PROJECT_ID = b.PROJECT_ID

Retrieving Data From Three Table

cmd =new SqlCommand("Select i.InvoiceNo,i.TotalAmount,
i.PaymentStatus,m.MovieID,m.MovieName, s.CompanyName
From InvoiceDetails i INNER JOIN Movie m ON
i.InvoiceNo=m.InvoiceNo and Supplier s
Inner Join Movie m ON s.SupplierID=m.SupplierID
Where SupplierID=#supplierID AND
CompanyName=#companyName AND PaymentStatus=#ddlPaymentStatus",
conPayment);
This is the wrong Select Statement and How can I modify it in order successful to get the data from three table.
The one thing that jumps out at me is one JOIN done incorrectly:
You have:
...
from InvoiceDetails i
inner join Movie m on i.InvoiceNo = m.InvoiceNo
and Supplier s
inner join Movie m on s.SupplierID = m.SupplierID
...
It should be:
select i.InvoiceNo,i.TotalAmount,i.PaymentStatus,m.MovieID,m.MovieName,s.CompanyName
from InvoiceDetails i
inner join Movie m on i.InvoiceNo = m.InvoiceNo
inner join Supplier s on s.SupplierID = m.SupplierID
where s.SupplierID = #supplierID
and s.CompanyName = #companyName
and i.PaymentStatus = #ddlPaymentStatus
You already joined Movie once, you have to then JOIN on supplier also. The way you are doing it will surely give you a syntax error. Once for having two tables with the same alias, and then it would also complaint about not knowing supplier s, since it would be expecting a column from the tables joined before that.

Sql query involving two tables

I have two tables, tbl_msg...
tbl_user
I want to select all the msgs from tbl_msg where toid=42 along with respective names of the person(instead of fromid) from whom msg has been sent. Result should look something like this..
|fromid(name, not the id)| Msg| toid(name!,which belongs to id 42)|some other column from msgid|
Query:
select tbl_msg.[MsgId]
,tbl_User.FirstName as sentby
,tbl_msg.[ToId]
,tbl_msg.[Msg]
from
tbl_msg
inner join
tbl_User on tbl_msg.FromId = tbl_User.ID
where
tbl_msg.ToId = 42
but this will only give me name of corresponding fromid and not names for both toid and fromid
How can this be done?
You have to join user two times:
select m.[MsgId]
,u1.FirstName as sentby
,u2.FirstName as sentTo
,m.[Msg]
from tbl_msg m
inner join tbl_User u1 on m.FromId = u1.ID
inner join tbl_User u2 on m.ToId = u2.ID
where m.ToId = 42
Try to change ON tbl_msg.FromId=tbl_User.ID to on tbl_msg.ToId=tbl_User.ID instead:
select tbl_msg.[MsgId]
,tbl_User.FirstName as sentby
,tbl_msg.[ToId]
,tbl_msg.[Msg] from tbl_msg inner join tbl_User
on tbl_msg.ToId=tbl_User.ID
where tbl_msg.ToId=42
Hope this query will helps you.
select tbl_msg.[MsgId]
,u1.FirstName as sentby
,u2.FirstName as FromSent
,tbl_msg.[ToId]
,tbl_msg.[Msg] from tbl_msg
inner join tbl_User u1 on tbl_msg.FromId=u1.ID
inner join tbl_User u2 on u1.ToID = u2.ID
where tbl_msg.ToId=42

Categories

Resources