Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is my query, but my problem is I can't get the result which I'm expecting in below mentioned two outputs. I need two queries to get the result like below.

Query:
select t2.VoucherNumber as Invoice_Number, i.ItemName as Item_Name, t2.Aqty as Qty
from Item i, Party p, Tran1 t1, VTran2 t2, Cases c, VoucherTypes VT
Where t1.LorryNo='22-8205' and t1.VoucherType=VT.VoucherType and VT.ActualVoucherType='sales'
and t1.VoucherNumber=t2.VoucherNumber and t2.ItemName=i.Itemname and
t2.ItemName=c.ItemName and t1.LoadingStatus='2'
GROUP BY t2.VoucherNumber, i.ItemName, t2.Aqty


Result (This is wrong):
Invoice_Number Item_Name   Qty
001               A       36.00
001               B       10.00
001               B       60.00
002               A       100.00


Expect Result from first query (This is I need, sum qty by same itemName in same invoice no):
Invoice_Number Item_Name   Qty
      001               A       36.00
      001               B       70.00
      002               A       100.00


Expect Result from second query (This is another one, sum qty same item name):
Item_Name   Qty
      A     136.00
      B     70.00
Posted

SQL
select t2.VoucherNumber as Invoice_Number, i.ItemName as Item_Name, Sum(t2.Aqty) as Qty
from Item i, Party p, Tran1 t1, VTran2 t2, Cases c, VoucherTypes VT
Where t1.LorryNo='22-8205' and t1.VoucherType=VT.VoucherType and VT.ActualVoucherType='sales'
and t1.VoucherNumber=t2.VoucherNumber and t2.ItemName=i.Itemname and
t2.ItemName=c.ItemName and t1.LoadingStatus='2'
GROUP BY t2.VoucherNumber, i.ItemName

and
SQL
select  i.ItemName as Item_Name, Sum(t2.Aqty) as Qty
from Item i, Party p, Tran1 t1, VTran2 t2, Cases c, VoucherTypes VT
Where t1.LorryNo='22-8205' and t1.VoucherType=VT.VoucherType and VT.ActualVoucherType='sales'
and t1.VoucherNumber=t2.VoucherNumber and t2.ItemName=i.Itemname and
t2.ItemName=c.ItemName and t1.LoadingStatus='2'
GROUP BY i.ItemName

few references :
Get Ready to Learn SQL Server: 6 – Group and Summarize Your Results[^]
http://www.w3schools.com/sql/sql_groupby.asp[^]
http://www.w3schools.com/sql/sql_func_sum.asp[^]
 
Share this answer
 
v2
Comments
Maciej Los 16-Dec-14 11:56am    
+5!
First query:
SQL
SELECT Invoice_Number, Item_Name, SUM(Qty) AS Qty
FROM <Table_list>
GROUP BY Invoice_Number, Item_Name

Second qurey:
SQL
SELECT Item_Name, SUM(Qty) AS Qty
FROM <Table_list>
GROUP BY Item_Name


For further information, please see:
SUM[^]
GROUP BY[^]
 
Share this answer
 
Comments
DamithSL 16-Dec-14 11:53am    
nice answer from God’s Gift :)
5wd!
Maciej Los 16-Dec-14 11:56am    
Thank you, Damith ;)

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