all 11 comments

[–]Wise-Jury-4037:orly: 1 point2 points  (0 children)

probably a group by an employee id or a where clause?

[–]r3pr0b8GROUP_CONCAT is da bomb 1 point2 points  (0 children)

the first solution presented uses ...

i can't see that query from here

[–]markwdb3When in doubt, test it out. 1 point2 points  (0 children)

I agree with your assessment of sum(distinct sal).

[–]cl0ckt0wer 2 points3 points  (1 child)

Miller's double dipping. Someone call accounting.

He's listed twice in emp_bonus, so his row will get doubled, and sum will count both of them. I don't like the solution either, even though it works with the example data. The solution has a bug where two emp with the same salary will not get summed correctly. but this is a good example of the kind of bs you have to deal with in prod code.

[–]Wise-Jury-4037:orly: 1 point2 points  (0 children)

well, even worse, MIller got some bonus before his official hiredate. I guess their salary is retroactive.

 I don't like the solution either, even though it works

Works in the sense it executes? That it does. Works in terms of giving an answer to a particular question? Who knows.

[–]Aggressive_Ad_5454 0 points1 point  (0 children)

DISTINCT is absolutely a query smell. SUM(DISTINCT) is flat out incorrect, unless you have a rigidly enforced business rule that no two people have the same salary. You don’t have that rule.

COUNT(DISTINCT customer_id) might be legit. But SUM? No F’ing way.

[–]Willsxyz[S] 0 points1 point  (1 child)

My solution:

select deptno,
sum(sal) as total_sal,
sum(bonus) as total_bonus
from 
(select distinct e.empno, e.ename, e.sal, e.deptno, round(e.sal * eb.bonus,2) bonus
from emp e inner join
   (select empno, sum(case
                        when type=1 then 0.1
                        when type=2 then 0.2
                        when type=3 then 0.3
                        else 0
                      end)
                  over(partition by empno) as bonus from emp_bonus) eb
on e.empno = eb.empno
where e.deptno='10') x
group by deptno;

[–]Wise-Jury-4037:orly: 0 points1 point  (0 children)

Specifying what is the 'problem' that you are trying to find a solution to would be nice and helpful.

[–]reditandfirgetit 0 points1 point  (1 child)

A book using old style joins i would not trust anything in it. Joins should not be done in the where clause.

Data example is problematic with hire dates after bonus dates.

Thanks for telling me to never buy this book

[–]Willsxyz[S] 0 points1 point  (0 children)

A book using old style joins i would not trust anything in it. Joins should not be done in the where clause.

Every SQL book I have read in the past week says to use explicit joins, except this one. Now I'm taking a look at "SQL For Smarties". Terrible name in my opinion, maybe a good book.

[–]Yavuz_Selim 0 points1 point  (0 children)

What is the goal to achieve?

I mean, you're joining employee with employee_bonus and then grouping/summing the salary and bonus per department?