日期:2014-05-16  浏览次数:20412 次

如何求两个聚合函数结果的差值
一个学生表:student (sNO, sName, mNO, sSex, sBirth, sNative, sHeight, sWeight, sEntime )
可以用聚合函数求全体学生的身高:select  avg(sHeight) as 平均身高 from student
也可以求符合某一条件的学生身高:select avg(sHeight) as 广州平均 from student where charindex('广州',sNative)>0
但我想用一条select 语句求出这两者之差,应该如何写?

------解决方案--------------------
select a.[平均身高]-b.[广州平均] from (select  avg(sHeight) as 平均身高 from student) a,(select avg(sHeight) as 广州平均 from student where charindex('广州',sNative)>0) b
------解决方案--------------------

select 
(select  avg(sHeight) as 平均身高 from student)-
(select avg(sHeight) as 广州平均 from student where charindex('广州',sNative)>0)

------解决方案--------------------
select sum(平均身高) 差值
from (
select  avg(sHeight) as 平均身高
 from student
union all
 select -1*avg(sHeight) 
 from student where charindex('广州',sNative)>0)a