query for summing up values present in different columns in sql server 2005 - sql-server-2005

i have columns i.e. theory marks, pratical marks, student's Id ,course Id ,& subject Id
i need to add up the vaules present in columns theory marks & practical marks in order to get the aggregate, i dont know how to add the column values though. I am using sql server 2005
please help

assuming theory marks and practical marks are numerical data types
SELECT
student_id,
course_id,
subject_id,
SUM(theory_marks + practical_marks) AS overall_mark
FROM
table
GROUP BY
student_id, course_id, subject_id

Related

Retrieving column value in table2 via same ID in table1

I have this SQL query that returns overdue assignments
SELECT DUE_DATE,
SUBJECT,
ASSIGNMENT,
STUDENT_NAME,
TEACHER_NAME
FROM(SELECT DISTINCT
a.due_date AS due_date,
a.subject AS subject,
a.assignment AS assignment,
a.student_name AS student_name,
a.student_id AS student_id,
a.teacher_name AS teacher_name,
a.teacher_id AS teacher_id
FROM DB.ASSIGNMENT a,
DB.ALL b,
WHERE (trunc(a.DATE_CREATED) >= trunc(db.utc_sysdate)))
WHERE((trunc(due_date) < trunc(db.utc_sysdate));
and I want to include both the teacher and student emails as additional columns in my SQL query - I was wondering how to map their id in table ASSIGNMENT in order to get their respective emails in table ALL with the existing query I have?
We do lack some information, but - wouldn't your query be like this?
select distinct
a.due_date,
a.subject,
a.assignment,
a.student_name,
a.student_email,
a.teacher_name,
a.teacher_email
from db.assignment a join db.all b
on trunc(a.date_created) >= trunc(b.utc_sysdate)
and trunc(a.due_date) < trunc(b.utc_sysdate);
What's the difference, if compared to your query?
your query is invalid
comma after db.all b
the final where clause references db. "alias" (although it is probably schema name, according to inline view's from clause)
there's no point in aliasing column names using exactly the same name; what's the difference between a.due_date as due_date and a.due_date itself? None. So don't use it, you're just causing confusion
as you want to include student's and teacher's e-mail addresses, why don't you just do that? Add those columns into the query ...
it seems that you don't need an inline view; put both where conditions into the same query and remove columns you don't need (both IDs)

How to combine multiple row values in Oracle SQL in a single table

I've been having trouble combining multiple row values into a single column for each semester.
The table (GENERALEDPATHWAY) has these columns:
*STUDENTID
*SEMESTER
*CLASS
*CLASS_COMBINATION (all values currently null)
*YEAR
*CLASS_GRADE
*ENTRYPOINT
*DEGREE
*CLASS_DISTRIBUTION
*DEGREE
*GRADUATED_IN
I'm only currently worried about the STUDENTID, SEMESTER, CLASS, and Class_Combination. Every student has a unique ID and may have a different combination of classes each semester. Instead of having a separate row for every class every semester, I want to put the class values into the CLASS_COMBINATION column. EX: instead of having 5 rows for 5 classes taken in a single semester, I just want 1 row for that semester with all classes listed alphabetically separated by commas in the CLASS_COMBINATION column.
The difficulty I'm having is that all of the information is in a single table and needs to work in Oracle SQL Developer.
Try this query:
Select studentid, semester, class,
Listagg(class) within group (order by class) as CLASS_COMBINATION
From GENERALEDPATHWAY
Group by studentid, semester, class;
If you want to update the table. Then use it in update or merge query to update each recoed and then remove the duplicate rows.
Cheers!!
What you describe is an aggregation query, with one row per student and semester.
You do not need a separate table for this. My recommendation is either a simple query or a view.
Your data structure describes STUDENTID as the primary key. However, you specify that you have multiple rows for a STUDENTID, so something is wrong -- either your explanation or the data model. From what you describe, you want:
select STUDENTID, semester,
listagg(class ', ') within group (order by class) as classes
from GENERALEDPATHWAY
group by STUDENTID, semester;

what does Group By multiple columns means?

I use oracle 11g , so i read alot of artics about it but i dont understand
how exactly its happened in database , so lets say that have two tables:
select * from Employee
select * from student
so when we want to make group by in multi columns :
SELECT SUBJECT, YEAR, Count(*)
FROM Student
GROUP BY SUBJECT, YEAR;
so my question is: what exactly happened in database ? i mean the query count(*) do first in every column in group by and then sort it ? or what? can any one explain it in details ?.
SQL is a descriptive language, not a procedural language.
What the query does is determine all rows in the original data where the group by keys are the same. It then reduces them to one row.
For example, in your data, these all have the same data:
subject year name
English 1 Harsh
English 1 Pratik
English 1 Ramesh
You are saying to group by subject, year, so these become:
Subject Year Count(*)
English 1 3
Often, this aggregation is implemented using sorting. However, that is up to the database -- and there are many other algorithms. You cannot assume that the database will sort the data. But, if it easier for you to think of it, you can think of the data being sorted by the group by keys, in order to identify the groups. Just one caution, the returned values are not necessarily in any particular order (unless your query includes an order by).

Joining section in the From clause

Here,I have used joining section in the from clause ...
select course_id, semester, year, sec_id, avg (tot_cred)
from takes natural join student
where year = 2009
group by course_id, semester, year, sec_id
having count (ID) >= 2
Now, My question is , Is this sql query correct ? If yes then why ? Or If not then why ? Thanks.
Your query is technically correct. However, I would advise you strongly to never using natural join. It is a bug waiting to happen. Why? It uses the names -- and only the names -- of columns in the underlying tables. It does not even use declared foreign key relationships.
Instead, use an explicit on or using clause:
select courseid, semester, year, secid, avg(totcred)
from takes t join
student s
using (studentid)
where year = 2009
group by courseid, semester, year, secid
having count(*) >= 2;
Also:
I assume that the spaces in "course id", "sec id", and "tot cred" are simply typos.
Use table aliases.
Qualify the column names -- that is identify what table they are coming from.

Retrieving duplicate and original rows from a table using sql query

Say I have a student table with the following fields - student id, student name, age, gender, marks, class.Assume that due to some error, there are multiple entries corresponding to each student. My requirement is to identify the duplicate rows in the table and the filter criterion is the student name and the class.But in the query result, in addition to identifying the duplicate records, I also need to find the original student detail which got duplicated. Is there any method to do this. I went through this answer: SQL: How to find duplicates based on two fields?. But here it only specifies how to find the duplicate rows and not a means to identify the actual row that was duplicated. Kindly throw some light on the possible solution. Thanks.
First of all: if the columns you've listed are all in the same table, it looks like your database structure could use some normalization.
In terms of your question: I'm assuming your StudentID field is a database generated, primary key and so has not been duplicated. (If this is not the case, I think you have bigger problems than just duplicates).
I'm also assuming the duplicate row has a higher value for StudentID than the original row.
I think the following should work (Note: I haven't created a table to verify this so it might not be perfect straight away. If it doesn't it should be fairly close)
select dup.StudentID as DuplicateStudentID
dup.StudentName, dup.Age, dup.Gender, dup.Marks, dup.Class,
orig.StudentID as OriginalStudentId
from StudentTable dup
inner join (
-- Find first student record for each unique combination
select Min(StudentId) as StudentID, StudentName, Age, Gender, Marks, Class
from StudentTable t
group by StudentName, Age, Gender, Marks, Class
) orig on dup.StudentName = orig.StudenName
and dup.Age = orig.Age
and dup.Gender = orig.Gender
and dup.Marks = orig.Marks
and dup.Class = orig.Class
and dup.StudentID > orig.StudentID -- Don't identify the original record as a duplicate