Reputation: 1
I have a Table
EmpName | Bonus
Rohit | 20
Steve | 35
Rohit | 9
Johnson | 15
Now i want the user name and the sum bonus of distinct Employee who have bonus <30. ANSWER should be;
EmpName | Sum(Bonus)
Rohit | 29
Johnson | 15
What will be the Query to get this output?
Upvotes: 0
Views: 45
Reputation: 460058
SELECT EmpName,
SUM(Bonus) AS SumBonus
FROM dbo.Table1
WHERE Bonus < 30
GROUP BY EmpName
Upvotes: 1
Reputation: 247670
You can use an aggregate function sum()
to get the Total Bonus for each empname
and then use a HAVING
clause to return the empname
that with a Total Bonus of less than 30:
select empname, sum(bonus) TotalBonus
from yt
group by empname
having sum(bonus) < 30;
Or if you want to only total those rows where the bonus
is less than 30, you can use:
select empname, sum(bonus) TotalBonus
from yt
where bonus < 30
group by empname;
Upvotes: 2