I have a query aggregating three result columns:
pending_with_documents
pending_without_documents
inprocessing
SELECT COUNT(DISTINCT (CASE
WHEN status_code IN ('ACK', 'INT') THEN
CASE
WHEN ack_no IN
(SELECT ack_no FROM bescom_appl_upload_doc) THEN ack_no END
END)) AS pending_with_documents,
COUNT(DISTINCT (CASE
WHEN status_code IN ('ACK', 'INT') THEN
CASE
WHEN ack_no NOT IN
(SELECT ack_no FROM bescom_appl_upload_doc) THEN ack_no END
END)) AS pending_without_documents,
COUNT(CASE
WHEN status_code NOT IN ('ACK', 'INT', 'APR', 'REJ', 'SBK', 'OBJ') THEN
ack_no
END) AS Inprocessing
FROM application_ht_install
WHERE service_code IN (36);
Now I need a fourth column total
summing up these three. So I tried:
SELECT COUNT(DISTINCT (CASE
WHEN status_code IN ('ACK', 'INT') THEN
CASE
WHEN ack_no IN
(SELECT ack_no FROM bescom_appl_upload_doc) THEN ack_no END
END)) AS pending_with_documents,
COUNT(DISTINCT (CASE
WHEN status_code IN ('ACK', 'INT') THEN
CASE
WHEN ack_no NOT IN
(SELECT ack_no FROM bescom_appl_upload_doc) THEN ack_no END
END)) AS pending_without_documents,
COUNT(CASE
WHEN status_code NOT IN ('ACK', 'INT', 'APR', 'REJ', 'SBK', 'OBJ')
THEN ack_no END) AS Inprocessing
SUM((COUNT(DISTINCT(CASE WHEN status_code IN ('ACK','INT') THEN
CASE WHEN ack_no IN
(SELECT ack_no FROM bescom_appl_upload_doc) THEN ack_no END
END)) AS pending_with_documents )+ ( COUNT(DISTINCT(CASE WHEN status_code IN ('ACK','INT') THEN
CASE WHEN ack_no NOT IN
(SELECT ack_no FROM bescom_appl_upload_doc) THEN ack_no END
END)) AS pending_without_documents)+ ( COUNT(CASE WHEN status_code NOT IN('ACK','INT','APR','REJ','SBK','OBJ') THEN
ack_no
END) AS Inprocessing))
FROM application_ht_install
WHERE service_code IN (36);
But I'm getting an error:
ERROR: syntax error at or near "sum" LINE 13: sum((count(distinct(case when status_code in ('ACK','INT') t... ^ SQL state: 42601 Character: 473
How to add up these three derived columns?
You have several syntax errors in your statement. You were aliasing things that didn't need aliasing. Here's the code with the syntax errors corrected. Note: One column had no alias so I called it "do_not_know_name" because I cannot tell what you meant.