Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division between the sum of two columns

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'
           )
like image 529
Tone Avatar asked Jan 17 '13 06:01

Tone


People also ask

How do you find the sum of two columns?

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.

How do you use Isnull and sum?

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.

What is sum Isnull?

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.

Does sum work with NULL?

Yes its safe . You can use Sum without handling NULL Value.


1 Answers

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'  
like image 197
Florin stands with Ukraine Avatar answered Oct 04 '22 18:10

Florin stands with Ukraine