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
這是效果在Excel中:
感謝
感謝您的回答理查德,但我只是試過這個,它不工作,它只是產生24.412407的十進制結果?有任何想法嗎? – Will 2013-04-24 10:20:45
我強調了Excel的AVERAGE產生一個實數(浮點數)的事實,但SQL Server將遵循基本列的類型。我已經評論說你還有其他問題。在不顯示所有數據的情況下,無法檢查。有很多方法可以將數據從SQL Server複製到Excel。 – RichardTheKiwi 2013-04-24 10:22:20
謝謝,excel數據是從我原來的問題中查詢的一個細微變化產生的,所以數據應該是準確的。 – Will 2013-04-24 10:32:23