2011-07-25 56 views
1
一筆或單行

我用這個變化:返回COUNT每一行,而不是在POSTGRES

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; 

所以我可以得到所有的真與假的計數,但我需要一個真正的假count在關聯數組中返回。

期望的結果:

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

回答

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 
+0

完美和簡單!非常感謝,你做搖滾! (顯然我還不能給任何人+1,有人請爲我做!)我現在可以+1! – roberthuttinger

+0

@robert:請將其中一個答案(您認爲最有幫助的)標記爲已接受。要做到這一點,請點擊答案的選票數下面的複選標記,以便它變成綠色。 – Quassnoi

+0

從我這裏稍微加了一點:'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' – roberthuttinger

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 
+0

這將返回等於計數。 – Quassnoi

+0

謝謝!每行返回14 | 3(示例) – roberthuttinger

+0

@roberthuttinger:固定的。 – marc

相關問題