ALTER PROCEDURE [dbo].[GetEggData]
@BGName NVARCHAR(255),
@BUKey NVARCHAR(255),
@Category NVARCHAR(255),
@UserName NVARCHAR(255),
@Level INT,
@StartDate INT,
@EndDate INT
AS
BEGIN
SET NOCOUNT ON;
SET @BUKey = REPLACE(@BUKey,'''','')
SET @Category = REPLACE(@Category,',',''',''')
SET @UserName = REPLACE(@UserName,',',''',''')
SELECT * INTO #111 FROM (
select 'Revenue'Category,d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey,sum(a.[NetRev])egg
from [dbo].[FactRevenue] a
join [dbo].[DimBU] b on a.BUKey=b.BUKey
join [dbo].[DimMonth] d on a.MonthKey=d.MonthKey
join [dbo].[UserPolicy] e on (b.BGName=e.BUOrBG and e.PolicyLevel=1) or (b.BUName=e.BUOrBG and e.PolicyLevel=2)
where d.MonthKey>=@StartDate
and d.MonthKey<=@EndDate
group by d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey)T1
SELECT * INTO #222 FROM (
select 'Revenue'Category,d.[MonthName]+N'%' as [MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey,''Pegg
from [dbo].[FactRevenue] a
join [dbo].[DimBU] b on a.BUKey=b.BUKey
join [dbo].[DimMonth] d on a.MonthKey=d.MonthKey
join [dbo].[UserPolicy] e on (b.BGName=e.BUOrBG and e.PolicyLevel=1) or (b.BUName=e.BUOrBG and e.PolicyLevel=2)
where d.MonthKey>=@StartDate
and d.MonthKey<=@EndDate
group by d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey)T1
DECLARE @MainSqlM varchar(2000);
DECLARE @MainSqlM1 varchar(2000);
DECLARE @ColumnSqlM1 varchar(2000);
SET @ColumnSqlM1 =
STUFF(
(
SELECT ',' +'['+ #111.[MonthName] +']'
FROM #111
GROUP BY #111.[MonthName],#111.MonthKey
order by #111.MonthKey
FOR XML PATH('')
), 1, 1, '');
select a.Category,a.BGName,a.BUName,a.BUKey,a.[MonthName],a.egg into #m111 from #111 a
SET @MainSqlM1 =
'SELECT distinct Category,BGName,BUName,BUKey,'+@ColumnSqlM1 +' FROM #m111
join [dbo].[UserPolicy] e on (#m111.BGName=e.BUOrBG and e.PolicyLevel=1) or (#m111.BUName=e.BUOrBG and e.PolicyLevel=2)'
+ ' PIVOT( sum(egg) FOR [MonthName] IN ( '
+ @ColumnSqlM1
+ ' ) ) tmp where Category in ('''+@Category+''') and BUKey in ('+@BUKey+') and username in ('''+@UserName+''')'
DECLARE @MainSqlM2 varchar(2000);
DECLARE @ColumnSqlM2 varchar(2000);
SET @ColumnSqlM2 =
STUFF(
(
SELECT ',' +'['+ #222.[MonthName] +']'
FROM #222
GROUP BY #222.[MonthName],#222.MonthKey
order by #222.MonthKey
FOR XML PATH('')
), 1, 1, '');
select a.Category,a.BGName,a.BUName,a.BUKey,a.[MonthName],a.Pegg into #m222 from #222 a
SET @MainSqlM2 =
'SELECT distinct Category a,BGName b ,BUName c,BUKey,'+@ColumnSqlM2 +' FROM #m222
join [dbo].[UserPolicy] e on (#m222.BGName=e.BUOrBG and e.PolicyLevel=1) or (#m222.BUName=e.BUOrBG and e.PolicyLevel=2)'
+ ' PIVOT( sum(Pegg) FOR [MonthName] IN ('
+ @ColumnSqlM2
+ ' ) ) tmp where Category in ('''+@Category+''') and BUKey in ('+@BUKey+') and username in ('''+@UserName+''')';
SET @MainSqlM=
'select a.*,b.* from ('+@MainSqlM1+')a left join ('+@MainSqlM2+')b on a.Category=b.a and a.BGName=b.b and a.BUName=b.c'
EXECUTE ( @MainSqlM );
drop table #111;
drop table #222;
drop table #m111;
drop table #m222;
END
@BGName NVARCHAR(255),
@BUKey NVARCHAR(255),
@Category NVARCHAR(255),
@UserName NVARCHAR(255),
@Level INT,
@StartDate INT,
@EndDate INT
AS
BEGIN
SET NOCOUNT ON;
SET @BUKey = REPLACE(@BUKey,'''','')
SET @Category = REPLACE(@Category,',',''',''')
SET @UserName = REPLACE(@UserName,',',''',''')
SELECT * INTO #111 FROM (
select 'Revenue'Category,d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey,sum(a.[NetRev])egg
from [dbo].[FactRevenue] a
join [dbo].[DimBU] b on a.BUKey=b.BUKey
join [dbo].[DimMonth] d on a.MonthKey=d.MonthKey
join [dbo].[UserPolicy] e on (b.BGName=e.BUOrBG and e.PolicyLevel=1) or (b.BUName=e.BUOrBG and e.PolicyLevel=2)
where d.MonthKey>=@StartDate
and d.MonthKey<=@EndDate
group by d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey)T1
SELECT * INTO #222 FROM (
select 'Revenue'Category,d.[MonthName]+N'%' as [MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey,''Pegg
from [dbo].[FactRevenue] a
join [dbo].[DimBU] b on a.BUKey=b.BUKey
join [dbo].[DimMonth] d on a.MonthKey=d.MonthKey
join [dbo].[UserPolicy] e on (b.BGName=e.BUOrBG and e.PolicyLevel=1) or (b.BUName=e.BUOrBG and e.PolicyLevel=2)
where d.MonthKey>=@StartDate
and d.MonthKey<=@EndDate
group by d.[MonthName],d.MonthKey,b.BGName,b.BUName,b.BUKey)T1
DECLARE @MainSqlM varchar(2000);
DECLARE @MainSqlM1 varchar(2000);
DECLARE @ColumnSqlM1 varchar(2000);
SET @ColumnSqlM1 =
STUFF(
(
SELECT ',' +'['+ #111.[MonthName] +']'
FROM #111
GROUP BY #111.[MonthName],#111.MonthKey
order by #111.MonthKey
FOR XML PATH('')
), 1, 1, '');
select a.Category,a.BGName,a.BUName,a.BUKey,a.[MonthName],a.egg into #m111 from #111 a
SET @MainSqlM1 =
'SELECT distinct Category,BGName,BUName,BUKey,'+@ColumnSqlM1 +' FROM #m111
join [dbo].[UserPolicy] e on (#m111.BGName=e.BUOrBG and e.PolicyLevel=1) or (#m111.BUName=e.BUOrBG and e.PolicyLevel=2)'
+ ' PIVOT( sum(egg) FOR [MonthName] IN ( '
+ @ColumnSqlM1
+ ' ) ) tmp where Category in ('''+@Category+''') and BUKey in ('+@BUKey+') and username in ('''+@UserName+''')'
DECLARE @MainSqlM2 varchar(2000);
DECLARE @ColumnSqlM2 varchar(2000);
SET @ColumnSqlM2 =
STUFF(
(
SELECT ',' +'['+ #222.[MonthName] +']'
FROM #222
GROUP BY #222.[MonthName],#222.MonthKey
order by #222.MonthKey
FOR XML PATH('')
), 1, 1, '');
select a.Category,a.BGName,a.BUName,a.BUKey,a.[MonthName],a.Pegg into #m222 from #222 a
SET @MainSqlM2 =
'SELECT distinct Category a,BGName b ,BUName c,BUKey,'+@ColumnSqlM2 +' FROM #m222
join [dbo].[UserPolicy] e on (#m222.BGName=e.BUOrBG and e.PolicyLevel=1) or (#m222.BUName=e.BUOrBG and e.PolicyLevel=2)'
+ ' PIVOT( sum(Pegg) FOR [MonthName] IN ('
+ @ColumnSqlM2
+ ' ) ) tmp where Category in ('''+@Category+''') and BUKey in ('+@BUKey+') and username in ('''+@UserName+''')';
SET @MainSqlM=
'select a.*,b.* from ('+@MainSqlM1+')a left join ('+@MainSqlM2+')b on a.Category=b.a and a.BGName=b.b and a.BUName=b.c'
EXECUTE ( @MainSqlM );
drop table #111;
drop table #222;
drop table #m111;
drop table #m222;
END