Hi @Analyst_SQL ,
2019:
Declare @startDate date ='03-03-2019'
Declare @Enddate date ='09-10-2019'
;with cte as
(select Descriptionitem,format(EntryDate,'yyyy-MM') EntryDate ,a.Bpqty
FROM #Bigbalprd a left JOIN #ItemMasterFile b
on a.Codeitem=b.Codeitem)
--where a.DelID is null and a.EntryDate BETWEEN @startDate AND @Enddate )
,cte2 as
(select * from cte t PIVOT (Sum(Bpqty) for EntryDate in ([2019-01],[2019-02],[2019-03],[2019-04],[2019-05],[2019-06],[2019-07],[2019-08],[2019-09],[2019-10],[2019-11],[2019-12])) AS stat)
,cte3 as
(select * ,isnull([2019-01],0)+isnull([2019-02],0)+isnull([2019-03],0)
+isnull([2019-04],0)+isnull([2019-05],0)+isnull([2019-06],0)
+isnull([2019-07],0)+isnull([2019-08],0)+isnull([2019-09],0)
+isnull([2019-10],0)+isnull([2019-11],0)+isnull([2019-12],0) Total
from cte2)
select * from cte3
union all
select 'Total',sum([2019-01]),sum([2019-02]),
sum([2019-03]),sum([2019-04]),
sum([2019-05]),sum([2019-06]),
sum([2019-07]),sum([2019-08]),
sum([2019-09]),sum([2019-10]),
sum([2019-11]),sum([2019-12]),sum(Total)
from cte3
The above code works for me.
If you want to display the annual data in a table, please refer to:
Create table #ItemMasterFile (Codeitem int,Descriptionitem varchar(50));
Create table #Bigbalprd (DelID int,Codeitem int,Bpqty int,B_Weight int,EntryDate date);
INSERT INTO #ItemMasterFile VALUES(1,'A'),(2,'B'),(3,'C'),(4,'D'),(5,'e'),
(6,'f'),(7,'g'),(8,'h'),(9,'K'),(10,'L'),(11,'M');
INSERT INTO #Bigbalprd VALUES
(111,1,1,500,'2019-03-06')
,(112,2,1,200,'2019-03-06')
,(113,1,1,300,'2019-03-06')
,(114,6,1,100,'2019-04-07')
,(115,1,1,200,'2019-04-07')
,(116,1,1,300,'2019-08-15')
,(117,7,1,100,'2019-08-20')
,(118,5,1,200,'2019-05-09')
,(119,8,1,300,'2019-06-09');
INSERT INTO #Bigbalprd
VALUES ( 111, 1, 1, 500, '2020-03-01' ) ,
( 112, 2, 1, 200, '2020-03-06' ) ,
( 113, 1, 1, 300, '2020-03-06' ) ,
( 114, 6, 1, 100, '2020-04-07' ) ,
( 115, 1, 1, 200, '2020-04-07' ) ,
( 116, 1, 1, 300, '2020-08-15' ) ,
( 117, 7, 1, 100, '2020-08-20' ) ,
( 118, 5, 1, 200, '2020-05-09' ) ,
( 119, 8, 1, 300, '2020-06-09' );
DECLARE @startDate DATE = '2019-03-01';
DECLARE @Enddate DATE = '2020-10-09';
;WITH cte
AS ( SELECT I.Descriptionitem ,
YEAR(B.EntryDate) AS YNumber ,
MONTH(B.EntryDate) AS MNumber ,
B.Bpqty
FROM #Bigbalprd B
JOIN #ItemMasterFile I ON I.Codeitem = B.Codeitem
WHERE B.EntryDate BETWEEN @startDate AND @Enddate ),
cte2 AS
(SELECT * FROM cte t PIVOT (SUM(Bpqty)
FOR MNumber IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )) P )
select * from cte2 where YNumber=2019
union all
select 'Total','2019',sum([1]),sum([2]),sum([3]),sum([4]),sum([5]),sum([6]),sum([7]),sum([8]),sum([9]),sum([10]),
sum([11]),sum([12]) from cte2 where YNumber=2019
union all
select * from cte2 where YNumber=2020
union all
select 'Total','2020',sum([1]),sum([2]),sum([3]),sum([4]),sum([5]),sum([6]),sum([7]),sum([8]),sum([9]),sum([10]),
sum([11]),sum([12]) from cte2 where YNumber=2020
drop table #Bigbalprd
drop table #ItemMasterFile
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.