sql to Kusto 照会言語 チート シート
適用対象: ✅Microsoft Fabric✅Azure データ エクスプローラー✅Azure Monitor✅Microsoft Sentinel
SQL に精通していて、KQL を学習したい場合は、コメント行、 --
、キーワード explain
を使用して SQL クエリの前に SQL クエリを作成することで、SQL クエリを KQL に変換します。 この出力には、KQL のバージョンのクエリが表示されます。これは、KQL の構文と概念を理解するのに役立ちます。
--
explain
SELECT COUNT_BIG(*) as C FROM StormEvents
出力
クエリ |
---|
StormEvents<br> | summarize C=count()<br> | project C |
SQL から Kusto のチート シート
次の表は、SQL のサンプル クエリとその KQL に相当するクエリを示しています。
カテゴリ | SQL クエリ | Kusto クエリ | 詳細情報 |
---|---|---|---|
テーブルからデータを選択 | SELECT * FROM dependencies |
dependencies |
表形式の式ステートメント |
-- | SELECT name, resultCode FROM dependencies |
dependencies | project name, resultCode |
project |
-- | 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" |
は |
-- | -- 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)) |
|
Join | 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 |
HAVING | SELECT COUNT(\*) FROM dependencies GROUP BY name HAVING COUNT(\*) > 3 |
dependencies | summarize Count = count() by name | where Count > 3 |
summarize where |
関連するコンテンツ
- T-SQL を使用してデータを照会する