i want to display dispatch qty item wise with balance
below is my data and desire output
Create table #item (ItemCode int,ItemName varchar(max))
create table #bigbale(ID int,ItemCode int,QTY int)
Create table #Distpatch(ID int,Itemcode int,DisQTY int,A_Date datetime)
insert into #bigbale values
(1001, 'A'),
(1002, 'B'),
(1003, 'C'),
(1004, 'D'),
(1005, 'E'),
(1006, 'F'),
(1007, 'G')
insert into #bigbale values
(1,1001, 8),
(2,1002, 5),
(3,1003, 2),
(4,1004, 2),
(5,1005, 3),
(6,1006, 23),
(7,1007, 7)
insert into #Distpatch values
(1,1001,2, '2019-05-28 17:18'),
(2,1002,3 ,'2018-05-28 17:18'),
(3,1003,1 ,'2018-05-28 17:18'),
(4,1006,2, '2018-05-28 17:18'),
(5,1001,2, '2019-05-28 17:18')
Output
Name Dispatch_QTY Balance
A 4 4
B 3 2
C 2 0
D 0 2