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

238
Views
Filter out results if a value exists in any of the rows in another table sql

I'm trying to count how many of the objects appear in table 2 without the status of 3 or 5. So if it has the status of 3 or 5 I want to exclude it from the count. Where I'm stuck is there are duplicate values, as they have may more than one status. Further explanation below.


Table 1

Object_ID 
1
2
3
4
5

Table 2

ID  |  object_id  |  status
1          2           2
2          2           3
3          2           5
4          3           2
5          3           2
6          3           7

END GOAL

Count how many object_ids have a status excluding 3 or 5. But also to ignore duplicates. In this example, the total count would be 1 (with the object_id being 3). As I need to find all of the rows in table 2, then essentially merge them together assuming neither of them has a status of 3 or 5.

Count.   |    object_id
  1      |        3

SELECT Count(distinct(object_id))
FORM table_2
WHERE status <> 3 or status <> 5

I seem to be able to group them if the status is 3 or 5, but I can't seem to exclude them.

Hopefully, it makes sense, I've tried to simplify it so I don't have irrelevant code included.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

One method without subqueries is:

select count(distinct object_id) - count(distinct case when status in (3, 5) then object_id end)
from table_2;

This counts the number of distinct object ids and then subtracts the number of distinct object ids that have the specified statuses.

More typically, I would approach this with two levels of aggregation:

select count(*)
from (select object_id
      from table_2
      group by object_id
      having sum( status in (3, 5) ) = 0
     ) o
over 4 years ago · Santiago Trujillo Report

0

You can use aggregation:

select count(*) 
from (
    select object_id
    from table2
    group by object_id
    having max(status in (3, 5)) = 0
) t
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!