2010-09-28 13 views
1

的結果我正在使用PostgreSQL。
我有數據表所示:總結表

Parent_id  Count  Read 
---------  ------ ------ 
52405   2   False 
52405   1   True 

現在我想總結的數據等:

Parent_id  Count  Read 
---------  ------ ------ 
52405   3   False 

Count將記錄的總和
Read將是邏輯操作。

回答

3
SELECT 
    "Parent_id", 
    SUM("Count"), 
    bool_and("Read") 
FROM 
    tablename 
GROUP BY 
    "Parent_id"; 

我沒有使用,因爲非法列名的雙引號「,並在名稱中使用大寫的。

+0

這DBMS'es支持呢?從來沒有聽說過它,谷歌不 – zerkms 2010-09-28 06:35:21

+1

PostgreSQL:http://www.postgresql.org/docs/current/interactive/functions-aggregate.html現在我看到這個話題也被標記爲其他dbms的...... – 2010-09-28 06:38:58

0
SELECT Parent_id, 
     s, 
     CASE WHEN logical_sum = cnt 
      THEN 'True' 
      ELSE 'False' 
     END 
    FROM (SELECT SUM("Count") as s, 
       SUM(CASE WHEN "Read" = 'True' 
         THEN 1 
         ELSE 0 
        END) AS logical_sum, 
       COUNT(*) AS cnt 
      FROM tbl 
     GROUP BY Parent_id) x