views:

43

answers:

3

I am using PostgreSQL.
I have the data in table like:

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

Now i want to summarize the data like :

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

Count would be the sum of the records.
Read would be the logical AND operation.

A: 
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
zerkms
+3  A: 
SELECT
    "Parent_id",
    SUM("Count"),
    bool_and("Read")
FROM
    tablename
GROUP BY
    "Parent_id";

I did use double quotes " because of illegal column names and the use of upper case in the names.

Frank Heikens
which DBMS'es support it? never heard of it and google doesn't help.
zerkms
PostgreSQL: http://www.postgresql.org/docs/current/interactive/functions-aggregate.html And now I see this topic is tagged for other dbms's as well...
Frank Heikens
+1  A: 

SELECT SUM(COUNT), MIN(READ) FROM TABLE GROUP BY PARENT_ID

vc 74
+1 for tricky `MIN()`
zerkms
MIN is not going to work in PostgreSQL: function min(boolean) does not exist. You have to create it before.
Frank Heikens
Sorry, I thought Read was a string
vc 74