Baljot Singh
Baljot Singh

Reputation: 143

How to add sum of two select columns in msql?

I have a report where i need to sum the result of two select columns .Tried various combinations but it is not working. This is my sql query

SELECT c.`ctv_name` AS locationbyctv ,
( SELECT IFNULL(SUM(1),0)  FROM 
  dedint_account_table d1,broadband_invoice b1
  WHERE d1.`account_id`=b1.`line_id` AND c.`ctv_id`=b1.`ctv_id` AND b1.`service_id` IN (12,29,82) AND d1.status='Activated'
    AND DATE(d1.activity_datetime)>='2019-10-15' AND  DATE(d1.activity_datetime)<='2019-11-30' )  AS a,

( SELECT  IFNULL(SUM(1),0) FROM 
  dedint_account_table d1,broadband_invoice b1,dedint_account_change_table dac 
  WHERE d1.`account_id`=b1.`line_id` AND c.`ctv_id`=b1.`ctv_id` AND b1.`service_id` IN (12,29,82) 
   AND dac.change_description='Activated-Account' AND dac.account_id=d1.id
    AND DATE(d1.activity_datetime)>='2019-10-15' AND  DATE(d1.activity_datetime)<='2019-11-30' ) AS b ,

FROM broadband_invoice b,broadband_ctv c,dedint_account_table d
 WHERE  
 c.`ctv_id`=b.`ctv_id` AND d.account_id=b.line_id
  GROUP BY c.`ctv_id`

And result is :

locationbyctv                 a       b      
----------------------------  ------  ------ 

SANTA ELENA TOWN            0       0      
VILLE VILLAGE               1       1               
PUNA GDA TOWN               2       1      
CORAL TOWN                  0       0      
PROSSO VILLAGE              0       3     
BEL CITY                    1       0    

Expected result is :

locationbyctv                 a+b           
    ----------------------------  

    SANTA ELENA TOWN            0             
    VILLE VILLAGE               2                      
    PUNA GDA TOWN               3             
    CORAL TOWN                  0             
    PROSSO VILLAGE              3            
    BEL CITY                    1           

Upvotes: 0

Views: 53

Answers (1)

user2342558
user2342558

Reputation: 6727

You can SUM two columns value with the + operator.

See the official documentation for more information.

Upvotes: 1

Related Questions