Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to extract some data using SQL queries from different tables I have already created. The tables are as follows:

SQL
CREATE TABLE Students 
(
Student_ID INTEGER PRIMARY KEY,
Enrollment_Year DATE,
Course_Current_Status VARCHAR(18), 
First_Name TEXT,
Last_Name TEXT,
Gender TEXT,
Date_Of_Birth DATE,
Email TEXT,
CourseCode INTEGER REFERENCES Courses(CourseCode)
);

CREATE TABLE Modules 
(
Module_Code INTEGER PRIMARY KEY,
Module_Name TEXT, 
Module_Credits INTEGER,
Module_Level INTEGER,
ConvenerID INTEGER REFERENCES Conveners(ConvenerID)
);

CREATE TABLE Enrollment 
(
Marks_Obtained INTEGER,
Module_Code INTEGER REFERENCES Modules(Module_Code),
Student_ID INTEGER REFERENCES Students(Student_ID),
Program_Year_When_Enrolled TEXT, 
PRIMARY KEY(Module_Code, Student_ID)
);


I want to show three columns with my query:

Student_ID, Average_Second_Year_Marks, Average_Third_Year_Marks, Overall_Marks
What I want to do is extract data for students graduating in 2017 i.e. the Course_Current_Status = 'Graduated-2017'

For second year marks the Enrollment.Program_Year_When_Enrolled = 'Second' And for the third year marks the Enrollment.Program_Year_When_Enrolled = 'Third'. For the overall marks a new column would have to be created by the query i.e. Overall Marks which would be 1/3 of the second year marks and 2/3 of the third year marks.

What I have tried:

What I'm using is as follows:

SQL
SELECT 
Students.Student_ID, 
AVG (Enrollement.Marks_obtained WHERE Enrollment.Program_Year_When_Enorolled = 'Third' ) AS avg_third_year_marks,
AVG (Enrollement.Marks_obtained WHERE Enrollment.Program_Year_When_Enorolled = 'Second' ) AS avg_third_year_marks
SUM (avg_third_year_marks*2/3 +avg_second_year_marks*1/3)
FROM 
Students LEFT JOIN
Enrollment ON Students.Student_ID=Enrollment.Student_ID
WHERE 
Students.Course_Current_Year='Graduated-2017'
GROUP BY 
Students.Student_ID
Posted
Updated 9-Apr-19 8:25am
v2

1 solution

You say you have tried this. I assume this is an attempt at the syntax (rather than it returning incorrect results) as there are errors:

1. Enrollement is not a table name - should be Enrollment
2. Program_Year_When_Enorolled - this is a misspelling isn't it?
3. avg_third_year_marks has been specified as an output column twice
4. Course_Current_Year is not a column in the Students table

Can you rectify and resumbit please. Meanwhile I'll see if I can make it work using some assumptions.

Here's a solution if I understood your problem correctly:
SQL
SELECT *, 
Averages.avg_third_year_marks*2/3 + Averages.avg_second_year_marks*1/3 AS overall 
FROM 
(
	SELECT 
	Student_ID,
	(
		SELECT AVG(Marks_obtained) 
		FROM Students s1 
		LEFT JOIN Enrollment e1 ON s1.Student_ID=e1.Student_ID 
		WHERE s1.Student_ID=s.Student_ID 
		AND Program_Year_When_Enrolled = 'Third'
	) AS avg_third_year_marks,
	(
		SELECT AVG(Marks_obtained) 
		FROM Students s2 
		LEFT JOIN Enrollment e2 ON s2.Student_ID=e2.Student_ID 
		WHERE s2.Student_ID=s.Student_ID 
		AND Program_Year_When_Enrolled = 'Second'
	) AS avg_second_year_marks
	FROM 
	Students s
) AS Averages

"Yes thank you very much this works perfectly. However, the results should not be shown for all students, they should relate only to those students whose Course_Current_Year in the students table is 'Graduated-2017'. Can you please tell me how to place this additional restriction. Thanks again"

I've rearranged the post as I was replying incorrectly and I've now lost your reply. Sorry about that...

As I said in my original post there is no column Students.Course_Current_Year so I'm not sure how you can achieve that. Let me know if you need to correct your table definition first and we'll go from there.
 
Share this answer
 
v6
Comments
The_Legend_99 8-Apr-19 9:22am    
Hi Steve,

Wasn't able to access my account so created a new one but I have sorted this out using an additional where clause. Thank you very much once again
Steve Grattan 10-Apr-19 6:41am    
Glad to help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900