SQL SELECT query to pull data from tables to calculate a sum - sql

I have four tables in a database, what I want to do is essentially list the employee number, name department number of any projects they are working on and the total hours that they spend working on projects.
I'm trying to do this all as one single SELECT statement, but am having trouble getting it to work with Oracle SQL. My latest attempt has been
SELECT EMPLOYEE.E#, EMPLOYEE.NAME, SUM(WORKSON.HOURS)
FROM EMPLOYEE JOIN WORKSON ON EMPLOYEE.E#=WORKSON.E#
GROUP BY EMPLOYEE.E#;
Which isn't even the complete statement, and gives me the error:
ERROR at line 1:
ORA-00979: not a GROUP BY expression
What can I do to complete the statement to also include the department number and what should I be doing? Should I be using a join for this? or should I be incorporating another SELECT statement within a WHERE clause?
The tables are as follows:
EMPLOYEE:
SQL> SELECT * FROM EMPLOYEE;
E# NAME DOB S SALARY SUPER D#
----- ------------------ ----------- -- ------ -------- ---
00100 Albert 13-OCT-65 M 186.5
00110 Alvin 13-OCT-77 M 156.4 00100 1
00120 Alice 17-JUN-73 F 156.5 00100 2
00150 Bob 02-JUL-60 M 166.4 00100 3
00200 Carl 02-FEB-67 M 156.3 00100 4
00250 Douglass 14-APR-83 M 156.4 00100 5
00101 Peter 13-NOV-76 M 85.2 00110 1
00103 Ami 12-SEP-85 F 78.2 00110 1
00107 Wendy 12-SEP-88 F 68.2 00110 1
00109 Michael 12-SEP-90 M 58.2 00110 1
00125 Angela 20-NOV-90 F 56.4 00120 2
00105 Robert 15-JAN-86 M 66.2 00150 3
00136 Aban 15-JAN-90 M 55.3 00200 4
00187 Eadger 07-APR-86 M 76.5 00250 5
14 rows selected.
WORKSON:
SQL> SELECT * FROM WORKSON;
E# P# HOURS
----- ------- -----
00110 1001 10
00101 1001 20
00150 1002 10
00105 1002 10
00105 1003 20
00105 1004 20
00250 1004 15
00187 1004 25
00105 1005 15
DEPARTMENT:
SQL> SELECT * FROM DEPARTMENT;
D# DNAME MANAG MSDATE
-- ------------------ ------ ------------
1 SALES 00110 02-JAN-12
2 ACCOUNTING 00120 30-OCT-10
3 GAMES 00150 01-MAR-08
4 HUMAN RESOURCES 00200 02-JAN-13
5 SPORTS 00250 10-MAY-10
PROJECT:
SQL> SELECT * FROM PROJECT;
P# PTITLE SPONSOR D# BUDGET
------ --- -------------- ---------------------- --- -------
1001 Computation Microsoft 1 25000
1002 Study methods Education committee 3 15000
1003 Racing car Cloud Pty Ltd 3 225000
1004 Football Football club 5 35000
1005 Swimming Education committee 5 125000
Any help would be greatly appreciated. Thanks in advance!

