Oracle Error-ORA-00933: SQL command not properly ended - sql

I cannot figure out why I am getting this error message....
SELECT student.student, course.coursename, course.coursehours, section.day, section.starttime, course.building, location.room
FROM student, course, section, location, registration
WHERE course.courseid = section.courseid, location.locationid =
section.locationid, student.studentid = registration.sectionid,
section.sectionid = registration.sectionid;
Error at line 3:
ORA-00933: SQL command not properly ended
I updated my data to look like this:
SELECT student.studentid,
course.coursename,
course.credithours,
section.days,
section.starttime,
location.building,
location.room
FROM student
INNER JOIN registration
ON student.studentid = registration.sectionid
INNER JOIN section
ON section.sectionid = registration.sectionid
INNER JOIN location
ON location.locationid = section.locationid
INNER JOIN course
ON course.courseid = section.courseid;
but now it is saying "no rows selected"?

When you have more than one condition you need to use AND/OR operator to apply the condition not comma. Also start using INNER JOIN syntax
SELECT student.student,
course.coursename,
course.coursehours,
section.day,
section.starttime,
course.building,
location.room
FROM student
INNER JOIN registration
ON student.studentid = registration.sectionid
INNER JOIN section
ON section.sectionid = registration.sectionid
INNER JOIN location
ON location.locationid = section.locationid
INNER JOIN course
ON course.courseid = section.courseid

Related

How to join 7 tables in SQL using INNER JOIN

