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.
Related
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
I have two tables, Customers And ListTypeData. I just want to join these two tables.
I have CityId And DistrictId In Customers table.
It gives me both City and District same how to fix
for City And District.
SELECT c.[CustomerId]
,c.[Name]
,c.[CompanyName]
,c.[ShopNo]
,list.[Description] AS 'City'
,list.[Description] AS 'District'
FROM [MakkiRuskFaisalabad].[dbo].[Customers] c
JOIN [dbo].[ListTypesData] list ON c.CityId = list.ListTypeDataId ]
You need to join to the ListTypesData table a second time:
SELECT c.CustomerId
,c.Name
,c.CompanyName
,c.ShopNo
,list.Description AS 'City'
,list2.Description AS 'District'
FROM MakkiRuskFaisalabad.dbo.Customers c
JOIN dbo.ListTypesData list ON c.CityId = list.ListTypeDataId
JOIN dbo.ListTypesData list2 ON c.DistrictId = list2.ListTypeDataId
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]
I have three tables below:
table patient
{ NID *pk
Name
Family
}
table disease
{
ICD_code *pk
Title
}
table_patient_disease *-*
{
ID
Fk_ICDcode
FK_Patient
}
it is a n-m relationship between patient and disease.
i want to write a query to select patients and their diseases
it needs to join in linq
the Sql pure query is:
SELECT
dbo.Table_Disease.*,
dbo.Table_PatDis.*,
dbo.Table_Patient.*
FROM
dbo.Table_Disease
INNER JOIN dbo.Table_PatDis ON dbo.Table_Disease.ICD_code = dbo.Table_PatDis.FK_Disease
INNER JOIN dbo.Table_Patient ON dbo.Table_PatDis.FK_PAtient = dbo.Table_Patient.NID
what is the same Linq statement ?
SELECT
dbo.Table_Disease.*,
dbo.Table_PatDis.*,
dbo.Table_Patient.*
FROM
dbo.Table_Disease
INNER JOIN dbo.Table_PatDis ON dbo.Table_Disease.ICD_code = dbo.Table_PatDis.FK_Disease
INNER JOIN dbo.Table_Patient ON dbo.Table_PatDis.FK_PAtient = dbo.Table_Patient.NID
Would become:
var results = (from d in DbContext.Table_Disease
join pd in DbContext.Table_PatDis on d.ICD_Code equals pd.FK_Disease
join p in DbContext.Table_Patient on pd.FK_PAtient equals p.NID
select new {d, pd, p});
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;