7

我需要将两列的总和除以总数。两列都是十进制数据类型。这是我迄今为止所拥有的,但它不起作用。

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

1 回答 1

8
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'  
于 2013-01-17T06:45:37.097 回答