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.
- Older than 2 months Formula = IF(LastAccessDate>TODAY()-(365/6),"","Older than 2 months")
- 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