8

I have both a MS SQL Database and a postgres database. A solution in either will work as I can translate it.

We have a customer_phone table where the relevant columns are:

id, customer_id, phone, is_bad

What I need to do is select all the customer_id's from this table that ONLY have is_bad = true. So if you have 1 good phone number and 1 bad, you shouldn't appear.

For some reason I'm struggling to find an easy way to do this, I feel like it should be deceptively simple.

This was a start that gives me at least all the customers a count of their good and bad numbers, but I'm wondering if there is a way that doesn't involve having to use subqueries and in's?

select  customer_id, is_bad, count(customer_id)
from customer_phone cp
group by customer_id, is_bad
order by customer_id desc
6
  • 1
    Bit confusing: you've tagged the question postgresql, but in the title you've mentioned TSQL which is Microsoft's proprietary version of SQL. Which database are you using?
    – LondonRob
    Jan 26, 2018 at 17:20
  • In fact, the title seems to refer to a completely different question! This question has nothing to do with 1:N or specific types!!
    – LondonRob
    Jan 26, 2018 at 17:24
  • There is no T-SQL in Postgres Jan 26, 2018 at 17:54
  • Make up your mind. It's for SQL Server or Postgres?????
    – Eric
    Jan 26, 2018 at 17:59
  • It's both, multiple databases. An answer for either one would suffice as I can most likely translate it to the other. Jan 26, 2018 at 20:27

4 Answers 4

5

The most intuitive way I've found to add over a boolean column like this is to SUM a column which is 1 when TRUE and 0 when FALSE:

CASE WHEN some_bool THEN 1 ELSE 0 END

So in your case:

SELECT id
FROM phones
GROUP BY id
HAVING
SUM(CASE WHEN NOT is_bad THEN 1 ELSE 0 END) = 0

Check out this SQL fiddle which shows the query in action.

4

You could use the "aggregate function", bool_and.

This takes a set of inputs and returns:

true if all input values are true, otherwise false

In your case, where we want every is_bad to be true for a particular customer:

select customer_id
from customer_phone
group by customer_id
having bool_and(is_bad)
1
  • This is very cool! I did not realize that postgres had this functionality. Jan 29, 2018 at 15:17
1

You can use NOT EXISTS:

select  customer_id
       ,is_bad
       ,count(customer_id)
from customer_phone cp
where not exists(select 1 from customer_phone 
                 where is_bad = true)
group by customer_id, is_bad
order by customer_id desc
;
3
  • This is the right approach but I think you have the is_bad filter value reversed. That should be 'is_bad = false'. They want only customers that don't have a good phone. Jan 26, 2018 at 17:05
  • @Tofystedeth yeah, that's why I used NOT EXISTS
    – Lamak
    Jan 26, 2018 at 17:39
  • 1
    Yes, but since you're saying NOT EXISTS where is_bad = true, you're getting entries that don't have a bad row. You want ones that don't have a good row. Also, shouldn't there be a and customer_id = cp.cusomter_id in there as well. Jan 26, 2018 at 19:01
1

You could use MIN to identify if there are only is_bad values. You just have to cast to Int first.

SELECT customer_id
FROM customer_phone cp
GROUP BY customer_id
HAVING MIN(cast(is_bad as int)) = 1;
3
  • Not sure how I feel about relying on the results of casting from boolean to int. I wouldn't be all that surprised if that behaviour is allowed to change...
    – LondonRob
    Jan 26, 2018 at 17:22
  • 2
    I hope not. I've been casting boolean to int for years
    – Greg Viers
    Jan 26, 2018 at 17:31
  • Ha ha. I hope not too!
    – LondonRob
    Jan 31, 2018 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.