SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_UserActivityReportRecords_Get]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_UserActivityReportRecords_Get]
GO

CREATE      PROCEDURE [dbo].cs_UserActivityReportRecords_Get
(
	 @nRecordNumberStart INT
    	,@nRecordNumberEnd INT
        ,@BegReportDate DateTime
	,@EndReportDate DateTime
	,@Paged BIT
)
AS
BEGIN
IF @Paged = 1
	BEGIN
		DECLARE @totalRecords INT
	
		--------------------------------------------------------------------
	    	-- Define the table to do the filtering and paging
	    	--------------------------------------------------------------------
	    	DECLARE @tblTempData TABLE
	        (
	        	nID INT IDENTITY
	        	,IP nvarchar(20)
	        	,VisitCount INT
	        )
		INSERT INTO @tblTempData
		(
	        	IP
	        	,VisitCount
	        )
	    	SELECT  IP, SUM(VisitCount) as VisitCount
		FROM     cs_VisitsDaily AS v
		WHERE 
			v.VisitDate between @BegReportDate and @EndReportDate
		GROUP BY
			v.IP
		ORDER BY
			VisitCount DESC
		SET @totalRecords = @@rowcount
	        ---------------------------------------------------------------------------------------------------------------------------------------
		SELECT
	        	IP
	        	,VisitCount
	    	FROM
			@tblTempData 
	    	WHERE
			nID BETWEEN @nRecordNumberStart AND @nRecordNumberEnd
	    	ORDER BY 
	        	nID ASC
	
		-- Return totalRecords
		SELECT @totalRecords
	END
ELSE
	BEGIN
		SELECT  IP as IP_Address, SUM(VisitCount) as Number_Page_Visits
		FROM     cs_VisitsDaily AS v
		WHERE 
			v.VisitDate between @BegReportDate and @EndReportDate
		GROUP BY
			v.IP
		ORDER BY
			Number_Page_Visits DESC
	END
END




GO


SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

grant execute on [dbo].[cs_UserActivityReportRecords_Get] to public
go
