如何将Excel公式转换/模拟为SQL查询

时间:2016-11-22 13:18:00

标签: sql excel-formula excel-2010 sql-query-store

我使用下面的sql查询从SQL db中将一些数据提取到Excel,然后在excel中我添加了两个附加列,其中各自的公式如下所示进行分析。现在我试图将这些excel公式添加到我的sql查询本身作为2列,我遇到了麻烦,请你建议我如何将上述两个公式添加到我的sql查询中。

非常感谢提前。

  1. 大于2个月的公式=     IF(LastAccessDate> TODAY() - (365/6),“”,“超过2个月”)
  2. 持续时间检查公式= IF(LastAccessDate-FirstAccessedDate = 0,“从不 登录“,LastAccessDate-FirstAccessedDate”
  3. 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
    

3 个答案:

答案 0 :(得分:2)

试试这个:

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; 

所以你的查询应该是这样的:

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

答案 1 :(得分:0)

1。超过2个月的公式= IF(LastAccessDate> TODAY() - (365/6),"","超过2个月")

在TSQL中:

DateDiff(月,LastAccessDate,GetDate())> 2时的情况 那么'年龄超过2个月' ELSE NULL END AS ColumnName

2。持续时间检查公式= IF(LastAccessDate-FirstAccessedDate = 0,"从未登录",LastAccessDate-FirstAccessedDate)

在TSQL中:

CASE WHEN(LastAccessDate-FirstAccessDate)= 0 然后,永远不会登录' ELSE(LastAccessDate-FirstAccessDate) END AS ColumnName

答案 2 :(得分:0)

是的,只需在两个语句之间添加一个逗号并替换即可 '列名'  具有独特的名称。