I am creating a stored procedure to join 7 tables in SQL using INNER JOIN, however I am getting an error about incorrect syntax.
I've tried to adjust formatting but I cannot get the syntax correct. Does anyone know where I've gone wrong? I get an error stating
Incorrect syntax near "WHERE"
My code:
CREATE PROCEDURE [dbo].[spInvoicing]
#InvoiceId INT
AS
BEGIN
SELECT
tblInvoices.InvoiceId, tblInvoices.InvoiceDate,
tblClients.Firstname, tblClients.Surname, tblClients.Address, tblClients.City,
tblClients.Postcode, tblClients.Email, tblClients.Phone,
tblAppointments.AppointmentId, tblAppointments.DateOfAppointment,
tblProductsUsed.ProductsUsedId, tblProducts.ProductName, tblProductsUsed.Quantity,
tblPointofSale.SaleId, tblPointOfSale.CostOfProducts, tblPointOfSale.CostOfServices,
tblPointOfSale.VAT, tblInvoices.SaleAmount, tblInvoices.PaymentMethod
FROM
tblClients
INNER JOIN
tblAppointments ON tblClients.ClientId = tblAppointments.ClientId
INNER JOIN
tblPointOfSale
INNER JOIN
tblInvoices ON tblPointOfSale.SaleId = tblInvoices.SaleId
INNER JOIN
tblProductsUsed
INNER JOIN
tblProducts ON tblProductsUsed.ProductId = tblProducts.ProductId
INNER JOIN
tblServicesRendered
INNER JOIN
tblServices ON tblServicesRendered.ServiceId = tblServices.ServiceId ON tblServicesRendered.AppointmentId = tblAppointments.AppointmentId
ON tblPointOfSale.AppointmentId = tblAppointments.AppointmentId
AND tblProductsUsed.SaleId = tblPointOfSale.SaleId
AND tblPointOfSale.ClientId = tblClients.ClientId
WHERE
tblInvoices.InvoiceId = #InvoiceId
END
Your JOINs got a bit scrambled. Try this below, and see if that fixes the syntax error.
Always try to get your JOINs to follow the format INNER JOIN [Table2] ON [Table2].[Field1] = [Table1].[Field1]
FROM tblClients
INNER JOIN tblAppointments ON tblClients.ClientId = tblAppointments.ClientId
INNER JOIN tblPointOfSale ON tblPointOfSale.AppointmentId = tblAppointments.AppointmentId
AND tblPointOfSale.ClientId = tblClients.ClientId
INNER JOIN tblInvoices ON tblPointOfSale.SaleId = tblInvoices.SaleId
INNER JOIN tblProductsUsed ON tblProductsUsed.SaleId = tblPointOfSale.SaleId
INNER JOIN tblProducts ON tblProductsUsed.ProductId = tblProducts.ProductId
INNER JOIN tblServicesRendered ON tblServicesRendered.AppointmentId = tblAppointments.AppointmentId
INNER JOIN tblServices ON tblServicesRendered.ServiceId = tblServices.ServiceId
I just copy and pasted your code into this website - http://poorsql.com/
and it hightlighted the error for you (you're missing AND in the joins).
As #DBro said - get in the habit of putting your JOINs on the new line and format it a little differently, and you'll have far less trouble.

SQL query with multiple where clause

I'm trying to write a query for a dataset in SSRS. I have a joining table called RequestSchools which links to three other tables:
Schools
Placing Request
RequestSchoolType
I want to get the name of the RequestedSchool (RequestedSchoolTypeId = 3)
I also want the name of the CatchmentSchool (RequestSchoolTypeId = 1)
I've attached an image of the SQL query that I can get working with the requested school. I want the catchment school as well. Grateful for any help with this.
SELECT
pr.PlacingRequestId, s.Name AS RequestedSchool
FROM
PlacingRequests AS pr
INNER JOIN
RequestSchools AS rs ON rs.PlacingRequestId = pr.PlacingRequestId
JOIN
Schools s ON s.SchoolId = rs.SchoolId
WHERE
rs.RequestSchoolTypeId = 3
I'm not 100% sure this is correct, as we can't see the schema, source data or a full example of the desired output, but I think this might be it:
SELECT
pr.PlacingRequestId,
s.Name as RequestedSchool,
cs.Name as CatchmentSchool
FROM
PlacingRequests AS pr
INNER JOIN RequestSchools AS rs
ON rs.PlacingRequestId = pr.PlacingRequestId
INNER JOIN Schools s
on s.SchoolId = rs.SchoolId
AND rs.RequestSchoolTypeId = 3 -- requested school
INNER JOIN Schools cs
on cs.SchoolId = rs.SchoolId
AND rs.RequestSchoolTypeId = 1 -- catchment school
Happy to update if it's not what you meant, and you can clarify the question as indicated.
According what I understand about your requirement, you will be needing multiple joins and not another AND condition in the where clause. So we can omit the where here and join the requestschools with schools one more time for CatchmentSchool as we have done it once already for Requestedschool. Only difference is we are using the requestedtypeid and catchmenttypeid values while joining.
Select
pr.PlacingRequestId,
rs.Name as RequestedSchool
cs.Name as CatchmentSchool
FROM
PlacingRequests AS pr
INNER JOIN RequestSchools AS rrs
ON rrs.PlacingRequestId = pr.PlacingRequestId
and rrs.RequestSchoolTypeId = 3
JOIN Schools rs
on rs.SchoolId = rrs.SchoolId
INNER JOIN RequestSchools AS crs
ON crs.PlacingRequestId = pr.PlacingRequestId
and crs.RequestSchoolTypeId = 1
JOIN Schools cs
on cs.SchoolId = crs.SchoolId
Hope this helps...
The best way to do this is with a CASE function, but I don't know which RDBMS you are using so here is the brute force approach: just run your query as two sub-queries, each specifying a different type ID and then join them. It would be something like this:
SELECT DISTINCT catch.placingrequestid, catch.catchmentschool, requested.requestedschool
FROM
(SELECT pr.placingrequestid, s.nam as catchmentschool
FROM (PlacingRequest pr
INNER JOIN RequestSchools rs ON pr.placingrequestid = rs.placingrequestid)
INNER JOIN School s ON rs.schoolid = s.schoolid
WHERE rs.requestschooltypeid = 1) catch
INNER JOIN
(SELECT pr.placingrequestid, s.nam as requestedschool
FROM (PlacingRequest pr
INNER JOIN RequestSchools rs ON pr.placingrequestid = rs.placingrequestid)
INNER JOIN School s ON rs.schoolid = s.schoolid
WHERE rs.requestschooltypeid = 3) requested
ON catch.placingrequestid = requested.placingrequestid

Power Query SQL code issue

I'm trying to run this SQL code in Power Query but keep getting the following error Message:
Incorrect syntax near the keyword 'GROUP'
The SQL code I'm using is
SELECT Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description, Count(History.HistoryID) AS CountOfHistoryID
FROM ((GroupAgent
INNER JOIN Groups ON GroupAgent.GroupID = Groups.GroupID)
INNER JOIN ((Agent LEFT JOIN History ON Agent.AgentID = History.AgentID)
LEFT JOIN CRC ON History.CRC = CRC.CRC) ON GroupAgent.AgentID = Agent.AgentID)
INNER JOIN (AgentTeams
INNER JOIN AgentStartDates ON AgentTeams.TeamID = AgentStartDates.TeamID) ON GroupAgent.AgentID = AgentStartDates.AgentID
WHERE (((History.CallDateTime) Between GetDate() And DateAdd(d,1,GetDate())
GROUP BY Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description
ORDER BY Groups.GroupName, AgentTeams.TeamName;
Could someone help me fix this or advise me where I'm going wrong?
Adam
Close the parentheses....
SELECT Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description, Count(History.HistoryID) AS CountOfHistoryID
FROM ((GroupAgent INNER JOIN Groups ON GroupAgent.GroupID = Groups.GroupID) INNER JOIN ((Agent LEFT JOIN History ON Agent.AgentID = History.AgentID) LEFT JOIN CRC ON History.CRC = CRC.CRC) ON GroupAgent.AgentID = Agent.AgentID) INNER JOIN (AgentTeams INNER JOIN AgentStartDates ON AgentTeams.TeamID = AgentStartDates.TeamID) ON GroupAgent.AgentID = AgentStartDates.AgentID
WHERE (((History.CallDateTime) Between GetDate() And DateAdd(d,1,GetDate())))
GROUP BY Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description
ORDER BY Groups.GroupName, AgentTeams.TeamName;
Hope it helps...

How to combine 2 sql statements

I am trying to retrieve data for a client from an old database that they have. The data I need is a mix of these 2 sql statements. How would I combine them to get all the data from the enrol_stud table and Stud_Haddress table in the second statement.
Statement 1 - I want all the data from Enrol_Stud and Stud_Haddress included in statement 2
select enrol_stud.*, stud_haddress.*
from enrol_stud_forms
inner join enrol_stud on enrol_stud_forms.student_id = enrol_stud.student_id
Statement 2 I want the data from the 2 tables above included in the statement below.
SELECT contact.*, vsmc.*, concarer1.*, salcarer2.*, contact.firstname ||'
'||contact.surname as STUDENT, salcarer1.salutation as CARER1_TITLE, concarer1.firstname as CARER1_FIRSTNAME, concarer1.surname as CARER1_SURNAME, concarer1.email_address as CARER1_EMAIL, salcarer2.salutation as CARER2_TITLE, concarer2.firstname as CARER2_FIRSTNAME, concarer2.surname as CARER2_SURNAME, concarer2.email_address as CARER2_EMAIL
FROM get_currently_enroled_students ('now') gces
INNER JOIN student on gces.student_id = student.student_id
INNER JOIN contact on student.contact_id=contact.contact_id
INNER JOIN view_Student_mail_carers vsmc on student.student_id=vsmc.student_id
INNER JOIN contact concarer1 on vsmc.carer1_contact_id=concarer1.contact_id
INNER JOIN contact concarer2 on vsmc.carer2_contact_id=concarer2.contact_id
INNER JOIN salutation salcarer1 on concarer1.salutation_id=salcarer1.salutation_id
INNER JOIN salutation salcarer2 on concarer2.salutation_id=salcarer2.salutation_id
ORDER BY contact.surname, contact.firstname
I know it is in the joins, I just can't work out how to include them.
You can try like below, include the columns from first statement per post as well include the joins as pointed below
SELECT contact.*,
vsmc.*,
enrol_stud.*, <- here
stud_haddress.*, <-here
concarer1.*,
salcarer2.*,
contact.firstname ||' '||contact.surname as STUDENT,
salcarer1.salutation as CARER1_TITLE,
concarer1.firstname as CARER1_FIRSTNAME,
concarer1.surname as CARER1_SURNAME,
concarer1.email_address as CARER1_EMAIL,
salcarer2.salutation as CARER2_TITLE,
concarer2.firstname as CARER2_FIRSTNAME,
concarer2.surname as CARER2_SURNAME,
concarer2.email_address as CARER2_EMAIL
FROM get_currently_enroled_students ('now') gces
INNER JOIN enrol_stud_forms on gces.student_id = enrol_stud_forms.student_id <- here
INNER JOIN enrol_stud on enrol_stud.student_id = gces.student_id <- here
INNER JOIN student on gces.student_id = student.student_id
INNER JOIN contact on student.contact_id=contact.contact_id
INNER JOIN view_Student_mail_carers vsmc on student.student_id=vsmc.student_id
INNER JOIN contact concarer1 on vsmc.carer1_contact_id=concarer1.contact_id
INNER JOIN contact concarer2 on vsmc.carer2_contact_id=concarer2.contact_id
INNER JOIN salutation salcarer1
on concarer1.salutation_id=salcarer1.salutation_id
INNER JOIN salutation salcarer2
on concarer2.salutation_id=salcarer2.salutation_id
ORDER BY contact.surname, contact.firstname

SQL with left outer join and 3 tables

I would like to add an ADD at the end of my code. Please have a look on my code and thanks for your support:
SELECT Area.org,
Supervisors.NomSup,
Supervisors.PrenomSup,
Employees.NomEmp,
Employees.PrenomEmp,
Employees.NoIdAlcanEmp,
Competencies.CodeCompetencies,
Competencies.CompetencyName,
LinkResultComp.AssNote,
LinkResultComp.AssDate
FROM ((((((
Area INNER JOIN Supervisors ON Area.IdArea = Supervisors.IdArea
)
INNER JOIN Employees ON Supervisors.IdSupervisor = Employees.IdSupervisor
)
INNER JOIN LinkProfilesEmployees ON Employees.IdEmp = LinkProfilesEmployees.IdEmp
)
INNER JOIN Profiles ON Profiles.IdProfiles = LinkProfilesEmployees.IdProfiles
)
INNER JOIN LinkProfComp ON Profiles.IdProfiles = LinkProfComp.IdProfiles
)
INNER JOIN Competencies ON Competencies.IdCompetencies = LinkProfComp.IdCompetencies
)
LEFT OUTER JOIN LinkResultComp ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
WHERE Area.org LIKE "*20*" AND Competencies.CodeCompetencies LIKE "khse2010-05"
ORDER BY Supervisors.NomSup, Employees.NomEmp;
Just remove the extra ON that you added
So change this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
------^^ This one
to this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
Of course I assume you meant different fields for the second condition