MS Access query problem - sql

SELECT
tbl_vehicle_models.model_name AS Vehicle_Model,
tbl_vehicle_models.manufacturer AS Manufacturer,
tbl_jobs.vehicle_registration_number AS Registration_Number,
tbl_customers.first_name + " " + tbl_customers.last_name AS Customer_Name,
tbl_customers.address AS Address,
tbl_customers.contact_no AS Contact_Number,
tbl_jobs.cost_charged AS Cost,
tbl_jobs.was_accident AS Was_Accident,
tbl_jobs.was_towed AS Was_Towed,
tbl_jobs.job_call_time AS Call_Time,
tbl_jobs.job_arrival_time AS Arrival_Time,
tbl_jobs.job_leaving_scene_time AS Leaving_Time,
tbl_places.place_name AS Place
FROM
tbl_jobs
INNER JOIN tbl_vehicle_models
ON ( tbl_vehicle_models.ID = tbl_jobs.vehicle_model )
INNER JOIN tbl_customers
ON ( tbl_customers.ID = tbl_jobs.customer_id )
INNER JOIN tbl_places
ON ( tbl_places.ID = tbl_jobs.job_place )
What's wrong with this query? I am getting error saying missing operator in query expression '( v.ID = j.vehicle_model )
INNER JOIN tbl_customers c ON ( c.id = j.customer_id )
INNER JOIN tbl_places p ON ( p.ID = j.job_place'
Edit: This solved my problem:
SELECT tbl_vehicle_models.model_name, tbl_vehicle_models.manufacturer, tbl_jobs.vehicle_registration_number, tbl_customers.first_name & " " & tbl_customers.last_name AS Expr1, tbl_customers.address, tbl_customers.contact_no, tbl_jobs.cost_charged, tbl_jobs.was_accident, tbl_jobs.was_towed, tbl_jobs.job_call_time, tbl_jobs.job_arrival_time, tbl_jobs.job_leaving_scene_time, tbl_places.place_name
FROM ((tbl_jobs INNER JOIN tbl_vehicle_models ON tbl_jobs.vehicle_model = tbl_vehicle_models.ID) INNER JOIN tbl_customers ON tbl_jobs.customer_id = tbl_customers.ID) INNER JOIN tbl_places ON tbl_jobs.job_place = tbl_places.ID;

Are you sure that v.ID and j.vehicle_model have the same data type?
Looks like v.ID is an integer and j.vehicle_model a string
I think this problem happens when you try to use operators with incompatible operand data types.
EDIT:
Then, try changing
c.first_name + " " + c.last_name
to
c.first_name & " " & c.last_name
I think that's a problem with this concatenation

This solved my problem:
SELECT tbl_vehicle_models.model_name, tbl_vehicle_models.manufacturer,
tbl_jobs.vehicle_registration_number, tbl_customers.first_name & " " &
tbl_customers.last_name AS Expr1, tbl_customers.address, tbl_customers.contact_no,
tbl_jobs.cost_charged, tbl_jobs.was_accident, tbl_jobs.was_towed, tbl_jobs.job_call_time,
tbl_jobs.job_arrival_time, tbl_jobs.job_leaving_scene_time, tbl_places.place_name
FROM ((tbl_jobs
INNER JOIN tbl_vehicle_models ON tbl_jobs.vehicle_model = tbl_vehicle_models.ID)
INNER JOIN tbl_customers ON tbl_jobs.customer_id = tbl_customers.ID)
INNER JOIN tbl_places ON tbl_jobs.job_place = tbl_places.ID;

Related

Too many results in query

I'm fetching some data from our database in MSSQL. Out of this data I want to determine who created the client entry and who took the first payment from this client.
There can be many payment entries for a client on a single booking/enquiry and at the moment, my query shows results for each payment. How can I limit the output to only show the first payment entry?
My query:
SELECT
c.FirstName,
c.LastName,
c.PostalCode,
o.OriginOfEnquiry,
s.SuperOriginName,
c.DateOfCreation,
DATEDIFF(day, c.DateOfCreation, p.DateOfCreation) AS DaysToPayment,
pc.PackageName,
CONCAT(u.FirstName, ' ', u.LastName) AS CreateUser,
(SELECT CONCAT(u.FirstName, ' ', u.LastName)
WHERE u.UserID = p.UserID ) AS PaymentUser
FROM tblBookings b
INNER JOIN tblPayments p
ON b.BookingID = p.BookingID
INNER JOIN tblEnquiries e
ON e.EnquiryID = b.EnquiryID
INNER JOIN tblCustomers c
ON c.CustomerID = e.CustomerID
INNER JOIN tblOrigins o
ON o.OriginID = e.OriginID
INNER JOIN tblSuperOrigins s
ON s.SuperOriginID = o.SuperOriginID
INNER JOIN tblBookingPackages bp
ON bp.bookingID = p.BookingID
INNER JOIN tblPackages pc
ON pc.PackageID = bp.packageID
INNER JOIN tblUsers u
ON u.UserID = c.UserID
WHERE c.DateOfCreation >= '2016-06-01' AND c.DateOfCreation < '2016-06-30'
AND p.PaymentStatusID IN (1,2)
AND e.CustomerID = c.CustomerID
AND p.DeleteMark != 1
AND c.DeleteMark != 1
AND b.DeleteMark != 1
;
I tried adding a "TOP 1" to the nested select statement for PaymentUser, but it made no difference.
you can use cross apply with top 1:
FROM tblBookings b
cross apply
(select top 1 * from tblPayments p where b.BookingID = p.BookingID) as p
Instead of table tblPayments specify sub-query like this:
(SELECT TOP 1 BookingID, UserID, DateOfCreation
FROM tblPayments
WHERE DeleteMark != 1
AND PaymentStatusID IN (1,2)
ORDER BY DateOfCreation) as p
I'm assuming that tblPayments has a primary key column ID. If it is true, you can use this statment:
FROM tblBookings b
INNER JOIN tblPayments p ON p.ID = (
SELECT TOP 1 ID
FROM tblPayments
WHERE BookingID = b.BookingID
AND DeleteMark != 1
AND PaymentStatusID IN (1,2)
ORDER BY DateOfCreation)

Inner Join In SqlQuery Giving Error

I am trying to perform this query :
List<string> info = db.Database.SqlQuery<string>("
SELECT u.Name, a.LName
FROM dbo.Table1 u
INNER JOIN dbo.Table2 a ON a.Table1id = u.id
WHERE u.Name = '" + name + "'").ToList();
When I try and do that query I get this error:
The data reader has more than one field. Multiple fields are not valid
for EDM primitive or enumeration types.
When I just tried to select the Name without the inner join it worked fine so how do I fix that so that I can perform my inner join without errors?
Thanks!
What #xdd said worked well
List<string> info = db.Database.SqlQuery<string>("
SELECT u.Name + a.LName
FROM dbo.Table1 u
INNER JOIN dbo.Table2 a ON a.Table1id = u.id
WHERE u.Name = '" + name + "'").ToList();
Fixed that

sqlite return subquery multi column

I have a sqlite subquery with the following query which is calculating the hours and labour_rate. The only issue I now have is can I get the two columns from my subquery to output in the main query. I've tried to layout the query according to some of the web tutorials but need that little bit of help to get me over the finish line as I keep getting a syntax error
SELECT c.customerID, c.customer, sum( ifnull(il.line_price, 0 ) )/10000 AS net,
FROM customer AS c
LEFT JOIN invoice AS i
ON c.customerID = i.customerID
LEFT JOIN invoice_line AS il
ON i.invoiceID = il.invoiceID
(SELECT sum(( ifnull(tl.mon,0) + ifnull(tl.tues,0) + ifnull(tl.wed,0) + ifnull(tl.thurs,0) + ifnull(tl.fri,0) + ifnull(tl.sat,0) + ifnull(tl.sun,0) ) * s.cost_rate)/10000 AS labour_rate,
sum(( ifnull(tl.mon,0) + ifnull(tl.tues,0) + ifnull(tl.wed,0) + ifnull(tl.thurs,0) + ifnull(tl.fri,0) + ifnull(tl.sat,0) + ifnull(tl.sun,0) ))/10000 AS
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t
ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s
ON t.staffID = s.staffID
WHERE (c.customerID = tl.customerID) AND (t.date BETWEEN '2014-03-01' AND '2015-12-01')
GROUP BY tl.customerID) AS time1
WHERE (i.date BETWEEN '2014-03-01' AND '2015-12-01') AND (time1.customerID = tl.customerID)
GROUP BY c.customerID
ORDER BY c.customer ASC
There are a few syntax errors in here. The first (and possibly most important) is you have to JOIN your subquery to the rest of the result set. So after
LEFT JOIN invoice_line AS il
ON i.invoiceID = il.invoiceID
you will need to add another JOIN statement:
LEFT JOIN
(SELECT
SUM(...) AS labour_rate,
SUM(...) AS hours,
tl.customerID
FROM
...) AS time1
ON <your join condition>
You will have to select some sort of field in your sub query that you can join back to the the invoice on i.e. tl.customerID.
Also, in your subquery you cannot reference a field that is outside of it, so where you have WHERE (c.customerID = tl.customerID) in your subquery it will fail because you are trying to reference c.<fieldname>. It needs to be moved to the ON part of the JOIN clause. Once you get your JOIN working correctly then you can just change your outer-most SELECT to something like
SELECT c.customerID, c.customer, sum(ifnull(il.line_price,0))/10000 AS net, time1.labour_rate, time1.hours
Here's an example of how I would do it:
SELECT c.customerID, c.customer, sum( ifnull(il.line_price, 0 ) )/10000 AS net, time1.labour_rate, time1.[hours]
FROM customer AS c
LEFT JOIN invoice AS i
ON c.customerID = i.customerID
LEFT JOIN invoice_line AS il
ON i.invoiceID = il.invoiceID
LEFT JOIN
(SELECT
sum(( ifnull(tl.mon,0) + ifnull(tl.tues,0) + ifnull(tl.wed,0) + ifnull(tl.thurs,0) + ifnull(tl.fri,0) + ifnull(tl.sat,0) + ifnull(tl.sun,0) ) * s.cost_rate)/10000 AS labour_rate,
sum(( ifnull(tl.mon,0) + ifnull(tl.tues,0) + ifnull(tl.wed,0) + ifnull(tl.thurs,0) + ifnull(tl.fri,0) + ifnull(tl.sat,0) + ifnull(tl.sun,0) ))/10000 AS [hours],
tl.customerID
FROM timesheet_line AS tl
LEFT JOIN timesheet AS t
ON tl.timesheetID = t.timesheetID
LEFT JOIN staff AS s
ON t.staffID = s.staffID
WHERE (t.date BETWEEN '2014-03-01' AND '2015-12-01')
GROUP BY tl.customerID) AS time1
ON c.customerID = time1.customerID
WHERE (i.date BETWEEN '2014-03-01' AND '2015-12-01')
GROUP BY c.customerID
ORDER BY c.customer ASC

How to Get All Rows matched + Unmatched and Unmatched Rows Column Will be null

I am working on an university management system. This is my database diagram
Database Diagram
I am creating a search form using this query:
SELECT Distinct
TblStudentBioData.RegNo,
TblStudentBioData.First_NameUr + SPACE(1)+ TblStudentBioData.Middle_NameUr + SPACE(1) + TblStudentBioData.Last_NameUr AS Name,
TblStudentBioData.Father_NameUr,
Ay.AcademicYearName,
Smst.SemName,
TBLCOLLEGE.CollegeName,
CID.ClassName,
TblImages.Images,
TblStudentBioData.Student_ID,
TblImages.ImageId,
Ay.AcademicYearId,
Smst.SemesterId,
TblClassSchedule.ClassSchId
FROM
TblStudentBioData
LEFT JOIN
TblStudentDetail ON (TblStudentBioData.Student_ID = TblStudentDetail.Student_ID)
OR (TblStudentBioData.Student_ID != TblStudentDetail.Student_ID)
INNER JOIN
TBLCFGSEX AS sex ON TblStudentBioData.CfgSexId = sex.CfgSexId
INNER JOIN
TBLMARITALSTATUS ON TblStudentBioData.MaritalStatusId = TBLMARITALSTATUS.MaritalStatusId
INNER JOIN
TblStudentSubAss ON TblStudentDetail.StudentDetailID = TblStudentSubAss.StudentDetailID
INNER JOIN
TblSubAss ON TblSubAss.SubAssId = TblStudentSubAss.SubAssId
INNER JOIN
TblClassSchedule ON TblStudentDetail.ClassSchId = TblClassSchedule.ClassSchID
INNER JOIN
TableClass AS CID ON TblClassSchedule.ClassID = CID.ClassID
INNER JOIN
TblImages ON TblStudentBioData.ImageId = TblImages.ImageId
LEFT JOIN
TBLCOLLEGE ON CID.CollegeId = TBLCOLLEGE.CollegeID
INNER JOIN
TBLBLOODGROUP BG On TblStudentBioData.BloodID = BG.BloodId
INNER JOIN
tableSemAssigning SA On TblClassSchedule.SemAssId = Sa.SemAssId
INNER JOIN
TblAcademicYear AY On SA.AcademicYearId = AY.AcademicYearId
INNER JOIN
TableSemester Smst On Smst.SemesterId = Sa.SemesterId
and this query gives me the output like this
In the result the 5th row is not matching row.
My question: how to show null value in non matching row's columns which I mention in the image?
As far as I can tell it would be like this. Anything that relates directly to TblStudentBioData MIGHT be an an INNER JOIN (I cannot tell) but everything that relates to TblStudentDetail should be a left join.
Also be careful with any where clause that you don't override these left joins.
SELECT DISTINCT /* I hate distinct, there is probably a better way */
TblStudentBioData.RegNo
, TblStudentBioData.First_NameUr + SPACE(1) + TblStudentBioData.Middle_NameUr + SPACE(1) + TblStudentBioData.Last_NameUr AS Name
, TblStudentBioData.Father_NameUr
, Ay.AcademicYearName
, Smst.SemName
, TBLCOLLEGE.CollegeName
, CID.ClassName
, TblImages.Images
, TblStudentBioData.Student_ID
, TblImages.ImageId
, Ay.AcademicYearId
, Smst.SemesterId
, TblClassSchedule.ClassSchId
FROM TblStudentBioData
INNER JOIN TBLCFGSEX AS sex
ON TblStudentBioData.CfgSexId = sex.CfgSexId
INNER JOIN TBLMARITALSTATUS
ON TblStudentBioData.MaritalStatusId = TBLMARITALSTATUS.MaritalStatusId
INNER JOIN TblImages
ON TblStudentBioData.ImageId = TblImages.ImageId
INNER JOIN TBLBLOODGROUP BG
ON TblStudentBioData.BloodID = BG.BloodId
LEFT JOIN TblStudentDetail
ON (TblStudentBioData.Student_ID = TblStudentDetail.Student_ID)
LEFT JOIN TblStudentSubAss
ON TblStudentDetail.StudentDetailID = TblStudentSubAss.StudentDetailID
LEFT JOIN TblClassSchedule
ON TblStudentDetail.ClassSchId = TblClassSchedule.ClassSchID
LEFT JOIN TblSubAss
ON TblSubAss.SubAssId = TblStudentSubAss.SubAssId
LEFT JOIN TableClass AS CID
ON TblClassSchedule.ClassID = CID.ClassID
LEFT JOIN TBLCOLLEGE
ON CID.CollegeId = TBLCOLLEGE.CollegeID
LEFT JOIN tableSemAssigning SA
ON TblClassSchedule.SemAssId = SA.SemAssId
LEFT JOIN TblAcademicYear AY
ON SA.AcademicYearId = AY.AcademicYearId
LEFT JOIN TableSemester Smst
ON Smst.SemesterId = SA.SemesterId

Show results of joined tables even if some values don't match

Access Database
table contacts
--------------
id
surname
name
table relations
---------------
contact_id
relation_id
Both contact_id and relation_id are foreign keys referenced to table contacts' id
I want to execute a query to get both the contact's surname/name and the relation's surname/name if a relation for the current contact exist. If it doesn't exist I want to get the contact's surname/name and blank values for the relation's fields.
All this in one query
EDIT:
I used left join. I am running the query using VB.NET:
Dim myOleDbDataReader As OleDbDataReader = _
New OleDbCommand( _
"SELECT c.id AS contact_id " & _
" , c.surname AS contact_surname " & _
" , c.name AS contact_name " & _
" , c2.id AS related_id " & _
" , c2.surname AS related_surname " & _
" , c2.name AS related_name " & _
"FROM ((contacts c " & _
"LEFT JOIN relations r " & _
"ON c.id = r.contact_id) " & _
"INNER JOIN contacts c2 " & _
"ON c2.id = r.relation_id)" _
, connection).ExecuteReader()
I get OleDbException: Join expression not supported.
They say in another post that:
"Access won't let you use conventional joins in the where clause when you use LEFT/RIGHT/INNER JOINS in the FROM clause. It is probably intentional to get you to buy more expensive software." - ( Is the join expression not supported by MS Access? )
It is not exactly that. From some examples I tried I came to the conclusion that:
Access won't let you use outer joins (LEFT/RIGHT) together with one or more INNER JOINS.
What in John Carmack's name can I do?
I would like to avoid seperate select queries.
Please help...
SELECT c.id AS contact_id
, c.surname AS contact_surname
, c.name AS contact_name
, c2.id AS related_id
, c2.surname AS related_surname
, c2.name AS related_name
FROM contacts c
LEFT JOIN relations r
ON c.id = r.contact_id
JOIN contacts c2
ON r.relation_id = c2.id
The above does NOT work in MS-Access.
This is slightly different (two left joins) but it works:
SELECT c.id AS contact_id
, c.surname AS contact_surname
, c.name AS contact_name
, c2.id AS related_id
, c2.surname AS related_surname
, c2.name AS related_name
FROM contacts c
LEFT JOIN
( relations r
LEFT JOIN contacts AS c2
ON r.relation_id = c2.id
)
ON c.id = r.contact_id
Despite the second LEFT JOIN, it will give same result set, since the second LEFT JOIN involves a Foreign Key relationship (in the direction from many -> one).
To have a LEFT JOIN with an INNER JOIN you could use:
SELECT c.id AS contact_id
, c.surname AS contact_surname
, c.name AS contact_name
, g.id AS related_id
, g.surname AS related_surname
, g.name AS related_name
FROM contacts c
LEFT JOIN
( SELECT r.contact_id
, c2.id
, c2.surname
, c2.name
FROM relations r
INNER JOIN contacts AS c2
ON r.relation_id = c2.id
) AS g
ON c.id = g.contact_id