1

I have used variations on this:

SELECT (select count(active) AS true 
        from sooper_entry 
        where active = 't' 
        and entry_id_ref = 28) AS true,
       (select count(active) AS false 
        from sooper_entry 
        where active = 'f' 
        and entry_id_ref = 28) AS false;

So I can get a COUNT of all the true and false, but I need a true false count returned in an associative array.

desired result:

 true | false | uId 
------+-------+-----
   16 |     0 |  1
   10 |     2 |  3
   13 |    10 |  4
   19 |     8 |  5
   12 |     3 |  8
   21 |     0 | 12
(6 rows)

2 Answers 2

4
SELECT 
  sum(case when active = 't' then 1 else 0 end) AS true, 
  sum(case when active = 'f' then 1 else 0 end) AS false, 
  entry_id_ref
FROM sooper_entry
GROUP BY entry_id_ref
Sign up to request clarification or add additional context in comments.

4 Comments

This will return equal counts.
Thanks! each row returns 14 | 3 (example)
Thanks (@quassnoi as well), you both pointed out my glaring omission of GROUP BY! cheers!
is there a benfit ti using the cast as opposed to case?
1
SELECT  SUM(active::BOOLEAN::INT) AS active, 
        SUM((NOT active::BOOLEAN)::INT) AS inactive, 
        entry_id_ref
FROM    sooper_entry
GROUP BY
        entry_id_ref

4 Comments

Perfection and simplicity! Thanks much, you do rock! (apparently I cannot +1 anyone yet, someone please do it for me!) Yay I can +1 now!
@robert: please mark one of the answers (that you think helped you most) as accepted. To do this, click the check mark under the number of votes for the answer so that it becomes green.
One slight addition from me: SELECT SUM(active::BOOLEAN::INT) as active, SUM((NOT active::BOOLEAN)::INT) as inactive, entry_id_ref FROM sooper_entry GROUP BY entry_id_ref
@robert: if active is a boolean, you may omit the ::BOOLEAN part (it's a cast to boolean)

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.