Search code examples
sqlexcel-formulaexcel-2010sql-query-store

How to convert/simulate Excel formula into SQL Query


I pulled out some data from SQL db to Excel using the below sql query and then in excel I have added two additional columns with their respective formulas as below for analysis. Now I am trying to add these excel formulas into my sql query itself as 2 columns where I am having trouble could you please suggest me how I can add the above two formulas into my sql query.

Many thanks in advance.

  1. Older than 2 months Formula = IF(LastAccessDate>TODAY()-(365/6),"","Older than 2 months")
  2. Duration check Formula =IF(LastAccessDate-FirstAccessedDate=0,"Never Logged On",LastAccessDate-FirstAccessedDate)

Sql Query:

SELECT s.DomainName as UserId
,s.fullname as FullName
,MIN(DATEADD(HH,DATEDIFF(HH,GetUTCDate(),GetDate())), A.CreatedOn)) [FirstAccessAt]
,MAX(DATEADD(HH,(DATEDIFF(HH,GetUTCDate(),GetDate())),A.CreatedOn)) [LastAccessAt]

--Tried on my own
    --,DATEPART(MM,DATEADD(HH,(DATEDIFF(HH,GetUTCDate(),GetDate())), A.CreatedOn))AS [Month]
    --,(MAX(DATEADD(HH,(DATEDIFF(HH,GetUTCDate(),GetDate())), A.CreatedOn))> -6, GETDATE())  [OlderThan6Months]

    FROM archive a
    INNER JOIN Systemuser s
    ON s.systemuserid = a.objectid
    WHERE a.action = 54
    and a.CreatedOn between '2015-05-22 00:00:00.000' and '2016-11-23 00:00:00.000'
    GROUP BY s.FullName,s.DomainName --DATEPART(MM,DATEADD(HH,(DATEDIFF(HH,GetUTCDate(),GetDate())), A.CreatedOn))
    ORDER BY [LastAccessAt] desc

Solution

  • Try this:

    DECLARE @FirstAccessDate DATETIME;
    DECLARE @LastAccessDate DATETIME;
    DECLARE @Today DATETIME;
    
    SET @FirstAccessDate = '20160920';
    SET @LastAccessDate = '20160922';
    SET @Today = '20161122';
    
    SELECT  CASE WHEN DATEADD(MONTH, 2, @LastAccessDate) >= @Today THEN NULL
                 ELSE 'Older than 2 months'
            END AS IsOlderThanTwoMonths,
            CASE WHEN @FirstAccessDate = @LastAccessDate THEN 'Never Logged On'
                 ELSE CAST(DATEDIFF(DAY, @FirstAccessDate, @LastAccessDate) AS VARCHAR(20))
            END AS Duration; 
    
    SET @LastAccessDate = '20160921';       
    SET @FirstAccessDate = '20160921';
    
    SELECT  CASE WHEN DATEADD(MONTH, 2, @LastAccessDate) >= @Today THEN NULL
                 ELSE 'Older than 2 months'
            END AS IsOlderThanTwoMonths ,
            CASE WHEN @FirstAccessDate = @LastAccessDate THEN 'Never Logged On'
                 ELSE CAST(DATEDIFF(DAY, @FirstAccessDate, @LastAccessDate) AS VARCHAR(20))
            END AS Duration; 
    

    So your query should look something like this:

    SELECT
        UserId,
        FullName,
        FirstAccessAt,
        LastAccessAt,
        CASE WHEN DATEADD(MONTH, 2, LastAccessAt) >= @Today THEN NULL
                ELSE 'Older than 2 months'
        END AS IsOlderThanTwoMonths,
        CASE WHEN FirstAccessAt = LastAccessAt THEN 'Never Logged On'
                ELSE CAST(DATEDIFF(DAY, FirstAccessAt, LastAccessAt) AS VARCHAR(20))
        END AS Duration
    FROM (
            SELECT
                s.DomainName as UserId,
                s.fullname as FullName,
                MIN(A.CreatedOn) AS FirstAccessAt,
                MAX(A.CreatedOn) AS LastAccessAt
            FROM archive a
            INNER JOIN Systemuser s
                ON s.systemuserid = a.objectid
            WHERE
                a.action = 54
            and a.CreatedOn between '2015-05-22 00:00:00.000' and '2016-11-23 00:00:00.000'
            GROUP BY
                s.FullName, s.DomainName
            ) t
    ORDER BY LastAccessAt DESC