For the love of dinky the duck whoever wrote that sql snippet needs to be slapped. Seriously...
Anywho i took the however long of my life i wont get back to clean up this sql (by clean i mean pain stakingly make it more readble through indentation).
Im not entirely sure what metric specifically you are looking for (basically what columns) but i've highleted what was changed.
DECLARE @tbl1 TABLE (PageId int
,PageName varchar(50)
,[Month] varchar(20)
,[Total Likes] int
,[New Likes] int
,Unlikes int
,[People Talking About Us] int
,[Total Average Impressions per post] float
,[Total Average Reach per post] float
,[Total Average Organic Reach] float
,[Total Average Viral Reach] float
,[Total Average Paid Reach] float
,[Active Database] float
,[Percent Different] float);
(SELECT
a.PageId,
a.PageName,
DATENAME(MONTH,b.[Date]) as [Month],
ROUND(Convert(float,(Sum(b.DailyPeopleTalkingAboutThis)))/Sum(b.DailyNewLikes),2) as [Active Database],
Sum(b.DailyNewLikes) as [New Likes],
Sum(b.DailyUnlikes ) as [Unlikes],
Sum(b.DailyNewLikes)/ Sum(b.DailyUnlikes) AS [Percent Different]
FROM
tbl_PageMaster as a,
tbl_PageDetails as b
WHERE
a.PageId=b.PageId
AND
a.PageId=@Page
AND
month(b.[Date])=@Month
GROUP BY a.PageId,a.PageName, DATENAME(MONTH,b.[Date])) as t1 ON t.PageId=t1.PageId and t.[Month]=t1.[Month]
Here is the snippet reformatted (As good as its gonna get because i just want to hurt whoever wrote that). Obviously its untested as i do not have access to your database.
alter PROCEDURE [dbo].[GetFacebookGeneralStatistics]
@FDate int
,@TDate int
,@Page int
AS
BEGIN
DECLARE @tbl1 TABLE (PageId int
,PageName varchar(50)
,[Month] varchar(20)
,[Total Likes] int
,[New Likes] int
,Unlikes int
,[People Talking About Us] int
,[Total Average Impressions per post] float
,[Total Average Reach per post] float
,[Total Average Organic Reach] float
,[Total Average Viral Reach] float
,[Total Average Paid Reach] float
,[Active Database] float
,[Percent Different] float);
DECLARE @Month int;
WHILE @FDate<=@TDate
BEGIN
SET @Month=@FDate;
INSERT INTO @tbl1
SELECT
t1.PageId,
t1.PageName,
t1.[Month],
t.[Total Likes],
t1.[New Likes],
t1.Unlikes,
t2.[People Talking About Us],
t3.[Total Average Impressions per post],
t3.[Total Average Reach per post],
t3.[Total Average Organic Reach],
t3.[Total Average Viral Reach],
t3.[Total Average Paid Reach],
t1.[Active Database]
t1.[Percent Different]
FROM (SELECT
a.PageId,
DATENAME(MONTH,b.[Date]) as [Month],
b.LifetimeTotalLikes as [Total Likes]
FROM
tbl_PageMaster as a, tbl_PageDetails as b
WHERE
a.PageId=@Page
AND
Month(b.[Date])=@Month
AND
a.PageId=b.PageId
AND
b.LifetimeTotalLikes <> 0
AND
b.[Date]=(SELECT
MAX(c.[Date])
FROM
tbl_PageDetails as c
WHERE
c.PageId=a.PageId
AND
Month(c.[Date]) = Month(b.[Date])
AND
c.LifetimeTotalLikes<>0 )
) as t
FULL OUTER JOIN
(SELECT
a.PageId,
a.PageName,
DATENAME(MONTH,b.[Date]) as [Month],
ROUND(Convert(float,(Sum(b.DailyPeopleTalkingAboutThis)))/Sum(b.DailyNewLikes),2) as [Active Database],
Sum(b.DailyNewLikes) as [New Likes],
Sum(b.DailyUnlikes ) as [Unlikes],
Sum(b.DailyNewLikes)/ Sum(b.DailyUnlikes) AS [Percent Different]
FROM
tbl_PageMaster as a,
tbl_PageDetails as b
WHERE
a.PageId=b.PageId
AND
a.PageId=@Page
AND
month(b.[Date])=@Month
GROUP BY a.PageId,a.PageName, DATENAME(MONTH,b.[Date])) as t1 ON t.PageId=t1.PageId and t.[Month]=t1.[Month]
FULL OUTER JOIN
(SELECT
a.PageId,
DATENAME(MONTH,a.[Posted]) as [Month],
Sum(isnull(d.likes,0)) + Sum(isnull(d.comments,0)) + Sum(isnull(d.shares,0)) as [People Talking About Us]
FROM
tbl_PostMaster as a,
tbl_Post_TalkingAboutThis as d
WHERE
a.PostMainId = d.PostId
AND
a.PageId=@Page
AND
Month(a.Posted)=@Month
GROUP BY a.PageId,DATENAME(MONTH,a.[Posted])) as t2 on t1.PageId=t2.PageId and t1.[Month]=t2.[Month]
FULL OUTER JOIN
(SELECT
a.PageId,
DATENAME(MONTH,a.[Posted]) as [Month],
round(Convert(float,(Sum(isnull(c.TotalImpressions,0))))/(COUNT(a.PostId)),2) as [Total Average Impressions per post],
round(Convert(float,(Sum(isnull(c.TotalReach,0))))/(COUNT(a.PostId)),2) as [Total Average Reach per post],
round(Convert(float,(Sum(isnull(c.OrganicReach,0))))/(COUNT(a.PostId)),2) as [Total Average Organic Reach],
round(Convert(float,(Sum(isnull(c.ViralReach,0))))/(COUNT(a.PostId)),2) as [Total Average Viral Reach],
round(Convert(float,(Sum(isnull(c.PaidReach,0))))/(COUNT(a.PostId)),2) as [Total Average Paid Reach]
FROM
tbl_PostMaster as a,
tbl_Post_Reach as c
WHERE
a.PostMainId = c.PostId
AND
a.PageId=@Page
AND
Month(a.Posted)=@Month
GROUP BY a.PageId,DATENAME(MONTH,a.[Posted])) as t3 on t1.PageId=t3.PageId and t1.[Month]=t3.[Month]
set @FDate=@FDate+1;
end
select * from @tbl1;
END