Skip to content
Advertisement

How to SUM columns in the same row?

I have a table like below:

How the table works is that pymt_no1 corresponds to pymt_amt1, etc…

What I am trying to do is, total up the pymt_amt columns based on a CASE statement where the pymt_no is between a range of values.

For example, I am trying to get the total pymt_amt where any of the pymt_no is between 100 and 150. So in this case for the table above, only pymt_no1 and pymt_no3 have a value of between 100 and 150, so I need to total the pymt_amt that corresponds to it, so the total here would be 140.

I have tried the query below, but my issue is that it is only pulling the first pymt_amt only and not the rest for me to add together:

My thought behind this process for the sample above was to for example in the cte, for it to look something like this:

And then with this stored in the cte, I could SUM the Amount column and group by the id column to get the correct total.

Any help with this is greatly appreciated.

Advertisement

Answer

You need to add case for each column

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement