2

以下の sql クエリを使用して SQL db から Excel にいくつかのデータを引き出した後、分析のために以下のようにそれぞれの式を含む 2 つの追加の列を Excel に追加しました。現在、これらの Excel 数式を 2 つの列として SQL クエリ自体に追加しようとしていますが、問題が発生しています。上記の 2 つの数式を SQL クエリに追加する方法を教えてください。

よろしくお願いします。

  1. 2 か月以上 数式 = IF(LastAccessDate>TODAY()-(365/6),"","2 か月以上")
  2. 期間チェック 式 =IF(LastAccessDate-FirstAccessedDate=0,"Never Logged On",LastAccessDate-FirstAccessedDate)

SQL クエリ:

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

はい、2 つのステートメントの間にコンマを追加し、'Column Name' を一意の名前に置き換えてください。

于 2016-11-22T15:00:03.733 に答える