Iniciar sesión

Practice: counting and grouping

14 min · 2 ejercicio(s)

Cheat sheet

Function Meaning
COUNT(*) Number of rows
COUNT(field) Rows where the field is not null
COUNT_DISTINCT(field) Number of different values (approximate on very large data)
SUM, AVG, MIN, MAX, MEDIAN Numeric statistics
VALUES(field) All distinct values as a list
BUCKET(@timestamp, 1 hour) Time buckets for BY
DATE_EXTRACT("hour_of_day", @timestamp) Hour (0–23, UTC)

Patterns

// Top N
FROM lab-web | STATS c = COUNT(*) BY url.path | SORT c DESC | LIMIT 5
// Rare values (often the interesting ones)
FROM lab-endpoint | STATS c = COUNT(*) BY process.name | SORT c ASC | LIMIT 10
// Filter on an aggregate
FROM lab-auth | STATS fails = COUNT(*) BY source.ip | WHERE fails > 50

Ties

When two groups have the same count, SORT c DESC | LIMIT 1 returns one of them more or less at random. Add a second sort key (SORT c DESC, host.name) when the answer must be stable, for example in a detection rule or a report.

Ejercicios prácticos de esta lección

Resuelve estos ejercicios con una consulta en el laboratorio Elasticsearch real. Inicia sesión gratis para acceder al laboratorio, las pistas y la corrección automática.

  1. 1. Broken links

    Which url.path returned the most 404 responses?

  2. 2. Distinct VPN users

    How many different users logged in through the VPN (service.name == "vpn")?