Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL> select * from employee;

NAME       EADD              EID     SALARY
---------- ---------- ---------- ----------
himanshu   sre                 1       4000
archana    jammu               2       3000
sanjana    delhi               3       2500
pooja      gng                 4       2600
bhavdeep   ynr                 5       1600

SQL> select * from project;

PNAME      DESCRIPTIO        PID       COST
---------- ---------- ---------- ----------
management manageall           1       1000
sap        spadmsn            30        300
javaadsn   adsn               40        400
.net       adsnnet            40        400


SQL> select sum(e.salary) , sum(p.cost) from employee e , project p;

SUM(E.SALARY) SUM(P.COST)
------------- -----------
        54800       10500
Posted
Comments
Sergey Alexandrovich Kryukov 2-May-12 14:16pm    
I don't know, but you should be charged for the difference... :-)
--SA
OriginalGriff 2-May-12 14:32pm    
Answer updated

The reason is that the statement
SQL
select sum(e.salary) , sum(p.cost) from employee e , project p;

makes a Full Outer Join, due to which the rows in table employee are repeated 4 times each, hence the Sum(e.salary) = 4 * 13700 = 54800 and the rows in table project are repeated 5 times each so that the Sum(p.cost) = 5 * 2100 = 10500.
 
Share this answer
 
Comments
Maciej Los 2-May-12 14:31pm    
Good answer, my 5!
VJ Reddy 2-May-12 14:38pm    
Thank you, losmac.
Yes.
10500 / 5 = 2300
1000 + 300 + 400 + 400 = 2300.
There are five records in the first table.

54800 / 4 = 13700
4000 + 3000 + 2500 + 2600 + 1600 = 13700
There are four records in the second table.

I'm not sure what the query should be, but that is why you are getting the result you are.

Try this:
SQL
select sum(salary) , (SELECT sum(cost) from  project) FROM employee
 
Share this answer
 
v2
Comments
Maciej Los 2-May-12 14:32pm    
Good answer, my 5!
VJ Reddy 2-May-12 14:36pm    
Good answer. 5!
nikhil_kumar 2-May-12 15:06pm    
thnx for help but when i try this following error occur.....
SQL> select sum(salary),(select sum(cost) from project) from employee ;
select sum(salary),(select sum(cost) from project) from employee
*
ERROR at line 1:
ORA-00937: not a single-group group function
OriginalGriff 2-May-12 15:25pm    
Why are you doing it twice?
nikhil_kumar 3-May-12 1:51am    
bcus i want to calculate sum by using only one query.....
You can do it in different way too, defining column-headers and row-headres and using UNION[^] command:
SQL
SELECT 'Salary' AS [Item], SUM([Salary]) AS [Sum]
FROM employee
UNION ALL
SELECT 'Cost' AS [Item], SUM([Cost]) AS [Sum]
FROM project


Results:

ItemSum
Salary13700
Cost2300
 
Share this answer
 
v2
Comments
VJ Reddy 4-May-12 12:47pm    
Good alternative. 5!
Maciej Los 4-May-12 13:24pm    
Thank you, VJ ;)
It is doing a Cartesian product of the tables so each column of table 1 is multiplied by the column of table 2.
If you are using a outer join then there should be one primary key and foreign key relation which in your case is not there.
you can only try 2 different queries separately for your result.
Select sum (salary) from employee;
Select sum(cost) from project ;
 
Share this answer
 
Comments
nikhil_kumar 3-May-12 1:52am    
thnx Muhammmad Faraz but is there any another way to get the sum by using only one statement?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900