2014-02-17 123 views
0
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我如何取兩列的平均值並在第三列顯示。請幫助取兩列的平均值sql

+0

看看我的答案。要做第三欄,您可以簡單地進行相同的平均計算,而不考慮where子句中的性別。 – TheOneWhoPrograms

回答

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 
+0

請您可以在同一個查詢中說明我寫的是我的真實表格是Transaction – vini

+0

@vini:請參閱更新。 –

+0

加權平均值是多少?例如,如果有10個男性和100個女性。 – MatBailie

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 

提供所有記錄任MaleFemaleChildGenderTheOneWhoPrograms's answer是一個更直接的解決方案。

+0

感謝您參考我的答案。我已經修改了我的答案,以考慮他的桌子不僅僅是男性還是女性。 – TheOneWhoPrograms

+0

爲什麼不使用avg函數? – vini

+0

@vini假設你已經有20個男孩,都是10歲,30個女孩,都是5歲,在你的數據庫中。 '(平均(男孩)+總和(女孩))/(總數(男孩)+總數(女孩))(平均(男孩)+平均(女孩))/ 2 =(10 + 5)/ 2 = 7.5' )=(200 + 150)/(20 + 30)= 7'。與前者不同,後者的計算考慮到女孩多於男孩的事實,導致平均年齡較低。看到不同?沒有*正確*或*錯誤*,有兩種不同的公式,由您決定哪一個是您需要的。 –

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 
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 
2

一有用的一條知識是從集合中排除NULL

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

以下使用CASENULLify某些記錄然後可以簡化事情。

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