共用方式為


SQL Kusto 查詢語言 速查表

適用於:✅Microsoft網狀架構Azure 數據✅總管 Azure 監視器✅Microsoft Sentinel

如果您熟悉 SQL 並想要瞭解 KQL,請使用批注行 --和 關鍵詞 explain將 SQL 查詢前置詞,將 SQL 查詢轉譯為 KQL。 輸出會顯示查詢的 KQL 版本,可協助您瞭解 KQL 語法和概念。

--
explain
SELECT COUNT_BIG(*) as C FROM StormEvents 

輸出

Query
StormEvents<br>| summarize C=count()<br>| 專案 C

SQL 對 Kusto 的功能提要

下表顯示 SQL 中的範例查詢及其 KQL 對等專案。

類別 SQL 查詢 Kusto 查詢 深入了解
從數據表選取數據 SELECT * FROM dependencies dependencies 表格式表達式語句
-- SELECT name, resultCode FROM dependencies dependencies | project name, resultCode 專案
-- SELECT TOP 100 * FROM dependencies dependencies | take 100 take
Null 評估 SELECT * FROM dependencies
WHERE resultCode IS NOT NULL
dependencies
| where isnotnull(resultCode)
isnotnull()
比較運算子 (日期) SELECT * FROM dependencies
WHERE timestamp > getdate()-1
dependencies
| where timestamp > ago(1d)
ago()
-- SELECT * FROM dependencies
WHERE timestamp BETWEEN ... AND ...
dependencies
| where timestamp between (datetime(2016-10-01) .. datetime(2016-11-01))
between
比較運算子 (字串) SELECT * FROM dependencies
WHERE type = "Azure blob"
dependencies
| where type == "Azure blob"
邏輯運算子
-- -- substring
SELECT * FROM dependencies
WHERE type like "%blob%"
// substring
dependencies
| where type has "blob"
has
-- -- wildcard
SELECT * FROM dependencies
WHERE type like "Azure%"
// wildcard
dependencies
| where type startswith "Azure"
// or
dependencies
| where type matches regex "^Azure.*"
startswith
matches regex
比較 (布林值) SELECT * FROM dependencies
WHERE !(success)
dependencies
| where success == False
邏輯運算子
群組、匯總 SELECT name, AVG(duration) FROM dependencies
GROUP BY name
dependencies
| summarize avg(duration) by name
summarize
avg()
Distinct SELECT DISTINCT name, type FROM dependencies dependencies
| summarize by name, type
summarize
distinct
-- SELECT name, COUNT(DISTINCT type)
FROM dependencies
GROUP BY name
dependencies
| summarize by name, type | summarize count() by name
// or approximate for large sets
dependencies
| summarize dcount(type) by name
count()
dcount()
數據行別名,擴充 SELECT operationName as Name, AVG(duration) as AvgD FROM dependencies
GROUP BY name
dependencies
| summarize AvgD = avg(duration) by Name=operationName
Alias 語句
-- SELECT conference, CONCAT(sessionid, ' ' , session_title) AS session FROM ConferenceSessions ConferenceSessions
| extend session=strcat(sessionid, " ", session_title)
| project conference, session
strcat()
project
排序 SELECT name, timestamp FROM dependencies
ORDER BY timestamp ASC
dependencies
| project name, timestamp
| sort by timestamp asc nulls last
sort
前 n 個依量值 SELECT TOP 100 name, COUNT(*) as Count FROM dependencies
GROUP BY name
ORDER BY Count DESC
dependencies
| summarize Count = count() by name
| top 100 by Count desc
top
Union SELECT * FROM dependencies
UNION
SELECT * FROM exceptions
union dependencies, exceptions union
-- SELECT * FROM dependencies
WHERE timestamp > ...
UNION
SELECT * FROM exceptions
WHERE timestamp > ...
dependencies
| where timestamp > ago(1d)
| union
(exceptions
| where timestamp > ago(1d))
聯結 SELECT * FROM dependencies
LEFT OUTER JOIN exceptions
ON dependencies.operation_Id = exceptions.operation_Id
dependencies
| join kind = leftouter
(exceptions)
on $left.operation_Id == $right.operation_Id
join
巢狀查詢 SELECT * FROM dependencies
WHERE resultCode ==
(SELECT TOP 1 resultCode FROM dependencies
WHERE resultId = 7
ORDER BY timestamp DESC)
dependencies
| where resultCode == toscalar(
dependencies
| where resultId == 7
| top 1 by timestamp desc
| project resultCode)
toscalar
擁有 SELECT COUNT(\*) FROM dependencies
GROUP BY name
HAVING COUNT(\*) > 3
dependencies
| summarize Count = count() by name
| where Count > 3
summarize
where
  • 使用 T-SQL 查詢數據