SQL求在 95% percentile(置信区间)的数,用STDEV函数调试有误

目的是找出Item和Item description相同的类中,那些不在置信区间的数,打个比方如第三行的数很明显就不在置信区间。

置信区间的公式是= 平均数+/- 标准方差*95%

select b.Item, b.[Item description], b.AVG+0.95 * b.STD as UPLIMIT, b.AVG - 0.95*b.STD as LOWLIMIT from
(select Item, [Item description],average(1) as AVG,stedev(1) from ['TABLE'] AS STD
GROUP BY Item, [Item description]) b

WHERE LOWLIMIT<Weight<UPLIMIT
GROUP BY b.Item, b.[Item description]
上面代码的思路就是先按 Item和Item description分组,然后求出各组的平均值和平均方差,然后再用第一行的公式,求出上下范围,然后最后用where语句,找出在这个范围的值。

请问这里哪里不对呢?谢谢了。

--这是MSSQL的
 --1.建表
 Create Table [Table]
 (
    [Item]  Varchar(10),
    [Item description] Varchar(10),
    [Weight] int
 )
 --2.插入数据
insert into [Table] values('y12','screw',23)
insert into [Table] values('y12','screw',24)
insert into [Table] values('y12','screw',4)
insert into [Table] values('y12345','barrel',30)
--3.查询结果(y12345只有一条记录是没有标准偏差)
Select 
    A.[Item], 
    A.[Item description],  
    B.[LOWLIMIT],
    B.[UPLIMIT]
    From [Table] A 
    Inner Join 
    (
        Select 
            [Item],
            [Item description],
            Avg([Weight])+0.95*StDev([Weight]) As UPLIMIT,
            Avg([Weight])-0.95*StDev([Weight]) As LOWLIMIT
        From [TABLE] 
            GROUP BY [Item],[Item description]
    ) B On A.[Item]=B.[Item]
    And A.[Item description]=B.[Item description]
        WHERE A.[Weight] Between LOWLIMIT And UPLIMIT

温馨提示:答案为网友推荐,仅供参考
相似回答