Hi
I want if value in Pivot is null then it should be displayed as 0. I also want to display Totals.
SQL
CREATE TABLE #temp(Invoice int,
Quantity numeric(19,6),itemName nvarchar(50))
insert into #temp
select Invoice,
IsNull(sum(T0.Qty),0) 'Quantity', T2.ItemName
From DR1 T0
inner join ORD T1 on T0.doc = T1.doc
inner join Oit T2 on T0.Item = T2.Item
where T1.DDATE = '2022/04/20'
GROUP BY T1.Invoice,T2.ItemName
declare @cols as nvarchar(max)='';
declare @query as nvarchar(max)='';
select @cols = @cols + QUOTENAME(ItemName) + ',' from (Select distinct ItemName from #temp) as tmp
select @cols = substring(@cols,0,len(@cols))
set @query = 'select Invoice, ' + @cols + ' from (select Invoice,Quantity,itemname from #temp) x pivot
( Sum(quantity) for itemname in (' + @cols + ')) piv ';
execute (@query)Thanks

Jignesh KumarRank #91 · 22.9K reputationPosted May 2, 2022, 1:31 AM EDT
Ramco RamcoRank #462 · 5.4K reputationPosted May 2, 2022, 2:05 AM EDT
Jignesh KumarRank #91 · 22.9K reputationPosted May 2, 2022, 2:26 AM EDT
Ramco RamcoRank #462 · 5.4K reputationPosted May 2, 2022, 2:59 AM EDT
Amit MohantyRank #112 · 20K reputationPosted May 2, 2022, 3:00 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted May 2, 2022, 5:18 AM EDT