[英]How to exclude general criteria value from group by
I have two tables : 我有两个表:
One for employees data
Employee
一种用于员工数据的
Employee
Another for employee vacations balance
VacBal
另一个用于员工休假的余额
VacBal
Now i want to get the current vacation balance for each employee,considering if the emp_num = 0
then it's a general balance
for all employees not for specific employee 现在我想获取每个雇员的当前休假余额,考虑是否
emp_num = 0
那么这是所有雇员的general balance
,而不是特定雇员的
If Employee table Sample data like that :
如果Employee表是这样的样本数据:
emp_num name
546 john
134 Ramy
and 和
VacBal like that
像这样的VacBal
emp_num bal_num bal_date
546 1 5-5-2015
546 2 6-5-2015
134 1 7-5-2015
0 3 2-2-2015
0 4 6-1-2015
I want result set like this : 我想要这样的结果集:
emp_num name SUM(bal_num)
546 john 10 --->3+7
134 Ramy 8 --->1+7
How to do this ? 这个怎么做 ?
You can use a correlated sub-query to SUM
: 您可以对
SUM
使用相关的子查询:
select e.emp_num, e.name, (select sum(vb.bal_num) from VacBal vb
where vb.emp_num in (e.emp_num,0))
from Employee e
SELECT e.emp_num,
name,
Sum(bal_num) + (SELECT Sum(bal_num)
FROM vacbal vb
WHERE emp_num = 0)
FROM employee e
inner join vacbal v on e.emp_num=v.emp_num
GROUP BY e.emp_num,
name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.