Query

查詢各公司工資總和

  • October 31, 2018

我們有兩個結構如下所示的表:

  • company (cid, cname)
  • emp(eid, ename, cid, salary)

查找 o/p 應該查詢如下?

cname salary  (sum of salary with particular company name)

表的樣本數據Comp

cid | cname
--- |-------
12  | It
13  | comp
14  | Mech

表的樣本數據Emp

eid |ename |cid |salary
----|------|----|-----
1   |xyz   |12  |300 ---]This sum of It company needs to add in sub total
2   |abc   |12  |400 ---] of 12.
3   |rst   |13  |500
4   |pqr   |14  |200

預期輸出:

cname |salary 
------|------
 It  |700
comp |500
Mech |200

我從您的查詢中了解到,您需要支付給所有公司員工的工資總和。這是解決方案

您還需要在 group by 子句之前的 where 子句中傳遞額外的過濾器參數

希望這會有所幫助

SELECT CName,SUM(Salary) AS SalaryPaidByCompany
FROM cmpny INNER JOIN emp ON cmpny.cid=emp.cid
GROUP BY Cname
SELECT c.cid,SUM(e.salary) as 'salary' from Comp c,Emp e 
where c.cid = e.cid
group by c.cid

引用自:https://dba.stackexchange.com/questions/159720