Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My sql query is :
SQL
select s.no,s.name,sum(e.qty),sum(x.qty) from Stock s left join entry e on s.no=e.no left join exit x on s.no=x.no group by s.no,s.name

I have three tables Stock,entry,exit. no column in stock is a primary key and no column in entry and exit is foreign key of stock table's column no.
I getting wrong sum values of e.qty,x.qty column(e.g displaying 600 instead of 200)
Posted
Comments
bhagirathimfs 2-Jul-12 8:08am    
Can you please explain the above problem with some example???
hit91 2-Jul-12 8:16am    
I am getting wrong output
values of these column e.qty and x.qty in output is wrong i.e if the sum of e.qty is 200 ,output is 400.
bhagirathimfs 2-Jul-12 8:19am    
Can you give all the Tables structure?
Means:
All Column's name of all tables.
hit91 2-Jul-12 8:36am    
table -'stock'
columns - no,name and available_stk

table - 'entry'
columns - no,qty

table - 'exit'
columns - no,qty
bhagirathimfs 2-Jul-12 8:42am    
What do you want to do?(Actual Problem,requirment?)

1 solution

This might be because of the join condition you used.
s.no=e.no This might be true for more than one row leading to duplication and hence wrong result.

Example:
Table A
No Name Qty
1  ABC  10
2  XYZ  20
3  QWE  30


Table B
No Name Qty
2  XYZ  40
5  DFG  30
2  XYZ  40


Table A Left Join Table B:
A.No A.Name A.Qty B.Qty
1    ABC     10    NULL
2    XYZ     20    40
2    XYZ     20    40
3    QWE     30    NULL


If you see, above XYZ is getting duplicated based on the join. This leads to wrong sum number.

Refer: W3School LEFT Join[^]
 
Share this answer
 
Comments
Lokesh Zende 3-Jul-12 4:33am    
quiet right guess sandip. my 5

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