两列之和的划分

时间:2013-01-17 06:40:36

标签: sql aggregate division

我需要取两列的总和并除以总数。两列都是十进制数据类型。这是我到目前为止所做的不起作用。

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'
           )

1 个答案:

答案 0 :(得分:7)

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 

或者,更好:

        select sum(total_salary)  /  count(distinct distenroll) 
        from [vw_salary] 
        where year = '2012' and 
              dist_name = 'Sch Dist'