2015-09-29 32 views
-7

這是我的表使用和操作

Companyid CompanyName Emp NAME role salary bonus others 
    1   comp1 smith  ed  120 180  null 
    1   comp1 raj  chairman 120 180  null 
    1   comp1 ravi   sd  140 null null 
    1   comp1 guru   bd  40 null 40 
    1   comp1 tech  director null null null 
    1   comp1 mahi  member null null null 
    1   comp1 sagi  ceo  null null null 

我需要輸出這樣的.sum工資獎金螞蟻他人的三列

CompanyName Total Remunaration 
comp1   820 
+6

那麼,你已經顯示了你的源和期望的結果。你試圖達到這個結果怎麼樣?你有什麼嘗試?你的嘗試出了什麼問題? –

回答

0

全總和我認爲你需要這個查詢:

SELECT CompanyName, SUM(COALESCE(salary, 0) + COALESCE(bonus, 0) + COALESCE(others, 0)) AS TotalRemunaration 
FROM yourTable 
GROUP BY CompanyName 

我使用COALESCE(fieldName, 0)這將返回0時val fieldName的ue爲空。