Click here to Skip to main content
14,430,403 members
   

Database

 
AnswerRe: LinkedServer version Pin
Mycroft Holmes12-Apr-19 13:43
MemberMycroft Holmes12-Apr-19 13:43 
QuestionSQL Interview Pin
Member 1417944312-Apr-19 4:38
MemberMember 1417944312-Apr-19 4:38 
AnswerRe: SQL Interview Pin
Richard MacCutchan12-Apr-19 7:34
communityengineerRichard MacCutchan12-Apr-19 7:34 
GeneralRe: SQL Interview Pin
Member 1417944312-Apr-19 23:36
MemberMember 1417944312-Apr-19 23:36 
GeneralRe: SQL Interview Pin
Richard MacCutchan13-Apr-19 0:20
communityengineerRichard MacCutchan13-Apr-19 0:20 
GeneralRe: SQL Interview Pin
Eddy Vluggen16-Apr-19 3:54
professionalEddy Vluggen16-Apr-19 3:54 
AnswerRe: SQL Interview Pin
#realJSOP16-Apr-19 3:00
mva#realJSOP16-Apr-19 3:00 
QuestionQuerying from multiple tables at the same time - related to correlated subqueries Pin
User 141845817-Apr-19 6:12
MemberUser 141845817-Apr-19 6:12 
I'm trying to extract some data using SQL queries from different tables I have already created. The tables are as follows:

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'm using is as follows:

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_second_year_marks
AVG (Enrollment.Marks_obtained = 1/3 * avg_second_year_marks + 2/3 * avg_third_year_marks) AS Overall_Marks
FROM
Students LEFT JOIN
Enrollment ON Students.Student_ID=Enrollment.Student_ID
WHERE
Students.Course_Current_Year='Graduated-2017'
GROUP BY
Students.Student_ID
AnswerRe: Querying from multiple tables at the same time - related to correlated subqueries Pin
Mycroft Holmes7-Apr-19 13:08
MemberMycroft Holmes7-Apr-19 13:08 
AnswerRe: Querying from multiple tables at the same time - related to correlated subqueries Pin
Eddy Vluggen8-Apr-19 1:44
professionalEddy Vluggen8-Apr-19 1:44 
AnswerRe: Querying from multiple tables at the same time - related to correlated subqueries Pin
CHill6011-Apr-19 7:48
mveCHill6011-Apr-19 7:48 
QuestionHELP WITH SQL QUERY Pin
BrunoPigeon4-Apr-19 18:37
MemberBrunoPigeon4-Apr-19 18:37 
AnswerRe: HELP WITH SQL QUERY Pin
Mycroft Holmes4-Apr-19 21:38
MemberMycroft Holmes4-Apr-19 21:38 
GeneralRe: HELP WITH SQL QUERY Pin
BrunoPigeon4-Apr-19 21:56
MemberBrunoPigeon4-Apr-19 21:56 
GeneralRe: HELP WITH SQL QUERY Pin
GuyThiebaut4-Apr-19 22:06
professionalGuyThiebaut4-Apr-19 22:06 
GeneralRe: HELP WITH SQL QUERY Pin
BrunoPigeon4-Apr-19 22:10
MemberBrunoPigeon4-Apr-19 22:10 
GeneralRe: HELP WITH SQL QUERY Pin
GuyThiebaut4-Apr-19 22:20
professionalGuyThiebaut4-Apr-19 22:20 
GeneralRe: HELP WITH SQL QUERY Pin
GuyThiebaut4-Apr-19 22:43
professionalGuyThiebaut4-Apr-19 22:43 
AnswerRe: HELP WITH SQL QUERY Pin
GuyThiebaut4-Apr-19 21:56
professionalGuyThiebaut4-Apr-19 21:56 
GeneralRe: HELP WITH SQL QUERY Pin
BrunoPigeon4-Apr-19 21:58
MemberBrunoPigeon4-Apr-19 21:58 
GeneralRe: HELP WITH SQL QUERY Pin
Mycroft Holmes4-Apr-19 22:16
MemberMycroft Holmes4-Apr-19 22:16 
GeneralRe: HELP WITH SQL QUERY Pin
GuyThiebaut4-Apr-19 22:24
professionalGuyThiebaut4-Apr-19 22:24 
GeneralRe: HELP WITH SQL QUERY Pin
BrunoPigeon5-Apr-19 1:13
MemberBrunoPigeon5-Apr-19 1:13 
GeneralRe: HELP WITH SQL QUERY Pin
GuyThiebaut4-Apr-19 22:17
professionalGuyThiebaut4-Apr-19 22:17 
GeneralRe: HELP WITH SQL QUERY Pin
BrunoPigeon5-Apr-19 1:11
MemberBrunoPigeon5-Apr-19 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.