Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!