Partilhar via


Folha de dicas do SQL para a linguagem de consulta Kusto

Aplica-se a: ✅Microsoft FabricAzure Data Explorer✅Azure MonitorMicrosoft Sentinel

Se você estiver familiarizado com SQL e quiser aprender KQL, converta as consultas SQL em KQL prefaciando a consulta SQL com uma linha de comentário, --, e a palavra-chave explain. A saída mostra a versão KQL da consulta, que pode ajudá-lo a entender a sintaxe e os conceitos KQL.

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

Saída

Consulta
StormEvents<br>| summarize C=count()<br>| projeto C

Roteiro do SQL para o Kusto

A tabela a seguir mostra exemplos de consultas em SQL e seus equivalentes KQL.

Categoria Consulta SQL Consulta Kusto Saiba mais
Selecione os dados da tabela SELECT * FROM dependencies dependencies Instruções de expressão tabular
-- SELECT name, resultCode FROM dependencies dependencies | project name, resultCode project
-- SELECT TOP 100 * FROM dependencies dependencies | take 100 take
Avaliação de nulo SELECT * FROM dependencies
WHERE resultCode IS NOT NULL
dependencies
| where isnotnull(resultCode)
isnotnull()
Operadores de comparação (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))
between
Operadores de comparação (cadeia de caracteres) SELECT * FROM dependencies
WHERE type = "Azure blob"
dependencies
| where type == "Azure blob"
Operadores lógicos
-- -- substring
SELECT * FROM dependencies
WHERE type like "%blob%"
// substring
dependencies
| where type has "blob"
Tem
-- -- wildcard
SELECT * FROM dependencies
WHERE type like "Azure%"
// wildcard
dependencies
| where type startswith "Azure"
// or
dependencies
| where type matches regex "^Azure.*"
startswith
matches regex
Comparação (booliano) SELECT * FROM dependencies
WHERE !(success)
dependencies
| where success == False
Operadores lógicos
Agrupamento, Agregação SELECT name, AVG(duration) FROM dependencies
GROUP BY name
dependencies
| summarize avg(duration) by name

resumir avg()
Distinct SELECT DISTINCT name, type FROM dependencies dependencies
| summarize by name, type

resumir distintos
-- 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
contagem()
dcontagem()
Aliases de coluna, Extensão SELECT operationName as Name, AVG(duration) as AvgD FROM dependencies
GROUP BY name
dependencies
| summarize AvgD = avg(duration) by Name=operationName
Instrução Alias
-- SELECT conference, CONCAT(sessionid, ' ' , session_title) AS session FROM ConferenceSessions ConferenceSessions
| extend session=strcat(sessionid, " ", session_title)
| project conference, session
projeto strcat()
Ordenando SELECT name, timestamp FROM dependencies
ORDER BY timestamp ASC
dependencies
| project name, timestamp
| sort by timestamp asc nulls last
sort
N principais por medida 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
início
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))
Ingressar 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
Consultas aninhadas 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
Posse SELECT COUNT(\*) FROM dependencies
GROUP BY name
HAVING COUNT(\*) > 3
dependencies
| summarize Count = count() by name
| where Count > 3

Resuma onde
  • Usar o T-SQL para consultar dados