【问题标题】:Add the value of two columns in sqlsql中两列的值相加
【发布时间】:2014-01-02 09:05:49
【问题描述】:

我有一个类似下面的代码

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`

我的目标是将another_row_1another_row_2 的结果相加

我尝试在END AS another_row_2, 之后立即执行以下行,但没有成功

SUM(another_row_1 + another_row_2) AS total_sum

【问题讨论】:

  • another_row_1another_row_2 是列投影,而不是行。因此,您只需使用 + 运算符添加它们
  • 你打算怎么做?

标签: php mysql sql if-statement case


【解决方案1】:

首先,为了减少混乱并提高可维护性,我建议将点重新计算逻辑(使用 CASE 实现的逻辑)包装到一个函数中

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;

注意:您可能需要将数据类型从INT 更改为DECIMAL(n,m)(根据points_1points_2 列的实际数据类型)

现在查询看起来像

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

样本输出:

| TOTAL_POINTS_1 | TOTAL_POINTS_2 | ANOTHER_TOTAL_1 | ANOTHER_TOTAL_2 |总和 | |----------------|----------------|--------------- --|--------|------------| | 165 | 386 | 265 |第686章951 |

这里是SQLFiddle演示

【讨论】:

  • @user3135626 有帮助吗?您的问题需要更多帮助吗?
【解决方案2】:

你必须把它放在一个内部查询中

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

【讨论】:

  • 外部查询中的SUM() 函数中不需要,您必须为子查询(派生表)提供别名
  • @user3135626 就这些了,你还需要什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多