You first find the sum of hours of each employee and then join the same with your main table. Inline view can be used for the same.
SELECT EMPLOYEE.E#, EMPLOYEE.NAME, NVL(WORKSON_AGGR.TOTAL_HOURS,0)
FROM EMPLOYEE
LEFT JOIN (SELECT E#, SUM(HOURS) as TOTAL_HOURS
FROM WORKSON
GROUP BY E# ) WORKSON_AGGR
ON (WORKSON_AGGR.E# = EMPLOYEE.E#)
Now, you can join your department table too easily.
SELECT
E.E#,
E.NAME,
NVL(WORKSON_AGGR.TOTAL_HOURS,0),
D.DNAME
FROM EMPLOYEE E
LEFT JOIN (SELECT E#, SUM(HOURS) as TOTAL_HOURS
FROM WORKSON
GROUP BY E# ) WORKSON_AGGR
ON (WORKSON_AGGR.E# = E.E#)
JOIN DEPARTMENT D ON (D.D# = E.D#)

Related

How do I display rows from a max count?

I want to return all the data, from max count query with hospital that has most number of patients. What I seem to be getting when I try to nest queries is display of all rows of hospital data. I've tried to look at similar questions in stack overflow and other sites it seems to be a simple query to do but i am not getting it.
select max(highest_hospital) as max_hospital
from (select count(hospital) as highest_hospital
from doctor
group by hospital)
highest_hospital
-------------
3
Doc ID Doctor Patient Hospital Medicine Cost
------ ------- ------ --------- ------ --------
1 Jim Bob Patient1 Town 1 Medicine 1 4000
2 Janice Smith Patient2 Town 2 Medicine 3 3000
3 Harold Brown Patient3 Town 2 Medicine 5 2000
4 Larry Owens Patient4 Town 2 Medicine 6 3000
5 Sally Brown Patient5 Town 3 Medicine 7 4000
6 Bob Jim Patient6 Town 4 Medicine 8 6000
Outcome should be return of 3 rows
Doc ID Doctor Patient Hospital Medicine Cost
------ ------- ------ --------- ------ --------
2 Janice Smith Patient2 Town 2 Medicine 3 3000
3 Harold Brown Patient3 Town 2 Medicine 5 2000
4 Larry Owens Patient4 Town 2 Medicine 6 3000
You can use window functions:
select d.*
from (select d.*, max(hospital_count) over () as max_hospital_count
from (select d.*, count(*) over (partition by hospital) as hospital_count
from doctor d
) d
) d
where hospital_count = max_hospital_count;
Edit:
Using GROUP BY is a pain. If you are only looking for a single hospital (even when there are ties), then in Oracle 12C you can do:
select d.*
from doctor d
where d.hospital = (select d2.hospital
from doctor d2
group by d2.hospital
order by count(*) desc
fetch first 1 row only
);
You can do this in earlier versions of Oracle using an additional subquery.

Write a query to display the names of the department and the staff count in each department, if staff not exist display the count as 0,

The code is running properly giving necessary output but one of the test cases fails.
select department_name,count(*) as staff_count
from department d,staff s
where d.department_id=s.staff_id
group by department_name
order by department_name;
Proposed grade: 50 / 100
Result Description
Failed Test
Test Case 2
Summary of tests
+------------------------------+
| 2 tests run / 1 test passed |
+------------------------------+
Sample output with necessary input data:
DEPARTMENT_NAME STAFF_COUNT
------------------------------ -----------
CSE 2
ECE 1
EEE 2
IT 2
SE 4
DEPARTMENT_ID DEPARTMENT_NAME DEPARTMENT_BLOCK_NUMBER
------------- ------------------------------ -----------------------
1 CSE 3
2 IT 3
3 SE 3
4 ECE 2
5 EEE 2
STAFF_ID STAFF_NAME DEPARTMENT_ID
---------- ------------------------------ -------------
1 Lakshmi 1
2 Venky 1
3 Senthil 2
4 Sandhya 2
5 Geetha 3
6 Tom 3
7 Rekha 3
8 Viji 3
9 Laya 4
10 Nisha 5
11 Venki 5
You need an outer join for this, not an inner join (which your old and outdated implicit join in the WHERE clause does). You also need to do the join between the two department_id columns.
select department_name, count(s.staff_id) as staff_count
from department d
left join staff s on d.department_id = s.department_id
group by department_name
order by department_name;
The left join (=outer join) will return NUL for s.staff_id for departments that do not have staff. Aggregate functions simply ignore NULL values and that's how you get the 0 for the staff count.
However your sample data does not have any department without staff.
Online example with an additional empty department: https://rextester.com/EEXE2322

Duplicating multiple records in table with primary key

I am working with duplicating multiple records in table 1 with primary key which has a reference with table 2 (foreign key)
Consider table employee with columns (eid(primary key),ename,dept,dept_code).
Table 2 employee_address with columns (eaid(primary key), eid(foreign key), city, country)
Now the task is the table 1 (ie. employee) may have number of values with different department codes.
Suppose dept_code may be 1 or 2 like so and so.
Now with both the table with some data look like this
EMPLOYEE table
Eid(pk) ename dept Dept_code
100 Sss Cse 1
101 Aaa Cse 1
102 Bbb Cse 1
103 Ccc Eee 2
104 ddd it 3
EMPLOYEE ADDRESS table
Eaid (pk) Eid (fk) city country
1 100 NY Us
2 100 NY Us
3 100 NY Us
4 101 CALIF Us
5 102 DC Us
6 102 DC Us
7 103 NJ Us
now the thing is i have to duplicate all the records in employee table where dept_code=1 and duplicate records dept_code should be = 5.
And the foreign key referencing to employee address table (ie. here in the current document 100,101,102) in the table 2 has to be duplicated number of times those foreign key value present in table 2.
I have to get the output like this
Employee table
Eid(pk) ename dept Dept_code
100 Sss Cse 1
101 Aaa Cse 1
102 Bbb Cse 1
103 Ccc Eee 2
104 ddd it 3
**105 sss cse 5
106 aaa cse 5
107 bbb cse 5**
Employee Address table
Eaid (pk) Eid (fk) city country
1 100 NY Us
2 100 NY Us
3 100 NY Us
4 101 CALIF Us
5 102 DC Us
6 102 DC Us
7 103 NJ Us
**8 105 NY Us
9 105 NY Us
10 105 Ny Us
11 106 CALIF Us
12 107 DC Us
13 107 DC Us**
Now I have to get the output like this.
Conditions for this task is,
no for loop
hitting a database should be as minimal as possible
can use java or if posssible in sql query itself.
I tried with various ideas nothing worked out.
Help me out in this.
If I understand your question, these two queries will work. They use tSql window functions, if you are not using Sql Server then you will need to find the equivalent.
Edited to answer comment from OP-
If you want to create new tables:
Select * Into EmployeeNew From (
Select *
From Employee E
Union All
Select Row_Number() Over (Order By eid) + (Select Max(eid) From Employee),
ename, dept, 5 dept_code
From Employee
Where Dept_Code = 1) A
Select * Into EmployeeAddressNew From (
Select A.*
From Employee E
Join EmployeeAddress A On A.eid = E.eid
Union All
Select A.eaid + (Select Max(eaid) From EmployeeAddress),
Dense_Rank() Over (Order By E.eid) + (Select Max(eid) From Employee),
City, Country
From Employee E
Join EmployeeAddress A On A.eid = E.eid
Where E.Dept_Code = 1) A
If you only want to insert 'new' records into the original tables:
Insert Employee
Select Row_Number() Over (Order By eid) + (Select Max(eid) From Employee),
ename, dept, 5 dept_code
From Employee
Where Dept_Code = 1
Insert EmployeeAddress
Select A.eaid + (Select Max(eaid) From EmployeeAddress),
Dense_Rank() Over (Order By E.eid) + (Select Max(eid) From Employee),
City, Country
From Employee E
Join EmployeeAddress A On A.eid = E.eid
Where E.Dept_Code = 1

JOIN 3 tables and COUNT for result

I am using an a join query between three tables. My query doesn't display entirely my desired result. There are three tables team, school and game. For example, how can I display the total wins Lawrence North High School(school_id = 11111) has?
Current query:
SELECT school.school_name
FROM school
INNER JOIN team ON school.school_id = team.school_id
INNER JOIN game ON team.team_id = game.game_id
WHERE school.school_id = 11111
tables used:
SQL> Select * From Team;
TEAM_ID SPORT_ID SCHOOL_ID TEAM_NAME
---------- ---------- ---------- ----------
1 1 11111 WildCats
2 2 11111 WILDCATS
3 3 11111 WildCats
4 4 11111 WILDCATS
5 5 11111 WildCats
6 6 11111 WildCats
7 7 11111 WildCats
8 1 123134 Bears
9 2 123134 Bears
10 3 123134 BEARS
11 4 123134 Bears
12 5 123134 Bears
13 6 123134 BEars
SQL> Select * From School;
SCHOOL_ID SCHOOL_NAME SCHOOL_TYPE
---------- ----------------------------- ------------
11546 Ivy Tech College COLLegE
11090 LAWRENCE Central Grade School GRADE SCHOOL
11111 Lawrence NORTH High School HIGH SCHooL
19283 Howe High SCHOOL High SchooL
123134 Lawrence Central High School HIGH SCHOOL
192 Little Big Horn Grade School GRADE SCHOOL
SQL> Select * From Game;
TEAM_ID GAME_DATE R
---------- --------- -
1 10-SEP-98 w
1 12-SEP-98 W
2 17-SEP-98 w
2 18-SEP-98 W
3 18-SEP-98 w
3 11-SEP-98 W
4 20-SEP-98 L
4 21-SEP-98 l
5 21-SEP-98 L
5 19-SEP-98 l
6 26-SEP-98 L
6 27-SEP-98 W
7 28-SEP-98 w
7 29-SEP-98 W
8 11-OCT-98 W
8 12-OCT-98 w
9 13-OCT-98 L
9 14-OCT-98 L
10 14-OCT-98 L
11 15-OCT-98 L
12 16-OCT-98 W
13 17-OCT-98 W
11 18-OCT-98 W
12 19-OCT-98 W
13 11-NOV-98
11 12-NOV-98
Try this
SELECT count(*) as Total_wins FROM school INNER JOIN team ON school.school_id = team.school_id INNER JOIN game ON team.team_id = game.game_id WHERE school.school_id = 11111 and game.r = 'W'
You can use the COUNT With Where clause with Group By on the schoolname.
SELECT school.school_name, COUNT(game.R) as TotalWins
FROM school INNER JOIN team
ON school.school_id = team.school_id
INNER JOIN game
ON team.team_id = game.game_id
WHERE
school.school_id = 11111
AND game.R = 'W'
If you are doing it for the one school you can just remove the Group By but if you want to get the count for all schools you need to apply the Group By and remove the Chool_id filter from Where clause.
You will also have to specify the sport id in the where clause.
where school.school_id = 11111 and
team.sportid = (what ever the sport id you want to see)

How does select query works?

I having trouble with this query
it is executing quit well but I cannot make out
how is this select statement working.
Any help or explanation on this problem will be appreciated ..
thank you
these are my tables and query
here am looking for the employee who lives in same city as the the company for which they work
Table:-emp
eid name street city
----------- ---------------- ------------- ------------
1 yeman asd vasai
2 aksh adssd mumbai
3 chintan ghfgh mumbai
4 samual ghfdgh bandra
5 ddlj fghfgh andheri
6 jack fghnfg Bandra
7 bridge gfhfgh vasai
8 rahim ghfgh mumbai
9 chirag fghfghfg bandra
10 mistry hhhty bandra
11 ravi tytey andheri
Table:- company
cid companyname city
----------- ------------------- ------------
1 Vasai Industries vasai
2 Mumbai Pharmacy mumbai
3 bandra loft bandra
4 andheri tactics andheri
Table:= works
eid cid salary
----------- ----------- -----------
1 1 200
2 3 4831
3 4 4457
4 2 20001
5 1 32221
6 2 224
7 3 784
8 1 336
9 3 2489
10 2 4789
11 1 22541
Query
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
why not use this query with joins and its easy to understand then a bunch of subqueries.
select * from emp
inner join works on works.eid = emp.eid
inner join company on company.city=emp.city
Explanation:
1.select cid from company where city=emp.city
Here you are getting city id regarding cities which are same in emp and company
2.
select eid from works
where cid=(select cid from company
where city=emp.city)
Here you getting collection of id's from works table which cid is same in emp and company
3.
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
here you are getting all records based on emp id's whose cities are same in emp and city