I need to take the sum of two columns and divide the totals. Both columns are of decimal data type. Here is what I have so far which does not work.
SELECT total_salary / DistEnroll
FROM dbo.vw_Salary
WHERE DistEnroll = (
SELECT DISTINCT(distenroll)
FROM dbo.vw_Salary
WHERE YEAR = '2012'
AND dist_name = 'Sch Dist'
)
AND total_salary = (
SELECT SUM(total_salary)
FROM [vw_salary]
WHERE YEAR = '2012'
AND dist_name = 'Sch Dist'
)
If you need to sum a column or row of numbers, let Excel do the math for you. Select a cell next to the numbers you want to sum, click AutoSum on the Home tab, press Enter, and you're done. When you click AutoSum, Excel automatically enters a formula (that uses the SUM function) to sum the numbers. Here's an example.
You wrap the SUM with an ISNULL, ISNULL(Sum(Ac_Billbook. CostsNet), 0) because SUM ignores nulls when summing or you can reverse the SUM and ISNULL like this, Sum(ISNULL(Ac_Billbook. CostsNet, 0)) which will convert individual NULL's to 0. Doesn't really matter which one you do.
SELECT SUM(ISNULL(Salary, 10000) AS Salary FROM Employee; Output: In MySQL, ISNULL() function is used to test whether an expression is NULL or not. If the expression is NULL it returns TRUE, else FALSE.
Yes its safe . You can use Sum without handling NULL Value.
select
( select sum(total_salary)
from [vw_salary]
where year = '2012'
and dist_name = 'Sch Dist') --totalsal
/
(select count(distinct distenroll)
from dbo.vw_Salary
where year = '2012'
and dist_name = 'Sch Dist') -- DistEnroll
or, better:
select sum(total_salary) / count(distinct distenroll)
from [vw_salary]
where year = '2012' and
dist_name = 'Sch Dist'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With