取两列sql的平均值

时间:2014-02-17 09:04:45

标签: sql sql-server

Select Age,

       (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Male') as avg_male,
       (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Female') as avg_female

from [dbo].[Transaction] t1

group by Age ORDER BY Age

嗨,我有两列avg_male和avg_female如何获取两列的平均值并显示在第三列中。请帮忙

5 个答案:

答案 0 :(得分:3)

只需使用简单的数学:

select avg_male
,      avg_female
,      ( avg_male + avg_female) / 2
from   transaction

编辑:

我认为这应该有效。没有SQLFiddle,但应该这样做:

Select Age
,      sum(case when ChildGender='Male' then ShoeSize else 0 end) / sum(case when ChildGender='Male' then 1 else 0 end) as avg_male,
,      sum(case when ChildGender='Female' then ShoeSize else 0 end) / sum(case when ChildGender='Female' then 1 else 0 end) as avg_male,
,      avg(ShoeSize) as avg_both
from   [dbo].[Transaction] t1
group
by     Age
ORDER
BY     Age

答案 1 :(得分:2)

使用(avg(x) + avg(y)) / 2,两列都具有相同的权重,即使它们没有相同数量的条目。如果您想获取两列中所有值的总算术平均值,则需要将它们相加并除以它们的总计数

SELECT (sum_male + sum_female) / (count_male + count_female)
    FROM transaction

SUMCOUNT聚合函数可帮助您计算这些值:

(select sum(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Male') as sum_male,
(select sum(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Female') as sum_female
(select count(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Male') as count_male,
(select count(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Female') as count_female

如果所有记录都是MaleFemale ChildGender TheOneWhoPrograms's answer是一个更直接的解决方案。

答案 2 :(得分:2)

以下内容应该有效。它与你做同样的事情,但是第三列没有考虑性别就做了平均值。

如果只有男性或女性的孩子

Select Age,

   (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and     t2.ChildGender='Male') as avg_male,
   (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Female') as avg_female
   (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age as avg_avg
from [dbo].[Transaction] t1

group by Age ORDER BY Age

如果有一些不是男性或女性,你只想要男性和女性的平均值

Select Age,

   (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and     t2.ChildGender='Male') as avg_male,
   (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age and t2.ChildGender='Female') as avg_female
   (select avg(ShoeSize) from [dbo].[Transaction] t2 where t2.age = t1.Age AND (t2.ChildGender='Male' OR t2.ChildGender='Female') as avg_avg
from [dbo].[Transaction] t1

group by Age ORDER BY Age

答案 3 :(得分:2)

一个有用的知识是NULL被排除在聚合之外。

这意味着AVG({1, 2, 3, NULL})为2. (1 + 2 + 3)/(3)

以下使用CASE NULLify 某些记录可以简化事情。

SELECT
  Age,
  AVG(CASE WHEN ChildGender = 'Male'   THEN ShoeSize ELSE NULL END)   AS avgMale,
  AVG(CASE WHEN ChildGender = 'Female' THEN ShoeSize ELSE NULL END)   AS avgFemale,
  AVG(                                      ShoeSize              )   AS avgAll
FROM
  [dbo].[Transaction]
GROUP BY
  Age

此技巧也适用于SUM()COUNT()

答案 4 :(得分:0)

;with CTE AS
(
select 0 avg_male ,avg(ShoeSize) avg_female from [Transaction] ChildGender='Female'
union
select avg(ShoeSize) avg_male ,0 avg_female from [Transaction] ChildGender='Male'
)
select 
SUM(avg_male) avg_male,SUM(avg_female),(SUM(avg_male) avg_male+SUM(avg_female))/2 Total
from CTE