Click here to Skip to main content
15,886,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir


i have two tables

Table X
Sheets	PoNo  invno     Itemid                   
2350	126     3         111

2078	129     3         111




Table Y
Sheets	PoNo  invno     itemid
2300    126     3        111

50      126     3        111

2078	129     3        111 



when i join both tables by pono,invno and itemid and sum table X sheets it shows double sum 6678 . but the total must be 2350+2078=4428
please give the any solution in query
the query is

select sum(pd.sheets) as tot from X pd,Y q where pd.itemid=q.itemid and pd.pono=q.pono and q.invno=p.invno and q.itemcode=518
Posted
Updated 6-Oct-15 1:39am
v3
Comments
vangapally Naveen Kumar 6-Oct-15 7:16am    
Use 'Group by' clause.I think the query is calculating like this 2350+2300+50+2078(it is taking the two records from tableY for invo 3, pono 126 and itemID111).

Refer below sample

SQL
declare @tabX table
(
    Sheets int,
	PoNo int,
	invno int,
	ItemId int
)

declare @tabY table
(
    Sheets int,
	PoNo int,
	invno int,
	ItemId int
)

insert into @tabX
values (2350,126,3, 111),(2078,129,3,111)

insert into @tabY
values(2300,126,3,111),(50,126,3,111),(2078,129,3,111)

select x.Sheets
into #result
from @tabX X,
     @tabY Y
where X.PoNo=y.PoNo
      and x.ItemId=y.ItemId
	  and x.invno=y.invno
Group by x.Sheets

select sum(sheets) from #result
 
Share this answer
 
Comments
rajbir singh thind 6-Oct-15 8:22am    
sir i am using access data base and i want to calculate this one query

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