Funciones de ventana (ROW_NUMBER/RANK/LAG)
Oracle
-- Rank by amount within each city and fetch the previous value:
SELECT city, name, amount,
ROW_NUMBER() OVER (PARTITION BY city ORDER BY amount DESC) AS row_num,
RANK() OVER (PARTITION BY city ORDER BY amount DESC) AS rnk,
LAG(amount) OVER (PARTITION BY city ORDER BY created_at) AS prev_amount
FROM sale;Oracle fue pionero en las funciones analíticas (de ventana); ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, SUM/AVG OVER, NTILE y FIRST_VALUE están totalmente soportadas. Dentro de OVER, PARTITION BY define los grupos, ORDER BY el orden y el marco (ROWS/RANGE BETWEEN) la ventana deslizante.
PostgreSQL
-- Window functions with OVER():
SELECT city, name, amount,
ROW_NUMBER() OVER (PARTITION BY city ORDER BY amount DESC) AS row_num,
RANK() OVER (PARTITION BY city ORDER BY amount DESC) AS rnk,
LAG(amount) OVER (PARTITION BY city ORDER BY created_at) AS prev_amount
FROM sale;PostgreSQL admite plenamente las funciones de ventana con la sintaxis SQL estándar. Están disponibles ROW_NUMBER/RANK/DENSE_RANK, LAG/LEAD, agregados con OVER, NTILE y FIRST_VALUE/LAST_VALUE. Las definiciones de ventana repetidas se pueden nombrar y compartir con la cláusula WINDOW.
MySQL
-- MySQL 8.0+ window functions:
SELECT city, name, amount,
ROW_NUMBER() OVER (PARTITION BY city ORDER BY amount DESC) AS row_num,
RANK() OVER (PARTITION BY city ORDER BY amount DESC) AS rnk,
LAG(amount) OVER (PARTITION BY city ORDER BY created_at) AS prev_amount
FROM sale;MySQL añadió las funciones de ventana en la versión 8.0 (MariaDB 10.2+ también las admite). Están disponibles ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, NTILE y agregados con OVER. Las versiones anteriores a 8.0 carecen de estas funciones; antes se emulaban con variables de usuario.
SQL Server
-- Window functions with OVER():
SELECT city, name, amount,
ROW_NUMBER() OVER (PARTITION BY city ORDER BY amount DESC) AS row_num,
RANK() OVER (PARTITION BY city ORDER BY amount DESC) AS rnk,
LAG(amount) OVER (PARTITION BY city ORDER BY created_at) AS prev_amount
FROM dbo.sale;SQL Server admite funciones de ventana desde 2005 (ROW_NUMBER/RANK) y 2012 (LAG/LEAD, marcos, agregados con OVER). PARTITION BY, ORDER BY y el marco ROWS/RANGE están plenamente disponibles. OFFSET/FETCH o ROW_NUMBER son habituales para la paginación.
MongoDB
// Window calculations with $setWindowFields:
db.sale.aggregate([
{ $setWindowFields: {
partitionBy: "$city",
sortBy: { amount: -1 },
output: {
row_num: { $documentNumber: {} },
rnk: { $rank: {} },
prev_amount: { $shift: { output: "$amount", by: -1 } }
}
} }
])La etapa de agregación $setWindowFields, introducida en MongoDB 5.0, proporciona funciones de ventana: $documentNumber (ROW_NUMBER), $rank/$denseRank, $shift (LAG/LEAD), funciones de agregación y ventanas deslizantes (window: { documents/range }). partitionBy y sortBy corresponden a PARTITION BY/ORDER BY en SQL.
Redis
# Not applicable: there are no SQL-like window functions.Redis es un almacén clave-valor; no tiene funciones analíticas de ventana. Para resultados ordenados se puede usar un Sorted Set (acceso tipo ranking con ZADD/ZRANGE); sin embargo, las operaciones de ventana relacionales como PARTITION BY/LAG se hacen en la capa de aplicación.
Elasticsearch
# Not applicable: there are no SQL-like window functions.
# Closest equivalent: bucket aggregations.
GET sale/_search
{ "size": 0, "aggs": { "by_city": {
"terms": { "field": "city" },
"aggs": { "total": { "sum": { "field": "amount" } } } } } }Elasticsearch no ofrece funciones de ventana clásicas (ROW_NUMBER/RANK/LAG). La agrupación y la ordenación se hacen con el framework de agregaciones: una terms aggregation se aproxima a PARTITION BY, top_hits a los registros principales y derivative/serial_diff (pipeline aggregations) a diferencias sucesivas tipo LAG.