Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle SQL AVG

Posted on 2014-12-31
2
Medium Priority
?
177 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

721 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