SQL - Add total from columns of two seperate tables - sql

Complete novice here, trying to find out the total number of students from both the part-time students and full-time students and display the total in a named column.
partTimeStudents**(bannerID, moduleCode, modStartDate, rvisitorID)
fullTimeStudents**(bannerID, courseCode, crsStartDate, rvisitorID)
Thank you in advance for any help given :)

select
(select count(*) from partTimeStudents)+
(select count(*) from fullTimeStudents) as Total

I agree what others have said about the database design, but here's one example of a query that would fulfill your requirement.
SELECT SUM(students_count)
FROM (
SELECT COUNT(*) AS students_count
FROM partTimeStudents
UNION ALL
SELECT COUNT(*) AS students_count
FROM fullTimeStudents
)

You should not take 2 tables just to distinguish it by full/part time student. You can simply take a flag in table like:
students(bannerID, Code, modStartDate, rvisitorID, timeFlag)
In timeFlag you can manage whether the student is full time or part time.
Now to get count of all students[full time + part time]:
select count(*) from students;
And to have counts for fullTime students or partime students:
select count(*) from students where tiemFlag=1; //--- assuming 1 is for fulltime
select count(*) from students where tiemFlag=0; //--- assuming 0 is for parttime

Related

SQL : Getting count of a column and then multiplying it with a column from another table

I have three tables in my database,
Students-has student id,
Instructors which has instructor id, course name and hourly pay.
Enrollment table which has student id, course name, instructor.
I need to write a query to get the max amount of money paid to an instructor. Somethings to consider
A student can enroll into multiple courses
An instructor can teach multiple courses (Instructor is paid the same for each course)
Multiple instructors can teach the same course.
I came up with the required tables to get the entries first.
select hourlyPay, ins_id, stu_id from Instructors, Enrollment where Enrollment.ins_id = Instructors.Instr_id group by hourlyPay,ins_ID,stu_id
This gives me the output:
My end goal is to multiply the hourly pay with the student count and get the maximum, Could somebody please help me do it? I want the student count per instructor multiplied with the hourly pay.
Since I don't know the data and complete requirement. You can try something below:
(This top 1 works in SQL Server). I think in Orcle you need try FETCH FIRST number ROWS ONLY;)
select top 1 ins_id,sum(hourlyPay) Pay
from(
select hourlyPay
, ins_id
, stu_id
from Instructors
Join Enrollment ON Enrollment.ins_id = Instructors.Instr_id
group by hourlyPay,ins_ID,stu_id
) a
group by ins_id
order by 2 desc

SQL query to return only students with specific score on all tests

