USE [linaprod]
GO
/****** Object: UserDefinedFunction [dbo].[func_Performance_Report] Script Date: 4/20/2017 8:11:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[func_Performance_Report]
(
@ProductID INT,
@Date NVARCHAR(20)--,
--@GroupID INT
)
RETURNS TABLE
AS
RETURN
(
SELECT
REPLACE(@Date, '%', '') [Date],
@ProductID [ProductID],
[T1].[TOTAL_REFS],
[T2].[SELF_REFS_COUNT],
[T3].[WPP_SHARES_COUNT],
[T4].[WPP_VIEWS_COUNT],
[T5].[SIGNUPS_COUNT],
[T6].[SALES_COUNT]
FROM
(
SELECT
COUNT([R].[ID]) AS 'TOTAL_REFS'
FROM
REFERRALS [R]
INNER JOIN [PRESENTERS] [P] ON
[P].[ID] = [R].[PRESENTERID]
WHERE
[R].[INSERTDT] LIKE @Date
AND [R].[PRODUCTID] = @ProductID
--AND [P].[GROUPID] = @GroupID
) AS T1,
(
SELECT
COUNT([R].[ID]) AS 'SELF_REFS_COUNT'
FROM
REFERRALS [R]
INNER JOIN [PRESENTERS] [P] ON
[P].[PHONE] = [R].[PHONE]
AND [P].[ID] = [R].[PRESENTERID]
WHERE
[R].[INSERTDT] LIKE @Date
AND [R].[PRODUCTID] = @ProductID
--AND [P].[GROUPID] = @GroupID
) AS T2,
(
SELECT
COUNT([SL].[ID]) AS 'WPP_SHARES_COUNT'
FROM
[SHARELOG] [SL]
INNER JOIN [PRESENTERS] [P] ON
[P].[ID] = [SL].[PRESENTERID]
WHERE
[SL].[INSERTDT] LIKE @Date
AND [SL].[PRODUCTID] = @ProductID
--AND [P].[GROUPID] = @GroupID
) AS T3,
(
SELECT
COUNT([VL].[ID]) AS 'WPP_VIEWS_COUNT'
FROM
[VIEWLOG] [VL]
INNER JOIN [PRESENTERS] [P] ON
[P].[ID] = [VL].[PRESENTERID]
WHERE
[VL].[INSERTDT] LIKE @Date
AND [VL].[PRODUCTID] = @ProductID
--AND [P].[GROUPID] = @GroupID
) AS T4,
(
SELECT
COUNT([P].[ID]) AS 'SIGNUPS_COUNT'
FROM
[PRESENTERS] [P]
WHERE
[P].[INSERTDT] LIKE @Date
--AND [P].[GROUPID] = @GroupID
) AS T5,
(
SELECT
COUNT([R].[ID]) AS 'SALES_COUNT'
FROM
[REFERRALS] [R]
INNER JOIN [PRESENTERS] [P] ON
[P].[ID] = [R].[PRESENTERID]
WHERE
[R].[ProcessStatusID] = 4
AND [R].[INSERTDT] LIKE @Date
AND [R].[PRODUCTID] = @ProductID
--AND [P].[GROUPID] = @GroupID
) AS T6
)