SQL untuk Bahasa Kueri Kusto contekan
Berlaku untuk: ✅Microsoft Fabric✅Azure Data Explorer✅Azure Monitor✅Microsoft Sentinel
Jika Anda terbiasa dengan SQL dan ingin mempelajari KQL, terjemahkan kueri SQL ke dalam KQL dengan prefacing kueri SQL dengan baris komentar, --
, dan kata kunci explain
. Output menunjukkan versi KQL kueri, yang dapat membantu Anda memahami sintaks dan konsep KQL.
--
explain
SELECT COUNT_BIG(*) as C FROM StormEvents
Hasil
Kueri |
---|
StormEvents| meringkas<br> C=count()<br> | proyek C |
Tabel berikut ini memperlihatkan contoh kueri di SQL dan setara KQL-nya.
Kategori | SQL Query | Kueri Kusto | Pelajari lebih lanjut |
---|---|---|---|
Memilih data dari tabel | SELECT * FROM dependencies |
dependencies |
Pernyataan ekspresi tabular |
-- | SELECT name, resultCode FROM dependencies |
dependencies | project name, resultCode |
proyek |
-- | SELECT TOP 100 * FROM dependencies |
dependencies | take 100 |
take |
Evaluasi null | SELECT * FROM dependencies WHERE resultCode IS NOT NULL |
dependencies | where isnotnull(resultCode) |
isnotnull() |
Operator perbandingan (tanggal) | 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 |
Operator perbandingan (string) | SELECT * FROM dependencies WHERE type = "Azure blob" |
dependencies | where type == "Azure blob" |
Operator logis |
-- | -- substring SELECT * FROM dependencies WHERE type like "%blob%" |
// substring dependencies | where type has "blob" |
Memiliki |
-- | -- wildcard SELECT * FROM dependencies WHERE type like "Azure%" |
// wildcard dependencies | where type startswith "Azure" // or dependencies | where type matches regex "^Azure.*" |
startswith kecocokan regex |
Perbandingan (boolean) | SELECT * FROM dependencies WHERE !(success) |
dependencies | where success == False |
Operator logis |
Pengelompokan, Agregasi | SELECT name, AVG(duration) FROM dependencies GROUP BY name |
dependencies | summarize avg(duration) by name |
meringkas avg() |
Distinct | SELECT DISTINCT name, type FROM dependencies |
dependencies | summarize by name, type |
meringkas berbeda |
-- | 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() |
Alias kolom, Memperluas | SELECT operationName as Name, AVG(duration) as AvgD FROM dependencies GROUP BY name |
dependencies | summarize AvgD = avg(duration) by Name=operationName |
Pernyataan alias |
-- | SELECT conference, CONCAT(sessionid, ' ' , session_title) AS session FROM ConferenceSessions |
ConferenceSessions | extend session=strcat(sessionid, " ", session_title) | project conference, session |
proyek strcat() |
Memesan | SELECT name, timestamp FROM dependencies ORDER BY timestamp ASC |
dependencies | project name, timestamp | sort by timestamp asc nulls last |
urutkan |
Top n by measure | 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)) |
|
Bergabung | 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 |
Kueri bertingkat | 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) |
skalar |
Having | SELECT COUNT(\*) FROM dependencies GROUP BY name HAVING COUNT(\*) > 3 |
dependencies | summarize Count = count() by name | where Count > 3 |
meringkas di mana |
- Menggunakan T-SQL untuk mengkueri data