Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
Return X elements before and after sorted by a property that is not unique SQL

I am trying to write a query that returns X elements before and after a given entity sorted by a property that is not unique.

For example: Property a is the primary column (a unique UUID), b is the property I would like to sort by

  table
  --------
  a      b
  --------
  ag     1     
  sb     1
  sf     1
  xk     2
- bd     2
  ve     2
  ku     2
  lt     3
  ac     3

If I wanted to return the elements before and after a = bd sorted by b

Before

SELECT * FROM table WHERE b >= 2 ORDER BY b DESC, a DESC LIMIT x

After

SELECT * FROM table WHERE b <= 2 ORDER BY b ASC, a DESC OFFSET 1 LIMIT x

If the property of b was unique this would work. How would I do this on a non-unique property.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use window functions:

select t.*
from (select t.*,
             count(*) filter (where a = 'bd') over (order by b, a rows between x preceding and x following) as cnt
      from t
     ) t
where cnt > 0;

Here is a db<>fiddle.

over 4 years ago · Santiago Trujillo Denunciar

0

This is an implementation that does windowing

WITH
    data AS (SELECT * FROM id_value),
    before AS (
        SELECT * FROM id_value
            WHERE VALUE < (SELECT VALUE FROM id_value WHERE id = ID)
            ORDER BY VALUE DESC, id DESC
            limit 1200
    ),
    after AS (
        SELECT * FROM id_value
            WHERE VALUE >= (SELECT VALUE FROM id_value WHERE id = ID)
            ORDER BY VALUE ASC, id ASC
            limit 1200
    ),
    windowed AS (
        (SELECT * FROM before)
        UNION ALL
        (SELECT * FROM after)
    )

SELECT t.*
FROM (
    SELECT *,
             COUNT(*) filter (
                 WHERE id = ID
             ) over (
                 ORDER BY VALUE, id rows BETWEEN 5 preceding AND 5 following
             ) AS cnt
    FROM windowed
) t
WHERE cnt > 0;
over 4 years ago · Santiago Trujillo Denunciar

0

You could row_number() as follows:

select a, b
from (
    select t.*, max(rn) filter(where a = 'bd') over() target_rn
    from (
        select t.*, row_number() over(order by b, a) rn
        from mytable t
    ) t
) t
where abs(rn - target_rn) <= 2
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda