SQL 或 SSRS 中组内的标准差(或者 - 计算 Sigma 或 Cpk)

发布于 2024-12-12 07:06:45 字数 1514 浏览 0 评论 0原文

我在 SSRS/SQl 中计算子组内的标准偏差时遇到严重问题(以更方便的为准)。这背后的真正原因是我需要计算 Sigma 值(根据六西格码原则)或 Cpk 值(过程能力),但我的努力停留在标准差上。

我不擅长统计,但似乎SSRS(和SQL)中的StDevStDevP函数不是在“子组内”计算的,而且我找不到一个函数可以做到这一点。下面是一个 Excel 屏幕截图,显示了我想在 SQL/SSRS 中实现的计算步骤 编辑:尚无法发布图像,请在下面查找

在此处输入图像描述

棘手的部分是计算每对值之间的绝对差之和。给出值的顺序很重要。我忘了提及 Rbar 分母(第 7 行)中的数字 27 是样本大小减 1 (27)。下面也是 SQL 中的一个表:

DECLARE @Measurements TABLE(Val FLOAT)
INSERT INTO @Measurements (Val)(
    SELECT 485
    UNION ALL SELECT 490.6
    UNION ALL SELECT 490.6
    UNION ALL SELECT 485
    UNION ALL SELECT 485
    UNION ALL SELECT 489
    UNION ALL SELECT 485
    UNION ALL SELECT 477
    UNION ALL SELECT 477
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 485
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 485
    UNION ALL SELECT 485
    UNION ALL SELECT 477
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 482
    UNION ALL SELECT 482
    UNION ALL SELECT 482
)
SELECT STDEV(Val) FROM @Measurements 

我猜我需要的可以通过使用 RollingValue 或 SSRS 中的某些存储过程来实现。当然,如果有更智能的方法来计算 Sigma 或 Cpk,我会很高兴听到它。

我希望这个问题得到很好的阐述,如果没有,请发表评论。这对我来说非常重要,所以我将不胜感激任何帮助:) 谢谢!

I have a serious problem with calculating Standard Deviation within subgroup in SSRS/SQl (whichever is more convenient). The real reason behind this is I need to calculate Sigma value (according to Six Sigma principles) or alternatively Cpk value (process capability), but my efforts stop at the standard deviation.

I'm not strong with statistics but it seems that StDev and StDevP functions in SSRS (and SQL) are not calculated 'within subgroup', and I cannot find a function that could do that. Below is an excel screen shot showing the calculation steps which I would like to achieve in SQL/SSRS
Edit: Cannot post images yet, please find below

enter image description here

The tricky part is calculating the sum of absolute differences between each pair of values. The order in which the values are given is significant. I forgot to mention that number 27 in denominator of Rbar (Row 7) is the sample size minus 1 (27). Below is also a table in SQL:

DECLARE @Measurements TABLE(Val FLOAT)
INSERT INTO @Measurements (Val)(
    SELECT 485
    UNION ALL SELECT 490.6
    UNION ALL SELECT 490.6
    UNION ALL SELECT 485
    UNION ALL SELECT 485
    UNION ALL SELECT 489
    UNION ALL SELECT 485
    UNION ALL SELECT 477
    UNION ALL SELECT 477
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 485
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 485
    UNION ALL SELECT 485
    UNION ALL SELECT 477
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 484.6
    UNION ALL SELECT 482
    UNION ALL SELECT 482
    UNION ALL SELECT 482
)
SELECT STDEV(Val) FROM @Measurements 

I'm guessing that what I need could be achieved by using either RollingValue or some stored procedure in SSRS. Of course if there is a smarter way to calculate Sigma or Cpk I'll be very happy to hear about it.

I hope the question is well formulated, if not please comment. This is very important for me so I will be grateful for any assistance :) Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-12-19 07:06:46

我认为这应该能解决问题?不确定 27 的分母或 d2 值是如何计算的,因此现在已将它们硬编码...

在使用此查询方面,保留行顺序很重要,因此我已将这些值放入临时表中使用标识列 - 如果您的值来自表并且有固定的排序条件,请改用 row_number() OVER(按 criteria_column 排序)

IF OBJECT_ID('tempdb..#values') IS NOT NULL DROP TABLE #values
CREATE TABLE #values (row INT IDENTITY (1,1),val FLOAT)
INSERT INTO #values
    SELECT 485 as val
    UNION ALL SELECT 490.6 
    UNION ALL SELECT 490.6 
    UNION ALL SELECT 485 
    UNION ALL SELECT 485 
    UNION ALL SELECT 489 
    UNION ALL SELECT 485 
    UNION ALL SELECT 477 
    UNION ALL SELECT 477 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 485 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 485 
    UNION ALL SELECT 485 
    UNION ALL SELECT 477 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 482 
    UNION ALL SELECT 482 
    UNION ALL SELECT 482 



;with stdevs AS
(
SELECT 
v1.val
,ABS(v1.val - v2.val) as abs_diff
,v2.val as value2
from #values v1
LEFT OUTER JOIN #values v2
ON v2.row = v1.row + 1
)
SELECT
avg(val) as average_value
,sum(abs_diff) as abs_sum
,sum(abs_diff) / 27 as Rbar
,1.13 as d2
,(sum(abs_diff) / 27) / 1.13 as std_dev
FROM stdevs

I think this should do the trick? Not sure how the denominator of 27 was calculated or the d2 value, so have hard coded them in for now...

In terms of using this query it is important to preserve the row order, so I have whacked the values into a temp table using an identity column - if your values are coming from a table and there is a fixed criteria for ordering, use row_number() OVER (order by criteria_column) instead

IF OBJECT_ID('tempdb..#values') IS NOT NULL DROP TABLE #values
CREATE TABLE #values (row INT IDENTITY (1,1),val FLOAT)
INSERT INTO #values
    SELECT 485 as val
    UNION ALL SELECT 490.6 
    UNION ALL SELECT 490.6 
    UNION ALL SELECT 485 
    UNION ALL SELECT 485 
    UNION ALL SELECT 489 
    UNION ALL SELECT 485 
    UNION ALL SELECT 477 
    UNION ALL SELECT 477 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 485 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 485 
    UNION ALL SELECT 485 
    UNION ALL SELECT 477 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 484.6 
    UNION ALL SELECT 482 
    UNION ALL SELECT 482 
    UNION ALL SELECT 482 



;with stdevs AS
(
SELECT 
v1.val
,ABS(v1.val - v2.val) as abs_diff
,v2.val as value2
from #values v1
LEFT OUTER JOIN #values v2
ON v2.row = v1.row + 1
)
SELECT
avg(val) as average_value
,sum(abs_diff) as abs_sum
,sum(abs_diff) / 27 as Rbar
,1.13 as d2
,(sum(abs_diff) / 27) / 1.13 as std_dev
FROM stdevs
痴情 2024-12-19 07:06:46

您可以根据 SUMCOUNT 聚合函数计算标准差:

STDEVP(x) := SQRT((SUM(x^2) - SUM(x)^2/COUNT(x)) / COUNT(x))
STDEV(x) := SQRT((SUM(x^2) - SUM(x)^2/COUNT(x)) / (COUNT(x) - 1))

这不是最准确的方法,但很简单。

You can calculate the standard deviation in terms of the SUM and COUNT aggregate functions:

STDEVP(x) := SQRT((SUM(x^2) - SUM(x)^2/COUNT(x)) / COUNT(x))
STDEV(x) := SQRT((SUM(x^2) - SUM(x)^2/COUNT(x)) / (COUNT(x) - 1))

Not the most accurate way, but it's easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文