Answer the question
In order to leave comments, you need to log in
How can you optimize a query with COUNT and SUM in PostgreSQL?
Hello comrades! Please help me figure it out. I use a PostgreSQL
database in my project :
PostgreSQL 11.0 (Debian 11.0-1.pgdg90+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
| SURVEY_ID (uuid) | EMPLOYEE (varchar) | STATUS (bool) | SEND (bool) | TREE_ORGANIZATION_ID (varchar) |
|--------------------------------------|------------------------|---------------|-------------|--------------------------------|
| bc90de33-62f9-4c6f-a7a6-6a76abb28b65 | [email protected] | true | true | \27623\27734\28304 |
| bc90de33-62f9-4c6f-a7a6-6a76abb28b65 | [email protected] | true | true | \27623\30556\30453 |
| bc90de33-62f9-4c6f-a7a6-6a76abb28b65 | [email protected] | true | true | \27623\30558\30353\30354\28233 |
CREATE INDEX INDEX_SURVEYS_EMPLOYEES_RELATIONSHIP ON SURVEYS_EMPLOYEES_RELATIONSHIP(SURVEY_ID, EMPLOYEE, STATUS, SEND, TREE_ORGANIZATION_ID);
SELECT
NULLIF(COUNT(*), 0) AS TOTAL_RESPONDENTS,
SUM(CASE WHEN SURVEYS_EMPLOYEES_RELATIONSHIP.SEND = TRUE AND SURVEYS_EMPLOYEES_RELATIONSHIP.STATUS = TRUE THEN 1 ELSE 0 END) AS PAST_RESPONDENTS
FROM
SURVEYS_EMPLOYEES_RELATIONSHIP
WHERE
SURVEYS_EMPLOYEES_RELATIONSHIP.SURVEY_ID = 'bc90de33-62f9-4c6f-a7a6-6a76abb28b65'
AND
SURVEYS_EMPLOYEES_RELATIONSHIP.TREE_ORGANIZATION_ID LIKE CONCAT('%', '\', '27623', '%')
Answer the question
In order to leave comments, you need to log in
PostgreSQL 11.0 (Debian 11.0-1.pgdg90+2)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question