平均值的统计显着性?

时间:2013-08-09 18:48:26

标签: sql statistics

考虑一个投票系统。例如。用于汽车。

  • 10人认为给车A得分为70%。
  • 1000人认为给车A得分为60%。

因此,我们有值0.70.6您如何比较这些值?毫无疑问,1000票比<10>更重要 。我希望在SQL(使用AVG函数或类似函数)中有效地执行此操作。

这类问题应该有一个众所周知的公式。请帮忙!

1 个答案:

答案 0 :(得分:0)

好的,让我们数一数。我们所有人都有1010人,其中1000人得分为60分,10人得分为70分。

平均得分为:

(1000 * 60 + 10 * 70)/(1000 + 10) = 60,09

现在将它们放到表中并对其运行查询:

create table scores (cust_id int identity(1,1), car char(1), score float);
go

------------------------------------------------------
declare @i int = 0;

while @i < 1000 
begin
    insert into scores(car, score) values ('A', 60.0);
    set @i = @i + 1
end

while @i < 1010
begin
    insert into scores(car, score) values ('A', 70.0);
    set @i = @i + 1
end;

------------------------------------------------------
select car, avg(score) [score], count(cust_id) [people_count]
from scores
group by car

结果:

car score   people_count
------------------------
A   60,09   1010

SQLFiddle


更新

create function compare_scores (@n1 int, @sc1 float, @n2 int, @sc2 float)
returns varchar(10)
as
begin
    return case when (@n1 * @sc1) <= (@n2 * @sc2) then (case when (@n1 * @sc1) = (@n2 * @sc2) then 'EQUAL' else 'LESS' end) else 'GREATER' end
end

----------------------------------------------------------
select dbo.compare_scores(10, 10.0, 1000, 8.0) [result]
union all
select dbo.compare_scores(10, 10.0, 10, 10.0) [result]
union all
select dbo.compare_scores(1000, 10.0, 10, 8.0) [result]

结果:

result
-------
LESS
EQUAL
GREATER
相关问题