Have a table for student test scores.
Students take multiple quizzes during a period.
Trying to develop a query that will pull only the students with 100% on all of the quizzes.
Table - Students
StudentId,
QuizScore
Certainly a student could receive 100% on a quiz or multiple quizzes, but just a list of the students whom scored 100% on all quizzes.
Thinking of a nested query but drawing a blank :(
Thanks in advance
Select studentid
From studentscores
Group by studentid
Having avg(quizscore) = 100
You can do an inner select to find students with at least one score that isn't 100, and then select all the StudentIDs that aren't in that set.
SELECT Distinct StudentID FROM Students WHERE StudentID NOT IN
(SELECT StudentID FROM Students WHERE QuizScore != 100)
You also want to use Distinct to get rid of multiple lines.
There are several ways to do this, one is to compare the count of all the results to those with 100. You can use case with count for that:
select studentid
from students
group by studentid
having count(case when quizscore >= 100 then 1 end) = count(*)
If quizes could have higher than 100, I've changed = to >= in the case statement to account for those. Otherwise, go with #Rajesh's solution as I believe it's the easiest to check for all 100s.

Access flag record when match found in table 2 (one-to-many relationship)

I have an educational db with a tbl_Students and tblStudentPrograms. The tblStudentPrograms has one record per student and program (ProgramID) per year (YearID.
I need to find out how many students participated in ProgramID=2 EVER. So, I need the DISTINCT subset of students who have participated in the program for any YearID.
(Of course, this will be complicated further by trying to find other records in other tables such as StudentAdvising as well , but this will be a good start.)
Thank you!
SELECT DISTINCT count(studentID) FROM tblStudentPrograms WHERE ProgramID = 2
Assuming you want a distinct count of students (excluding those where the same student may have taken a program twice...
SELECT count(distinct StudentID)
FROM tblStudentPRograms
WHERE ProgramID = 2
Assuming you want a distinct count of students (excluding those where the same student may have taken a program twice...
SELECT count(distinct StudentID)
FROM tblStudentPRograms
WHERE ProgramID = 2
though I'm not positive access supports a distinct w/in a count like other RDBMS do...
so you may have to do:
SQL : how can i count distinct record in MS ACCESS
SELECT count(BStudentID) as DistinctStudentsInProgram
FROM (select distinct studentID, ProgramID from tblStudentPrograms) B
WHERE B.ProgramID = 2

SQL: Access query unique meets multiple criterias

I have a table containing the columns studentID, course and start_date where studentID is unique.
I want to write a query so that students that haven't gone to any courses with a start date within the year 2010 appears. My problem is that I seem to return all students that have gone to any course that isn't 2010 even though they have attended a course during 2010.
SELECT DISTINCT studentID
FROM attendant
WHERE Startdatum NOT BETWEEN #1/1/2010# AND #12/31/2010#
What I'm really asking is; Is there any way to not show a row if it contains a certain value? How?
Subquery of some sort?
It should be something like:
SELECT DISTINCT studentID FROM attendant
WHERE studentID NOT IN
(SELECT DISTINCT studentID FROM attendant
WHERE Startdatum BETWEEN #1/1/2010# AND #12/31/2010#)
A good way to answer this question is using the having clause:
SELECT studentID
FROM attendant
GROUP BY studentID
HAVING sum(iif(Startdatum BETWEEN #1/1/2010# AND #12/31/2010#, 1, 0)) = 0;
The having clause is counting the number of courses that each student took during the time frame. If there are any, then the filter fails.

Simple SQL (hopefully!) Query Question

I hope someone can help me with my faltering steps to formulate a SQL query for the following problem.
I have a simple table that records visitor names and dates. The relationship is many to many, in that for any given date there are many visitors, and for any given visitor there will be one or more dates (i.e. repeat visits). There is a complicating third column that records the name of the exhibit(s) the visitor interacted with. The data might look like this:
NAME ART DATE
Joe Picture 1 23-1-09
Joe Picture 2 23-1-09
Joe Picture 3 23-1-09
Janet Picture 2 23-1-09
Joe Picture 2 31-2-09
I want to know what the distribution of single and multiple visits are, in other words, how many people only visited once, how many people visited on 2 separate days, how many on 3 separate days, and so on.
Can anyone help please? Thank you in anticipation!
Frankie
If you only want to count the total number of distinct visits, including multiple visits on the same date, you could use:
SELECT [Name],
COUNT(*) AS Count_Dates
FROM MyTable
GROUP BY [Name]
However, if you don't want to count multiple visits on the same date, you could use the following:
SELECT [Name],
COUNT(*) AS Count_Dates
FROM
(
SELECT DISTINCT [Name],
[Date]
FROM MyTable
) a
GROUP BY [Name]
This will show you the distribution of total people who visited x times per day. However, this will not display numbers for counts where 0 people visited that many times - for example, if nobody visited 8 times, then there won't be a row for Count_Dates = 8. If you did want to display a full list from 0-10 visits, you could create a temp table of Count_Dates and insert values from 0-10, then use it as part of the main query.
SELECT Count_Dates,
COUNT(*) AS Count_Visitors
FROM (SELECT [Name], COUNT(DISTINCT [Date]) AS Count_Dates FROM MyTable GROUP BY [Name]) a
GROUP BY Count_Dates
ORDER BY Count_Dates
SELECT NAME, COUNT(ART) as num_exhibits, COUNT(DATE) as num_days
FROM table GROUP BY NAME;
This will give you a table of each name along with the total number of visits for that name and the total number of dates visited.
To get an average exhibit per date you can do:
SELECT
NAME,
COUNT(ART) as num_exhibits,
COUNT(DATE) as num_days,
(num_exhibits / num_days) as avg_exhibit_per_day
FROM table GROUP BY NAME;