1

I am trying to figure a way to sum a column based on conditions are create a new column with the aggregated values.

enter image description here

I need to sum column D based based on column condition from Column A, B and C. Value 1 in column A is the exception. The output that is required is as follows -

enter image description here

Where a new column E is created that sums Column D(2958673+2166646) by the condition of Column A(10,12) and Column B(20) and Column C(3) in Row 1. Similarly, Column D(1799504) by the condition of Column A(12) and Column B(20) and Column C(4) in Row 2

1 Answer 1

2

This is perhaps a comment, but it is too long.

I simply do not follow the logic that you want to implement. Your results can be produced by a simple query with filtering:

select a, b, c, d as d, d as e
from t
where a = 1;

EDIT:

Perhaps this is what you want?

select 1, b, c,
       sum(d) filter (where a = 1) as d,
       sum(d) filter (where a in (10, 12)) as e
from t
group by b, c;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.