如何在同一查询中在新列中求和结果(目标+交付)?

SELECT Sum(CASE
             WHEN the_status = 'goal' THEN 1
             ELSE 0
           END) AS goal,
       Sum(CASE
             WHEN the_status = 'delivered' THEN 1
             ELSE 0
           END) AS delivered
FROM   the_data
where ....

最佳答案

您可以同时添加第三列并同时满足以下两个条件:

SELECT Sum(CASE
             WHEN the_status = 'goal' THEN 1
             ELSE 0
           END) AS goal,
       Sum(CASE
             WHEN the_status = 'delivered' THEN 1
             ELSE 0
           END) AS delivered,
       Sum(CASE
             WHEN the_status IN ('goal', 'delivered') THEN 1
             ELSE 0
           END) AS goal_and_delivered
FROM   the_data

关于php - 如何对同一查询中的返回结果求和?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47479747/

10-15 10:54