user2298919
user2298919

Reputation: 1

Sql Query to get the desired output

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

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460058

SELECT EmpName,
       SUM(Bonus) AS SumBonus
FROM dbo.Table1
WHERE Bonus < 30
GROUP BY EmpName

Demo

Upvotes: 1

Taryn
Taryn

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;

See SQL Fiddle with Demo

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;

See SQL Fiddle with Demo

Upvotes: 2

Related Questions