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

210
Views
get records where one colum has values within range across records with same column names

with a table like below

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

need to find out the city, day, hour where the car_count for all car_names is >= 3 and <= 10

expected result

| city | day | hour |  
+------+-----+------+ 
|    1 |  12 |   00 |
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use group by and having.

select city,day,hour
from tablename
group by city,day,hour
having sum(case when car_count>=3 and car_count<=10 then 1 else 0 end) = count(*)
over 4 years ago · Santiago Trujillo Report

0

You can group by on city, day and hour with the having condition sum(your condition) = count(your condition)
So basically we are creating a flag for each row which satisfies the condition "10 >= car_count >= 3" . Now we are summing all the flags and counting them simultaneously, if both the count and sum are equal that means your condition "10 >= car_count >= 3" was true for all the cars against city,day and hour

create table want as
select city,day,hour from have
group by city,day,hour
having sum(car_count>=3 and car_count<=10)=count(car_count>=3 and car_count<=10);

Please let me know in case of any queries.

over 4 years ago · Santiago Trujillo Report

0

select city, day, hour
from t
group by 1, 2, 3
having bool_and(car_count >= 3)
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!