Skip to content
Advertisement

How to SUM columns in the same row?

I have a table like below:

id      pymt_no1     pymt_no2      pymt_no3    pymt_no4     pymt_amt1     pymt_amt2       pymt_amt3    pymt_amt4
25      100          5             150           50             60           70            80              90

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:

with pymt as (
SELECT 
    id,
    CASE
        WHEN pymt_no1 BETWEEN 100 and 150 THEN pymt_amt01
        WHEN pymt_no2 BETWEEN 100 and 150 THEN pymt_amt02
        WHEN pymt_no3 BETWEEN 100 and 150 THEN pymt_amt03
        WHEN pymt_no4 BETWEEN 100 and 150 THEN pymt_amt04
        END AS [Amount]
FROM pymt
)


SELECT 
    id,
    SUM(Total) as [Total]
FROM pymt
GROUP BY id

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

id     Amount
25       60
25       80  

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

--Sample table and Data 
CREATE TABLE #temp (
    id INT ,
    pymt_no1 int,
    pymt_no2 INT,
    pymt_no3 INT,
    pymt_no4 INT,
    pymt_amt1 INT,
    pymt_amt2 INT,
     pymt_amt3 INT,
     pymt_amt4 INT)

 INSERT #temp (   id ,
                  pymt_no1 ,
                  pymt_no2 ,
                  pymt_no3 ,
                  pymt_no4 ,
                  pymt_amt1 ,
                  pymt_amt2 ,
                  pymt_amt3 ,
                  pymt_amt4
              )
 VALUES (   25 , -- id - int
       100 , -- pymt_no1 - int
       5 , -- pymt_no2 - int
       150 , -- pymt_no3 - int
       50 , -- pymt_no4 - int
       60 , -- pymt_amt1 - int
       70 , -- pymt_amt2 - int
       80 , -- pymt_amt3 - int
       90   -- pymt_amt4 - int
   )



--Solution
with pymt as (
SELECT 
id,
 CASE  WHEN pymt_no1 BETWEEN 100 and 150 THEN pymt_amt1 ELSE 0 END AS AmOUNT1 ,
 CASE  WHEN pymt_no2 BETWEEN 100 and 150 THEN pymt_amt2  ELSE 0 END AS AmOUNT2 ,
 CASE  WHEN pymt_no3 BETWEEN 100 and 150 THEN pymt_amt3  ELSE 0 END AS Amount3 ,
 CASE  WHEN pymt_no4 BETWEEN 100 and 150 THEN pymt_amt4  ELSE 0 END AS Amount4 

 FROM #temp
 )


SELECT 
  id,
  SUM([Amount1] + pymt.AmOUNT2 + pymt.Amount3 + pymt.Amount4) as [Total]
 FROM pymt
 GROUP BY id
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement