Solved

Oracle SQL AVG

Posted on 2014-12-31
2
173 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

733 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