简体   繁体   中英

How to sum the returned results in the same query?

How can I sum the sum-ed results (goal + delivered) in a new column, in the same query?

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 ....

You could just add a third column with both conditions:

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

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