Solved

Oracle SQL AVG

Posted on 2014-12-31
2
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
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.
Suggested Courses

615 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