DA SQL a Linguaggio di query Kusto foglio informativo

Si applica a: ✅Microsoft Fabric✅Azure Esplora dati Azure MonitorMicrosoft Sentinel

Se si ha familiarità con SQL e si vuole apprendere KQL, convertire le query SQL in KQL anteponendo la query SQL con una riga di commento, --e la parola chiave explain. L'output mostra la versione KQL della query, che consente di comprendere la sintassi e i concetti di KQL.

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

Output

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

Scheda di riferimento rapido sul passaggio da SQL a Kusto

La tabella seguente illustra le query di esempio in SQL e i relativi equivalenti KQL.

Categoria Query SQL Query Kusto Altre informazioni
Selezione di dati da una tabella SELECT * FROM dependencies dependencies Istruzioni di espressione tabulare
-- SELECT name, resultCode FROM dependencies dependencies | project name, resultCode project
-- SELECT TOP 100 * FROM dependencies dependencies | take 100 take
Valutazione null SELECT * FROM dependencies
WHERE resultCode IS NOT NULL
dependencies
| where isnotnull(resultCode)
isnotnull()
Operatori di confronto (data) 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))
tra
Operatori di confronto (stringa) SELECT * FROM dependencies
WHERE type = "Azure blob"
dependencies
| where type == "Azure blob"
Operatori logici
-- -- substring
SELECT * FROM dependencies
WHERE type like "%blob%"
// substring
dependencies
| where type has "blob"
ha
-- -- wildcard
SELECT * FROM dependencies
WHERE type like "Azure%"
// wildcard
dependencies
| where type startswith "Azure"
// or
dependencies
| where type matches regex "^Azure.*"
startswith
matches regex
Confronto (booleano) SELECT * FROM dependencies
WHERE !(success)
dependencies
| where success == False
Operatori logici
Raggruppamento, aggregazioni 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()
Alias di colonna, estensioni SELECT operationName as Name, AVG(duration) as AvgD FROM dependencies
GROUP BY name
dependencies
| summarize AvgD = avg(duration) by Name=operationName
Istruzione Alias
-- SELECT conference, CONCAT(sessionid, ' ' , session_title) AS session FROM ConferenceSessions ConferenceSessions
| extend session=strcat(sessionid, " ", session_title)
| project conference, session
strcat()
project
Creazione dell'ordine SELECT name, timestamp FROM dependencies
ORDER BY timestamp ASC
dependencies
| project name, timestamp
| sort by timestamp asc nulls last
ordinamento
Primi n in base a misura 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
Query nidificate 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
riepilogare
dove
  • Usare T-SQL per eseguire query sui dati