Avadhesh
Avadhesh

Reputation: 4703

Summarize the result of the table

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.

Upvotes: 1

Views: 120

Answers (2)

Frank Heikens
Frank Heikens

Reputation: 126991

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.

Upvotes: 3

zerkms
zerkms

Reputation: 254886

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

Upvotes: 0

Related Questions