Hi @Analyst_SQL ,
Is the following output the result you expect:
with cte
as(select '' codeitem,'' item_Name,'' Balance_QTY,'' Balance_Weight,'B_Qty' [2020-03-06],'B_Weight' [2020-03-061],'D_QTY' [2020-03-062] ,'D_Weight' [2020-03-063] ,'B_Qty' [2020-04-061],'B_Weight' [2020-04-062],'D_QTY' [2020-04-063] ,'D_Weight' [2020-04-064] ,'B_Qty' [2020-05-06],'B_Weight' [2020-05-061],'D_QTY' [2020-05-062] ,'D_Weight' [2020-05-063] ,'B_Qty' [2020-06-061],'B_Weight' [2020-06-062],'D_QTY' [2020-06-063] ,'D_Weight' [2020-06-064]
UNION ALL
SELECT cast(codeitem as varchar(10)) codeitem,item_Name,Balance_QTY,Balance_Weight,cast(ISNULL([2020-03-06 B_QTY],0) as varchar(10)) [2020-03-06 B_QTY],cast(ISNULL([2020-03-06 B_Weight],0) as varchar(10)) [2020-03-06 B_Weight],cast(ISNULL([2020-03-06 D_QTY],0) as varchar(10)) [2020-03-06 D_QTY],cast(ISNULL([2020-03-06 D_Weight],0) as varchar(10)) [2020-03-06 D_Weight],cast(ISNULL([2020-04-06 B_QTY],0) as varchar(10)) [2020-04-06 B_QTY],cast(ISNULL([2020-04-06 B_Weight],0) as varchar(10)) [2020-04-06 B_Weight],cast(ISNULL([2020-04-06 D_QTY],0) as varchar(10)) [2020-04-06 D_QTY],cast(ISNULL([2020-04-06 D_Weight],0) as varchar(10)) [2020-04-06 D_Weight],cast(ISNULL([2020-05-06 B_QTY],0) as varchar(10)) [2020-05-06 B_QTY],cast(ISNULL([2020-05-06 B_Weight],0) as varchar(10)) [2020-05-06 B_Weight],cast(ISNULL([2020-05-06 D_QTY],0) as varchar(10)) [2020-05-06 D_QTY],cast(ISNULL([2020-05-06 D_Weight],0) as varchar(10)) [2020-05-06 D_Weight],cast(ISNULL([2020-06-06 B_QTY],0) as varchar(10)) [2020-06-06 B_QTY],cast(ISNULL([2020-06-06 B_Weight],0) as varchar(10)) [2020-06-06 B_Weight],cast(ISNULL([2020-06-06 D_QTY],0) as varchar(10)) [2020-06-06 D_QTY],cast(ISNULL([2020-06-06 D_Weight],0) as varchar(10)) [2020-06-06 D_Weight]
FROM (
SELECT codeitem,item_Name,Balance_QTY,Balance_Weight, CAST(B_Date AS VARCHAR) + ' '+ITEM AS Name, VALUE
FROM (
select * from #t
)s
UNPIVOT
(VALUE FOR Item IN ([B_QTY], [B_Weight], [D_QTY],[D_Weight])) p
) src
PIVOT
(
MAX(VALUE) FOR Name IN ([2020-03-06 B_QTY],[2020-03-06 B_Weight],[2020-03-06 D_QTY],[2020-03-06 D_Weight],[2020-04-06 B_QTY],[2020-04-06 B_Weight],[2020-04-06 D_QTY],[2020-04-06 D_Weight],[2020-05-06 B_QTY],[2020-05-06 B_Weight],[2020-05-06 D_QTY],[2020-05-06 D_Weight],[2020-06-06 B_QTY],[2020-06-06 B_Weight],[2020-06-06 D_QTY],[2020-06-06 D_Weight])
) pvt)
,cte2 as
(
select f.CodeItem,f.item_Name,(f.Bigbale_QTY-f.Dispatch_QTY) as Balance_Qty,(f.Bigbale_weight-f.Dispatch_Weight) as Balance_Weight from (
select e.CodeItem,e.item_Name,isnull(min(e.[Bigbale_QTY]),0) as [Bigbale_QTY],isnull(min(e.[Bigbale_weight]),0) as [Bigbale_weight],isnull(sum(c.QTY),0) as [Dispatch_QTY],
isnull(sum(c.Bweight),0) as [Dispatch_Weight] from (
select upper(a.Descriptionitem) item_Name,(a.CodeItem) CodeItem,isnull(sum(b.Bpqty),0) as [Bigbale_QTY],isnull(sum(b.Bweight),0) as [Bigbale_Weight]
from #ItemMasterFile a
--inner join Catagory ca on ca.CID=a.CID
left join #Bigbalprd b on a.CodeItem=b.CodeItem
where a.Packsize ='bigbale' and b.delID is null
group by a.Descriptionitem,a.CodeItem) e
left join #Dispatch_BD c on e.CodeItem=c.CodeItem
where c.Delidd is null
group by e.item_Name,e.CodeItem
)f)
select c1.codeitem,c1.item_Name,c2.Balance_QTY,c2.Balance_Weight,c1.[2020-03-06],c1.[2020-03-061],c1.[2020-03-062] ,c1.[2020-03-063] ,c1.[2020-04-061],c1.[2020-04-062],c1.[2020-04-063] ,c1.[2020-04-064] ,c1.[2020-05-06],c1.[2020-05-061],c1.[2020-05-062] ,c1.[2020-05-063] ,c1.[2020-06-061],c1.[2020-06-062],c1.[2020-06-063] ,c1.[2020-06-064]
from cte c1
left join cte2 c2
on c1.CodeItem=c2.CodeItem