表dbo.AllApps
記錄所有已運行的可執行文件,它具有一個名爲ApplicationHash
的列,該列存儲exe的哈希值。
因此,如果exe已經運行了10次,表中將有10個條目具有相同的信息但時間戳不同。
我想生成一個報告,它只會返回10行中的1行(包括所有列時間,名稱,desc ...)和一個額外的列,它們將提供執行exe的次數。
有什麼建議嗎?SQL - 僅返回匹配的第一行
`/* !!!!!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 */
SELECT ApplicationHash, COUNT(ApplicationHash) AS ProcessCount
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, dbo.AllApps
WHERE dbo.AllApps.ApplicationHash = #PCount.ApplicationHash
AND TokenType = 'Elevated'
AND ApplicationType != 'COM Class'
AND ApplicationType != 'ActiveX Control'
AND ProcessStartTime >= @StartDate
AND ProcessStartTime < @EndDate
ORDER BY ProcessStartTime DESC`
在sql server中,您只需以'TOP 1'作爲字段列表的前綴來獲取第一行。 – Hogan
而在10行中,您希望讀取哪個時間,名稱,desc等? – dotNET
@Hogan - 如果你只想要第一行,而不是所有'ApplicationHash'值的第一行,這纔是真實的。 –