2013-04-24 60 views
1

我想在SQL服務器上顯示平均值,但是當我在Excel中測試數據時結果不一樣,必須有一些明顯的我缺少的東西。SQL Server AVG和Excel AVERAGE產生不同的結果?

下面是從SQL Server的代碼和結果:

SELECT DISTINCT 
d.d_reference + ' - ' + d.d_name AS Faculty, 
AVG(sub.GroupSize) AS FacultyAverage 


FROM 
unitesnapshot.dbo.capd_register r 
INNER JOIN unitesnapshot.dbo.capd_studentregister sr ON sr.sr_register = r.r_id 
INNER JOIN unitesnapshot.dbo.capd_activity a ON a.a_register = r.r_id 
INNER JOIN unitesnapshot.dbo.capd_moduleactivity ma ON ma.ma_activity = a.a_id 
INNER JOIN unitesnapshot.dbo.capd_module m ON m.m_id = ma.ma_activitymodule 
INNER JOIN unitesnapshot.dbo.capd_department d ON d.d_id = m.m_moduledept 
INNER JOIN unitesnapshot.dbo.capd_section sec ON sec.s_id = m.m_modulesection 
INNER JOIN (SELECT 
      r.r_reference, 
      COUNT(DISTINCT s.s_studentreference) AS GroupSize 
      FROM 
      unitesnapshot.dbo.capd_student s 
      INNER JOIN unitesnapshot.dbo.capd_person p ON p.p_id = s.s_id 
      INNER JOIN unitesnapshot.dbo.capd_studentregister sr ON sr.sr_student = p.p_id 
      INNER JOIN unitesnapshot.dbo.capd_register r ON r.r_id = sr.sr_register 
      GROUP BY 
      r.r_reference) sub ON sub.r_reference = r.r_reference 

WHERE 
SUBSTRING(r.r_reference,4,2) = '12' AND 
d.d_reference = '730' 

GROUP BY 
d.d_reference, 
d.d_name 

enter image description here

這是效果在Excel中:

enter image description here

感謝

回答

3

嘗試此好玩:

select avg(a) 
from 
    (values(1),(2),(3),(4)) x(a); 

avg(a) 
------- 
2 

AVG()返回與基列相同的數據類型。如果您的列的類型爲int,那麼結果也會被截斷爲int。以下返回「正確」的結果。

select avg(cast(a as decimal(10,5))) 
from 
    (values(1),(2),(3),(4)) x(a); 

result 
-------- 
2.5 

你呈現的差異(24 VS 19.50484)將最有可能涉及與此相伴隨的另一個錯誤。例如,要檢查您是否在Excel中總結了與SQL Server中相同的數據,請將此結果轉儲到Excel中並對其進行總結。如果它與您目前認爲的SQL Server數據的Excel 等效的不匹配,請將列對齊並檢查它們是否具有相同的行數。然後按值ASCENDING分別對每列進行排序並再次比較。

SELECT d.d_name, sub.GroupSize AS FacultyAverage 
FROM unitesnapshot.dbo.capd_register r 
INNER JOIN unitesnapshot.dbo.capd_studentregister sr ON sr.sr_register = r.r_id 
INNER JOIN unitesnapshot.dbo.capd_activity a ON a.a_register = r.r_id 
INNER JOIN unitesnapshot.dbo.capd_moduleactivity ma ON ma.ma_activity = a.a_id 
INNER JOIN unitesnapshot.dbo.capd_module m ON m.m_id = ma.ma_activitymodule 
INNER JOIN unitesnapshot.dbo.capd_department d ON d.d_id = m.m_moduledept 
INNER JOIN unitesnapshot.dbo.capd_section sec ON sec.s_id = m.m_modulesection 
INNER JOIN (SELECT r.r_reference, 
      COUNT(DISTINCT s.s_studentreference) AS GroupSize 
      FROM unitesnapshot.dbo.capd_student s 
      INNER JOIN unitesnapshot.dbo.capd_person p ON p.p_id = s.s_id 
      INNER JOIN unitesnapshot.dbo.capd_studentregister sr ON sr.sr_student = p.p_id 
      INNER JOIN unitesnapshot.dbo.capd_register r ON r.r_id = sr.sr_register 
      GROUP BY r.r_reference) sub ON sub.r_reference = r.r_reference 
WHERE SUBSTRING(r.r_reference,4,2) = '12' AND d.d_reference = '730' 
ORDER BY d.d_name 
+0

感謝您的回答理查德,但我只是試過這個,它不工作,它只是產生24.412407的十進制結果?有任何想法嗎? – Will 2013-04-24 10:20:45

+0

我強調了Excel的AVERAGE產生一個實數(浮點數)的事實,但SQL Server將遵循基本列的類型。我已經評論說你還有其他問題。在不顯示所有數據的情況下,無法檢查。有很多方法可以將數據從SQL Server複製到Excel。 – RichardTheKiwi 2013-04-24 10:22:20

+0

謝謝,excel數據是從我原來的問題中查詢的一個細微變化產生的,所以數據應該是準確的。 – Will 2013-04-24 10:32:23