简体   繁体   中英

Add the value of two columns in sql

I have a code that something like the one below

SELECT SUM(points_1) AS total_points_1, SUM(points_2) AS total_points_2,

       total_points_1 +
       CASE 
         WHEN total_points_1 BETWEEN 50 AND 100 THEN 50
         WHEN total_points_1 BETWEEN 100 AND 200 THEN 100
         WHEN total_points_1 BETWEEN 200 AND 300 THEN 200
         WHEN total_points_1 BETWEEN 300 AND 400 THEN 300
         ELSE 500
       END AS another_row_1,

       total_points_2 +
       CASE 
         WHEN total_points_2 BETWEEN 50 AND 100 THEN 50
         WHEN total_points_2 BETWEEN 100 AND 200 THEN 100
         WHEN total_points_2 BETWEEN 200 AND 300 THEN 200
         WHEN total_points_2 BETWEEN 300 AND 400 THEN 300
         ELSE 500
       END AS another_row_2,

FROM  `table`

My goal is to add the result of another_row_1 and another_row_2

I've tried doing the line below right after the END AS another_row_2, but did not work

SUM(another_row_1 + another_row_2) AS total_sum

First of all to decrease the clutter and improve maintainability I'd suggest to wrap point recalculation logic (the one that implemented with CASE ) into a function

CREATE FUNCTION add_points(_points INT)
RETURNS INT
RETURN 
  _points + 
  CASE 
    WHEN _points BETWEEN  50 AND  99 THEN 50
    WHEN _points BETWEEN 100 AND 199 THEN 100
    WHEN _points BETWEEN 200 AND 299 THEN 200
    WHEN _points BETWEEN 300 AND 399 THEN 300
    ELSE 500
  END;

Note: you might need to change the data type from INT to DECIMAL(n,m) (according to your actual data type of columns points_1 and points_2 )

Now the query would look like

SELECT total_points_1,
       total_points_2,
       another_total_1,
       another_total_2,
       another_total_1 + another_total_2 total_sum
  FROM
(
  SELECT SUM(points_1) total_points_1, 
         SUM(points_2) total_points_2,
         add_points(SUM(points_1)) another_total_1,
         add_points(SUM(points_2)) another_total_2
    FROM table1
) q

Sample output:

| TOTAL_POINTS_1 | TOTAL_POINTS_2 | ANOTHER_TOTAL_1 | ANOTHER_TOTAL_2 | TOTAL_SUM |
|----------------|----------------|-----------------|-----------------|-----------|
|            165 |            386 |             265 |             686 |       951 |

Here is SQLFiddle demo

You have to put it in an inner query

SELECT (res.another_row_1 + res.another_row_2) AS total_sum FROM
(
SELECT SUM(points_1) AS total_points_1, SUM(points_2) AS total_points_2,

       total_points_1 +
       CASE 
         WHEN total_points_1 BETWEEN 50 AND 100 THEN 50
         WHEN total_points_1 BETWEEN 100 AND 200 THEN 100
         WHEN total_points_1 BETWEEN 200 AND 300 THEN 200
         WHEN total_points_1 BETWEEN 300 AND 400 THEN 300
         ELSE 500
       END AS another_row_1,

       total_points_2 +
       CASE 
         WHEN total_points_2 BETWEEN 50 AND 100 THEN 50
         WHEN total_points_2 BETWEEN 100 AND 200 THEN 100
         WHEN total_points_2 BETWEEN 200 AND 300 THEN 200
         WHEN total_points_2 BETWEEN 300 AND 400 THEN 300
         ELSE 500
       END AS another_row_2,

FROM  `table`
) AS res

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM