Quote:
|
Originally Posted by cy163
However, I got wrong number for 'Total'. Why?
|
why?
i'll tell you why
because once again you have
faked your real problem
here is the answer to the fake problem you posted
the detailed calculations look like this:
Code:
SELECT tb1.Name
, CASE WHEN tb1.Dept='Sale' THEN 0.67
WHEN tb1.Dept='Finance' THEN 0.88
ELSE 1 END AS rate
, tb1.Salary
, COALESCE(tb2.Bonus,0) AS bonus
, CASE WHEN tb1.Dept='Sale' THEN 0.67
WHEN tb1.Dept='Finance' THEN 0.88
ELSE 1 END
* ( tb1.Salary
+ COALESCE(tb2.Bonus,0) ) AS Subtotal
FROM tb1
LEFT OUTER
JOIN tb2
ON tb2.Name = tb1.Name
Name rate Salary bonus Subtotal
Bob 0.67 1000 100 737
Tom 0.88 2000 200 1936
John 0.67 3000 0 2010
Winson 0.67 4000 0 2680
and here are the totals:
Code:
SELECT tb1.Name
, SUM(
CASE WHEN tb1.Dept='Sale' THEN 0.67
WHEN tb1.Dept='Finance' THEN 0.88
ELSE 1 END
* ( tb1.Salary
+ COALESCE(tb2.Bonus,0) ) ) AS Total
FROM tb1
LEFT OUTER
JOIN tb2
ON tb2.Name = tb1.Name
GROUP
BY tb1.Name
ORDER
BY Total
Name Total
Bob 737
Tom 1936
John 2010
Winson 2680
so: the answer is, your query
does work correctly
have a happy holiday
