Solved

Oracle SQL AVG

Posted on 2014-12-31
2
168 Views
Last Modified: 2014-12-31
I have a little confusion with this.
Here is the situation.
There are tables:
PROJEKAT (PROJECT)
with attributes:
SPR (ID FOR PROJECT)
NAP (NAME OF PROJECT)
NAR (ID OF COMPANY WHO ORDERED PROJECT - CUSTOMER)
RUK (ID OF PROJECT MANAGER)

and table:
RADPROJ (EMPLOYEE_PROJECT)
with attributes:
SPR (ID OF PROJECT)
MBR (ID OF EMPLOYEE WORKING ON PROJECT)
BRC (NUMBER OF WORKING HOURS PER PROJECT PER EMPLOYEE)

I was writing a query to get ID of projects at which average working hours are bigger than average working hours on ALL projects.
And here is how it looks like:
--first solution
select rp.spr, p.nap, avg (rp.brc), (select avg (rp.brc) from radproj rp)
from radproj rp, projekat p
where rp.spr = p.spr
group by rp.spr, p.nap
having  avg (rp.brc) > (select avg (rp.brc) from radproj rp );

Open in new window


and it works OK, it yields results:
2      Skladistenje      13      11,15384615384615384615384615384615384615
5      Dijete      15,625      11,15384615384615384615384615384615384615

But to my big surprise this query as well gives same result:
-- second solution
SELECT p.spr, p.nap, AVG(rp.brc), AVG(rp1.brc)
FROM radproj rp, radproj rp1, projekat p
WHERE rp.spr=p.spr
GROUP BY p.spr, p.nap
HAVING AVG(rp.brc)>AVG(rp1.brc);

Open in new window


My question is how come that in second solution
AVG(rp.brc) gives average working hours per employee on project (as it should), but then second AVG(rp1.brc) which is basically exactly the same as first AVG function doesn't give the same but it "figures out" and give exactly what I need and tat is AVG working hours for ALL projects? Why the second AVG(rp1.brc) have not to be in (select avg (rp.brc) from radproj rp) format? What do I miss here?


Whole schema look like this:
Untitled.pngSchema is as:
Radnik({mbr,prz,ime,plt,god,sef,sodel,srm},{mbr})
Projekat({spr,nap,nar,ruk},{spr})
Radproj({spr,mbr,brc},{spr+mbr})
Odeljenje({sodel,nazod,lok},{sodel})
Radnomesto({srm,naz},{srm})

and referential integrities are:
Radnik[sef]Radnik[mbr]
Radnik[sodel]Odeljenje[sodel]
Radnik[srm]Radnomesto[srm]
Projekat[ruk]Radnik[mbr]
Radproj[spr]Projekat[spr]
Radproj[mbr]Radnik[mbr]

And whole radnikprojekat-tabele.sql script for creating all tables is in attachment.
radnikprojekat-tabele.sql
0
Comment
Question by:KPax
2 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 40525755
The reason is a cartesian product of the "radproj rp1," table which does not have any join conditions.
;)
0
 

Author Closing Comment

by:KPax
ID: 40525794
Yes, you are right. It is very obvious.
I figured it out later, but left open question anyway.
Thank for answer.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now