Need to create 12 month rolling report , user will enter year and Month , need to show report Month (YYYY-MM) on Top from F0911 , which is a detail table in JD Edwards , Amount is in row.
also Year and Month is in Row.
alter proc SP_ActualByAdminRollingAmt
--declare
@frmPN varchar(7) ='2017-08'
as
declare @frmdate datetime =@frmPN+'-01'
declare @TOdate datetime =DATEADD(M,-12, @frmPN+'-01')
--drop table #temp11
--drop table #temp12
--drop table #temp13
select
glaid,
glmcu,
glObj ,glsub,
glsblt,GLSBL,
glan8 ,
glfy,
glpn,
glaa
into #temp11
from mfdb.proddta.f0911 with(nolock)
where glfy in(17,16) and gllt='AA' and left(globj,2) in('60','61')
and gldgj>=[dbo].[JDT_GTJ](@TOdate) and gldgj<=[dbo].[JDT_GTJ](@frmdate)
--JDT_GTJ - function to convert calendar date to Julian date
and glco=1 and glaa<>0
--print [dbo].[JDT_GTJ](@frmdate)
--print [dbo].[JDT_GTJ](@TOdate)
select
glmcu,
mcdl01,
glObj Obj,
GmDL01 Obj_Desc,
ltrim(glsblt+ltrim(GLSBL)) Subledger,
DRDL01 Subledger_Desc,
glan8 VendorNo,
abalph VendorName,
glfy,
glpn,
sum(glaa)/100 glaa,
case when glfy=17 then '2017-' + right('0'+cast(glpn as varchar(2)),2) else '2016-' + right('0'+cast(glpn as varchar(2)),2) end Period into #temp12
from #temp11
left outer join proddta.f0101 on aban8=glan8
left outer join proddta.f0901 on gmaid=glaid
left outer join proddta.f0006 on mcmcu=glmcu
LEFT OUTER JOIN PRODCTL.F0005 ON DRSY='55' AND DRRT='SL' AND LTRIM(DRKY)=LTRIM(GLSBL)
where left(glObj,2) in('60','61')
group by glObj,GmDL01,glsblt+ltrim(GLSBL) ,DRDL01,glan8,abalph,glfy,
glpn,glmcu,mcdl01
----main logic PIVOT
select distinct Period into #temp13 from #temp12
DECLARE @cols AS NVARCHAR(MAX);
-- need to do following as amount filed which are in Row---
--set @cols='[2016-08],[2016-09],[2016-10],[2016-11],[2016-12], [2017-01],[2017-02],[2017-03],-----[2017-04],[2017-05],[2017-06],[2017-07],[2017-08],[2017-09],[2017-10],[2017-11],[2017-12]'
select @cols = STUFF((SELECT distinct ',' +
QUOTENAME(Period)
FROM #temp13
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
DECLARE @query AS NVARCHAR(MAX);
set @query=
'SELECT * FROM(SELECT ltrim(glmcu) BU,mcdl01 BU_Desc,Obj, Obj_Desc, Subledger, Subledger_Desc, VendorNo, VendorName ,glaa,[Period]
FROM #temp12 )t PIVOT (SUM([glaa])
FOR [Period] IN ( ' + @cols + ' )' +
' ) AS pvt
order by 1,3,5'
execute(@query);
also Year and Month is in Row.
alter proc SP_ActualByAdminRollingAmt
--declare
@frmPN varchar(7) ='2017-08'
as
declare @frmdate datetime =@frmPN+'-01'
declare @TOdate datetime =DATEADD(M,-12, @frmPN+'-01')
--drop table #temp11
--drop table #temp12
--drop table #temp13
select
glaid,
glmcu,
glObj ,glsub,
glsblt,GLSBL,
glan8 ,
glfy,
glpn,
glaa
into #temp11
from mfdb.proddta.f0911 with(nolock)
where glfy in(17,16) and gllt='AA' and left(globj,2) in('60','61')
and gldgj>=[dbo].[JDT_GTJ](@TOdate) and gldgj<=[dbo].[JDT_GTJ](@frmdate)
--JDT_GTJ - function to convert calendar date to Julian date
and glco=1 and glaa<>0
--print [dbo].[JDT_GTJ](@frmdate)
--print [dbo].[JDT_GTJ](@TOdate)
select
glmcu,
mcdl01,
glObj Obj,
GmDL01 Obj_Desc,
ltrim(glsblt+ltrim(GLSBL)) Subledger,
DRDL01 Subledger_Desc,
glan8 VendorNo,
abalph VendorName,
glfy,
glpn,
sum(glaa)/100 glaa,
case when glfy=17 then '2017-' + right('0'+cast(glpn as varchar(2)),2) else '2016-' + right('0'+cast(glpn as varchar(2)),2) end Period into #temp12
from #temp11
left outer join proddta.f0101 on aban8=glan8
left outer join proddta.f0901 on gmaid=glaid
left outer join proddta.f0006 on mcmcu=glmcu
LEFT OUTER JOIN PRODCTL.F0005 ON DRSY='55' AND DRRT='SL' AND LTRIM(DRKY)=LTRIM(GLSBL)
where left(glObj,2) in('60','61')
group by glObj,GmDL01,glsblt+ltrim(GLSBL) ,DRDL01,glan8,abalph,glfy,
glpn,glmcu,mcdl01
----main logic PIVOT
select distinct Period into #temp13 from #temp12
DECLARE @cols AS NVARCHAR(MAX);
-- need to do following as amount filed which are in Row---
--set @cols='[2016-08],[2016-09],[2016-10],[2016-11],[2016-12], [2017-01],[2017-02],[2017-03],-----[2017-04],[2017-05],[2017-06],[2017-07],[2017-08],[2017-09],[2017-10],[2017-11],[2017-12]'
select @cols = STUFF((SELECT distinct ',' +
QUOTENAME(Period)
FROM #temp13
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
DECLARE @query AS NVARCHAR(MAX);
set @query=
'SELECT * FROM(SELECT ltrim(glmcu) BU,mcdl01 BU_Desc,Obj, Obj_Desc, Subledger, Subledger_Desc, VendorNo, VendorName ,glaa,[Period]
FROM #temp12 )t PIVOT (SUM([glaa])
FOR [Period] IN ( ' + @cols + ' )' +
' ) AS pvt
order by 1,3,5'
execute(@query);
No comments:
Post a Comment