PostgreSQL función de la ventana con el LÍMITE de
-
22-12-2019 - |
Pregunta
La consulta por debajo de los grupos los resultados de first
en 4 igualmente espaciados fecha de contenedores y de los agregados de un promedio de the_value
en cada bin.
WITH first as(
SELECT
extract(EPOCH FROM foo.t_date) as the_date,
foo_val as the_value
FROM bar
INNER JOIN foo
ON
foo.user_id = bar.x_id
and
foo.user_name = 'xxxx'
)
SELECT bin, round(sum(bin_sum) OVER w /sum(bin_ct) OVER w, 2) AS running_avg
FROM (
SELECT width_bucket(first.the_date
, x.min_epoch, x.max_epoch, x.bins) AS bin
, sum(first.the_value) AS bin_sum
, count(*) AS bin_ct
FROM first
, (SELECT MIN(first.the_date) AS min_epoch
, MAX(first.the_date) AS max_epoch
, 4 AS bins
FROM first
) x
GROUP BY 1
) sub
WINDOW w AS (ORDER BY bin)
ORDER BY 1;
Me gustaría ser capaz de calcular el promedio más bajo de decir 20 the_value
's en cada bin.De otros posts aquí en Stackoverflow he visto que esto es posible y que tal vez ORDER BY the_value
y rank()
es la mejor manera de ir sobre ella.Pero mi lucha es que no estoy seguro de que mi consulta actual debe ser modificado para implementar esto.
Cualquier visión se agradece.
Postgres versión 9.3
Solución
Uso row_number()
en cada compartimento.
Primer lugar, calcular el número de fila rn
, a continuación, aplicar WHERE rn < 21
en el siguiente paso:
WITH first AS (
SELECT extract(EPOCH FROM foo.t_date) AS the_date
, foo_val AS the_value
FROM bar
JOIN foo ON foo.user_id = bar.x_id
AND foo.user_name = 'xxxx'
)
, x AS (
SELECT MIN(the_date) AS min_epoch
, MAX(the_date) AS max_epoch
FROM first
)
, y AS (
SELECT width_bucket(f.the_date, x.min_epoch, x.max_epoch, 4) AS bin, *
FROM first f, x
)
, z AS (
SELECT row_number() OVER (PARTITION BY bin ORDER BY the_value) AS rn, *
FROM y
)
SELECT bin, round(sum(bin_sum) OVER w / sum(bin_ct) OVER w, 2) AS running_avg
FROM (
SELECT bin
, sum(the_value) AS bin_sum
, count(*) AS bin_ct
FROM z
WHERE rn < 21 -- max 20 lowest values
GROUP BY 1
) sub
WINDOW w AS (ORDER BY bin)
ORDER BY 1;
Cte y
y z
podría ser refundidos.Del mismo modo first
y x
podría ser refundidos.
Pero la más clara de esa manera.
No ha sido probado, ya que no disponemos de datos de prueba.