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: -- Create date: <14-Jan-2011> -- Description: -- ============================================= 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 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 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 END GO