1

This is Postgres 8.x. Speicifcally Redshift.

Table has all FALSE in all the 3 columns. However, when I run this query, it returns as if all are TRUE.

SELECT
count(CASE WHEN facebook THEN 1 ELSE 0 END)
, count(CASE WHEN instagram THEN 1 ELSE 0 END)
, count(CASE WHEN twitter THEN 1 ELSE 0 END)
FROM public.sampletable

Not sure what I'm doing wrong. I've also tried CASE WHEN facebook = TRUE

I've made sure the cells aren't NULL or anything as well

7
  • 2
    use sum() instead. count just checks if there's something that's countable (e.g. not null). 0 counts exactly the same as 1. Commented Oct 26, 2015 at 19:22
  • Ah, thanks. Do you know if it's possible to sum all 3 columns without a subquery? Commented Oct 26, 2015 at 19:25
  • you don't have any subqueries there. Commented Oct 26, 2015 at 19:26
  • 1
    SUM(CASE WHEN facebook THEN 1 ELSE 0 END + CASE WHEN twitter THEN 1 ELSE 0 END + CASE WHEN instagram THEN 1 ELSE 0 END) Simple + do the job Commented Oct 26, 2015 at 19:29
  • 1
    sum doesn't accept multiple arguments. do sum(a+b+c), instead of sum(a,b,c) Commented Oct 26, 2015 at 19:31

2 Answers 2

4

The problem is that COUNT count 1 and 0 the same. One way is to change the values you want to avoid to NULL

SELECT
  count(CASE WHEN facebook  THEN 1 ELSE NULL END)
, count(CASE WHEN instagram THEN 1 ELSE NULL END)
, count(CASE WHEN twitter   THEN 1 ELSE NULL END)
FROM public.sampletable

SELECT
   count(CASE WHEN facebook  THEN 1 END) -- ELSE NULL is default so you can skip
  ,count(CASE WHEN instagram THEN 1 END)
  ,count(CASE WHEN twitter   THEN 1 END)
FROM public.sampletable

or as Marc B proposed in comment use SUM:

SELECT
 SUM(CASE WHEN facebook THEN 1 ELSE 0 END)
,SUM(CASE WHEN instagram THEN 1 ELSE 0 END)
,SUM(CASE WHEN twitter THEN 1 ELSE 0 END)
FROM public.sampletable

EDIT:

If you need combined sum you can use:

SUM(CASE WHEN facebook THEN 1 ELSE 0 END +
    CASE WHEN twitter THEN 1 ELSE 0 END +
    CASE WHEN instagram THEN 1 ELSE 0 END)

EDIT 2

This answer is inspired by answer below. You can simply cast data:

SELECT SUM(CAST(facebook AS INT)),
       SUM(CAST(instagram AS INT)),
       SUM(CAST(twitter AS INT)),
       SUM(CAST(facebook AS INT) + CAST(instagram AS INT) + CAST(twitter AS INT))
FROM sampletable;

SqlFiddleDemo

Sign up to request clarification or add additional context in comments.

1 Comment

count is an aggregate function. It COUNTs the number of rows returned, not their values. SUM adds all the VALUEs of the rows. Can't edit because there are too many pending edits.
3

You can use simple cast to integer:

select
    sum(facebook::int) facebook,
    sum(instagram::int) instagram,
    sum(twitter::int) twitter,
    sum(facebook::int+ instagram::int+ twitter::int) total
from public.sampletable;

3 Comments

This is much simpler than the cumbersome case syntax in the accepted answer.
Good point, casting data for true -1 false - 0 is good approach. I allowed myself update my answer too. Anyway +1 :)
@ClodoaldoNeto I am not from Postgresql world so I would use approach I suggested. The point of view dependent of what are you familliar with for example with MySQL you don't need to do anything: sqlfiddle.com/#!9/e1836/1/0. Anyway I also wanted to answer why OP original query doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.