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

133
Vistas
custom aggregation of one column

with a table table1 like below

+------+-----+------+----------+-----------+
| city | day | hour | car_name | car_count |
+------+-----+------+----------+-----------+
|    1 |  12 |   00 | corolla  |         8 |
|    1 |  12 |   00 | city     |         9 |
|    1 |  13 |   00 | corolla  |        17 |
|    1 |  13 |   00 | city     |         2 |
|    1 |  14 |   00 | corolla  |         3 |
+------+-----+------+----------+-----------+

for each city, day , hour need to find out the difference count_diff in the car_count for car_names corolla and city excluding those when both or either of them is not present. count_diff is essentially [ (count of corollas) - (count of city) ]

expected output

+------+-----+------+-----------+
| city | day | hour | count_diff|
+------+-----+------+-----------+
|    1 |  12 |   00 | -1        | 
|    1 |  13 |   00 | 15        | 
+------+-----+------+-----------+
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

with data as 
(select city, day, hour , 
    sum(case when  car_name = 'corolla' then car_count else 0 end) corolla_count, 
    sum(case when  car_name = 'city' then car_count else 0 end) as city_count
 group by city, day, hour
) 
select city, day, hour, corolla_count - city_count from data 
where corrolla_count > 0 and city_count > 0

You could try this.

over 4 years ago · Santiago Trujillo Denunciar

0

t=# with a as (
  select
  *
  , count(1) over a
  , car_count - lead(car_count) over a count_diff
  from table1
  window a as (partition by city,day,hour)
)
select city,day,hour,count_diff
from a
where count >1 and count_diff is not null;
 city | day |  hour  | count_diff
------+-----+--------+------------
    1 |  12 |    00  |         -1
    1 |  13 |    00  |         15
(2 rows)

Time: 0.411 ms
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