SQL 两列之和之间的除法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14373466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:11:31 来源:igfitidea点击:
Division between the sum of two columns
提问by Tone
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'
)
回答by Florin Ghita
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'