我有一个包含数据集的表,我将其分区为每组Zscore。我计算了集合的平均值。我计算了集合的STDEV。我计算了集合的ZScore。
然后每套我想在SELECT分区中添加一行“My Label”,[Set columns],[Calculated Partition Average + X * STDEV]
下面使用X = 1.5,表数据将包含分区的apple和orange数据
item value avg stdev
apple 1 2 0.81
apple 2 2 0.81
apple 3 2 0.81
my apple 3.2 null null
orange 2 4.6 2.49
orange 4 4.6 2.49
orange 8 4.6 2.49
my orange 8.4 null null
答案 0 :(得分:0)
(我以为你的意思是STDEVP,而不是STDEV) 你可以用这样的查询(包括样本数据创建 - 表@t)来做到这一点:
DECLARE @t TABLE(item VARCHAR(10), value FLOAT);
INSERT INTO @t(item, value)
VALUES('apple', 1),
('apple', 2),
('apple', 3),
('orange', 2),
('orange', 4),
('orange', 8);
DECLARE @factor FLOAT=1.5;
WITH
tmp AS (
SELECT item, value, AVG(value) OVER (PARTITION BY item) AS [Avg], STDEVP(value) OVER (PARTITION BY item) AS [StdevP]
FROM @t
UNION ALL
SELECT item, AVG(value)+(STDEVP(value)* @factor) AS [Value], NULL AS [avg], NULL AS [StdevP]
FROM @t
GROUP BY item
)
SELECT CASE WHEN [Avg] IS NULL THEN 'My ' ELSE '' END+item AS [Label], value, [Avg], [StdevP]
FROM tmp
ORDER BY item, CASE WHEN [Avg] IS NULL THEN 1 ELSE 0 END;