dbo.GetSiteLicenseUsageReport.StoredProcedure.sql
7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
USE [AIADatabaseV5]
GO
/****** Object: StoredProcedure [dbo].[GetSiteLicenseUsageReport] Script Date: 2/1/2018 12:15:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Magic Software>
-- Create date: <14-Jan-2011>
-- Description: <To get the list of Site License Usage Report>
-- =============================================
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetSiteLicenseUsageReport]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetSiteLicenseUsageReport]
GO
CREATE PROCEDURE [dbo].[GetSiteLicenseUsageReport]
-- Add the parameters for the stored procedure here
@sFromDate varchar(20), @sToDate varchar(20), @sAccoutNumber varchar(50)='', @iEditionId tinyint = 0,
@pageNo int, @pageLength int, @recordCount int out
AS
BEGIN
IF 1=0 BEGIN
SET FMTONLY OFF
END
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON
DECLARE @dtFromDate DATETIME
DECLARE @dtToDate DATETIME
-- convert the datatype of fromdate & todate parameter to datetime
SELECT @dtFromDate = CONVERT(DATETIME,@sFromDate)
SELECT @dtToDate = DATEADD(ms,-3,DATEADD(DAY,1,CONVERT(DATETIME,@sToDate)))
Select RowNum,AccountNumber, EditionTitle, ReferalUrl, InstitutionName, LicenseCreationDate,TotalLogins,LastLogin
from (
SELECT ROW_NUMBER() OVER (ORDER BY UserLoginLog.AccountNumber) AS RowNum , UserLoginLog.AccountNumber, Edition.Title AS EditionTitle, UserLoginLog.ReferalUrl,
(SELECT License.InstitutionName FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as InstitutionName,
(SELECT CONVERT(VARCHAR,License.CreationDate,101) FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as LicenseCreationDate,
COUNT(DISTINCT UserLoginLog.LogDate) AS TotalLogins,
CONVERT(VARCHAR,MAX(UserLoginLog.LogDate),101) AS LastLogin FROM
UserLoginLog INNER JOIN Edition ON UserLoginLog.Edition = CAST(Edition.Id AS NVARCHAR)
WHERE UserLoginLog.FailureId IS NULL
AND UserLoginLog.LogDate BETWEEN @dtFromDate AND @dtToDate
AND UserLoginLog.AccountNumber = (CASE WHEN LEN(@sAccoutNumber) > 0 THEN @sAccoutNumber ELSE UserLoginLog.AccountNumber END)
AND Edition.IsActive = 1
AND Edition.Id = (CASE WHEN @iEditionId > 0 THEN @iEditionId ELSE Edition.Id END)
GROUP BY UserLoginLog.AccountNumber, Edition.Title, UserLoginLog.ReferalUrl) as usr
WHERE RowNum > @pageLength * (@pageNo - 1) AND RowNum <= @pageLength * @pageNo order by AccountNumber
--Calculate total number of records
select @recordCount = count(ResultTable.AccountNumber) from (
SELECT UserLoginLog.AccountNumber, Edition.Title AS EditionTitle, UserLoginLog.ReferalUrl,
(SELECT License.InstitutionName FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as InstitutionName,
(SELECT CONVERT(VARCHAR,License.CreationDate,101) FROM License WHERE License.AccountNumber = UserLoginLog.AccountNumber) as LicenseCreationDate,
COUNT(DISTINCT UserLoginLog.LogDate) AS TotalLogins,
CONVERT(VARCHAR,MAX(UserLoginLog.LogDate),101) AS LastLogin FROM
UserLoginLog INNER JOIN Edition ON UserLoginLog.Edition = CAST(Edition.Id AS NVARCHAR)
WHERE UserLoginLog.FailureId IS NULL
AND UserLoginLog.LogDate BETWEEN @dtFromDate AND @dtToDate
AND UserLoginLog.AccountNumber = (CASE WHEN LEN(@sAccoutNumber) > 0 THEN @sAccoutNumber ELSE UserLoginLog.AccountNumber END)
AND Edition.IsActive = 1
AND Edition.Id = (CASE WHEN @iEditionId > 0 THEN @iEditionId ELSE Edition.Id END)
GROUP BY UserLoginLog.AccountNumber, Edition.Title, UserLoginLog.ReferalUrl) as ResultTable;
END
GO