2

我使用下面的 sql 查询从 SQL db 中提取了一些数据到 Excel,然后在 excel 中我添加了两个额外的列,它们各自的公式如下进行分析。现在我正在尝试将这些 excel 公式作为 2 列添加到我的 sql 查询中,我遇到了麻烦,请您建议我如何将上述两个公式添加到我的 sql 查询中。

提前谢谢了。

  1. 超过 2 个月公式 = IF(LastAccessDate>TODAY()-(365/6),"","超过 2 个月")
  2. 持续时间检查公式 =IF(LastAccessDate-FirstAccessedDate=0,"Never Logged On",LastAccessDate-FirstAccessedDate)

查询:

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
4

3 回答 3

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
于 2016-11-22T14:47:44.853 回答
0

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

在 TSQL 中:

CASE WHEN DateDiff(Month,LastAccessDate,GetDate()) >2 THEN 'Older Than 2 months' ELSE NULL END AS ColumnName

2.时长校验公式=IF(LastAccessDate-FirstAccessedDate=0,"Never Logged On",LastAccessDate-FirstAccessedDate)

在 TSQL 中:

CASE WHEN (LastAccessDate-FirstAccessDate)=0 THEN 'Never Logged On' ELSE (LastAccessDate-FirstAccessDate) END AS ColumnName

于 2016-11-22T14:43:32.070 回答
0

是的,只需在两个语句之间添加一个逗号并将“列名”替换为唯一名称。

于 2016-11-22T15:00:03.733 回答