2017-04-12 66 views
0

dbo.AllApps記錄所有已運行的可執行文件,它具有存儲exe文件哈希值的列ApplicationHash,存儲執行exe的用戶ID的列UserNameSQL Server:將結果返回一行並附加COUNT列

如果exe已經運行了10次,表中將會有10個條目具有相同的信息,但具有不同的時間戳和執行該實例的用戶。

我想生成一個只返回10行(包括所有列時間,名稱,desc ...)的報告,另外還有一列將提供exe的次數被執行(在這個例子中總共10行)和執行exe的唯一用戶數量(在這個例子中是3)。

有什麼建議嗎?

ApplicationHash TimeStamp UserName 
----------------------------------- 
ABCDE12345  00:00:05 User1 
ABCDE12345  00:02:05 User2 
ABCDE12345  00:04:05 User1 
ABCDE12345  00:10:05 User3 
ABCDE12345  00:15:05 User3 
ABCDE12345  00:20:05 User1 
ABCDE12345  00:21:05 User2 
ABCDE12345  00:33:05 User2 
ABCDE12345  00:45:05 User1 
ABCDE12345  00:55:05 User1 

結果應該是somethine這樣的:

ApplicationHash ProcessCount UserCount 
-------------------------------------- 
ABCDE12345   10   3 

這裏是有人想出了這確實解決了ProcessCount問題,但我需要一個唯一的用戶名數,以及:

declare @StartDate datetime = '20170401'; 
declare @EndDate datetime = '20170406'; 

select top 1 with ties 
    ProcessCount = count(*) over (partition by ApplicationHash) 
    , ApplicationHash 
    , ProcessStartTime 
    , ApplicationType 
    , Description 
    , Publisher 
    , ProductName 
    , ProductVersion 
    , EventDescription 
    , CommandLine 
    , FileName 
from dbo.AllApps aa 
where TokenType = 'Elevated' 
    and ApplicationType != 'com Class' 
    and ApplicationType != 'ActiveX Control' 
    and ProcessStartTime >= @StartDate 
    and ProcessStartTime < @Enddate 
    and ProductName = 'WizSvcUt Application' 
order by row_number() over (partition by ApplicationHash order by ProcessStartTime desc) 
+1

請縮短本文。 – niksofteng

回答

0

您可以將count(distinct userName)添加到臨時表中。我也改變了你的隱式連接到一個顯式連接:

/* !!!!!DEFINE START AND END DATE FOR REPORT HERE!!!!!*/ 
DECLARE @StartDate DATETIME = 2017-01-01 
DECLARE @EndDate DATETIME = 2017-01-02 

/* Checks if the temp table #PCount exist and deletes the table if it does */ 
IF OBJECT_ID('tempdb..#PCount') IS NOT NULL 
BEGIN 
    DROP TABLE #PCount 
END 

/* Performs a COUNT on the ApplicationHash entries and distinct user names*/ 
SELECT ApplicationHash, 
     COUNT(ApplicationHash) AS ProcessCount, 
     COUNT(DISTINCT UserName) As UserCount 
INTO #PCount 
FROM dbo.AllApps 
WHERE TokenType = 'Elevated' 
    AND ApplicationType != 'COM Class' 
    AND ApplicationType != 'ActiveX Control' 
    AND ProcessStartTime >= @StartDate 
    AND ProcessStartTime < @EndDAte 
GROUP BY ApplicationHash 

/* Pulls up the actual report and inserts the count value for the ApplicationHash */ 
SELECT 
    #PCount.ProcessCount, 
    dbo.AllApps.ApplicationHash, 
    dbo.AllApps.ProcessStartTime, 
    dbo.AllApps.ApplicationType, 
    dbo.AllApps.Description, 
    dbo.AllApps.Publisher, 
    dbo.AllApps.ProductName, 
    dbo.AllApps.ProductVersion, 
    dbo.AllApps.EventDescription, 
    dbo.AllApps.CommandLine, 
    dbo.AllApps.FileName 
FROM 
    #PCount 
INNER JOIN dbo.AllApps ON dbo.AllApps.ApplicationHash = #PCount.ApplicationHash 
WHERE TokenType = 'Elevated' 
AND ApplicationType != 'COM Class' 
AND ApplicationType != 'ActiveX Control' 
AND ProcessStartTime >= @StartDate 
AND ProcessStartTime < @EndDate 
ORDER BY 
    ProcessStartTime DESC 

我不知道如果這是在2008年版本的支持,但在2012年,你可以簡單地使用過子句計數,並獲得同一結果在一個查詢中:

SELECT 
    COUNT(*) OVER (PARTITION BY ApplicationHash) AS ProcessCount, 
    COUNT(*) OVER (PARTITION BY ApplicationHash, UserName) As UserCount 
    ApplicationHash, 
    ProcessStartTime, 
    ApplicationType, 
    Description, 
    Publisher, 
    ProductName, 
    ProductVersion, 
    EventDescription, 
    CommandLine, 
    FileName 
FROM dbo.AllApps 
WHERE TokenType = 'Elevated' 
AND ApplicationType != 'COM Class' 
AND ApplicationType != 'ActiveX Control' 
AND ProcessStartTime >= @StartDate 
AND ProcessStartTime < @EndDate 
ORDER BY 
    ProcessStartTime DESC 
+0

我得到這個錯誤
AlexJP

+0

你得到了什麼錯誤? –

+0

對於OVER子句不允許使用DISTINCT。 – AlexJP

相關問題