类别:

聚合函数 (通用)、窗口函数 (通用、窗口框架)

STDDEV、STDDEV_SAMP

返回非 NULL 值的样本标准差(样本方差的平方根)。STDDEV 和 STDDEV_SAMP 是同一函数的别名。

也可以看看 STDDEV_POP,它返回总体标准差(方差的平方根)。

语法

聚合函数

{ STDDEV | STDDEV_SAMP } ( [ DISTINCT ] <expr1> )
Copy

窗口函数

{ STDDEV | STDDEV_SAMP } ( [ DISTINCT ] <expr1> ) OVER (
                                                       [ PARTITION BY <expr2> ]
                                                       [ ORDER BY <expr3> [ ASC | DESC ] [ <window_frame> ] ]
                                                       )
Copy

有关 window_frame 语法的详细信息,请参阅 窗口框架语法和用法

实参

expr1

计算结果为数值的表达式。这是计算标准偏差的表达式。

expr2

这是用于划分分区的可选表达式。

expr3

这是每个分区中作为排序规则的可选表达式。

返回

返回值的数据类型是 DOUBLE。

如果组内的所有记录都是 NULL,则此函数返回 NULL。

使用说明

  • 对于单条记录输入,STDDEV 和 STDDEV_SAMP 都返回 NULL。这与 Oracle 行为不同,在 Oracle 行为中,STDDEV_SAMP 为单条记录返回 NULL,并且 STDDEV 返回 0。

  • 传递 VARCHAR 表达式时,此函数会将输入隐式转换为浮点值。如果无法执行转换,则会返回错误。

  • 当此函数作为窗口函数调用且 OVER 子句包含 ORDER BY 子句时:

    • DISTINCT 关键字被禁止,并导致 SQL 编译错误。

    • 必须指定窗口框架。如果您未指定窗口框架,则使用隐式累积窗口框架:

      RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

    有关窗口框架的更多详细信息(包括语法和示例),请参阅 窗口框架语法和用法

    有关隐式窗口框架的更多信息,请参阅 窗口框架使用说明

聚合函数示例

以下示例计算了一小部分整数样本的标准差:

CREATE TABLE t1 (c1 INTEGER);
INSERT INTO t1 (c1) VALUES
    (6),
   (10),
   (14)
   ;
SELECT STDDEV(c1) FROM t1;
Copy
+----------+
| STDDEV() |
|----------|
|        4 |
+----------+

请注意,函数 STDDEV_SAMP 返回相同的结果:

SELECT STDDEV_SAMP(c1) FROM t1;
Copy
+-----------------+
| STDDEV_SAMP(C1) |
|-----------------|
|               4 |
+-----------------+

以下示例使用一个名为 menu_items 的小表格,其中列出了食品卡车所出售的物品。如果您想创建并加载此表,请参阅 创建并加载 menu_items 表

要找出销售货物成本(COGS)和 Dessert 行售价的样本标准差,请运行此查询:

SELECT menu_category, STDDEV(menu_cogs_usd) stddev_cogs, STDDEV(menu_price_usd) stddev_price
  FROM menu_items
  WHERE menu_category='Dessert'
  GROUP BY 1;
Copy
+---------------+-------------+--------------+
| MENU_CATEGORY | STDDEV_COGS | STDDEV_PRICE |
|---------------+-------------+--------------|
| Dessert       |  1.00519484 |  1.471960144 |
+---------------+-------------+--------------+

窗口函数示例

下面的例子也使用了 menu_items 表格(见 创建并加载 menu_items 表),但调用 STDDEV 函数用作窗口函数。

窗口函数按 menu_category 列对行进行分区。因此,每个类别都计算一次标准差,并且该值在组内的每一行结果中不断重复。在此示例中,行必须按菜单类别和销售货物成本进行分组。

SELECT menu_category, menu_cogs_usd,
    STDDEV(menu_cogs_usd) OVER(PARTITION BY menu_category) stddev_cogs
  FROM menu_items
  GROUP BY 1,2
  ORDER BY menu_category;
Copy

以下输出是此查询的部分结果集(前 15 行):

+---------------+---------------+--------------+
| MENU_CATEGORY | MENU_COGS_USD |  STDDEV_COGS |
|---------------+---------------+--------------|
| Beverage      |          0.50 | 0.1258305738 |
| Beverage      |          0.65 | 0.1258305738 |
| Beverage      |          0.75 | 0.1258305738 |
| Dessert       |          1.25 | 1.054751155  |
| Dessert       |          3.00 | 1.054751155  |
| Dessert       |          1.00 | 1.054751155  |
| Dessert       |          2.50 | 1.054751155  |
| Dessert       |          0.50 | 1.054751155  |
| Main          |          4.50 | 3.444051572  |
| Main          |          2.40 | 3.444051572  |
| Main          |          1.50 | 3.444051572  |
| Main          |         11.00 | 3.444051572  |
| Main          |          8.00 | 3.444051572  |
| Main          |          NULL | 3.444051572  |
| Main          |         12.00 | 3.444051572  |
...

创建并加载 menu_items 表

要在一些函数示例所使用的 menu_items 表中创建并插入行,请运行以下 SQL 命令。(该表包含 60 行。它基于 Tasty Bytes 示例数据库 (https://quickstarts.snowflake.com/guide/tasty_bytes_introduction/index.html#0) 中的 menu 表,但又与之不完全相同。

CREATE OR REPLACE TABLE menu_items(
  menu_id INT NOT NULL,
  menu_category VARCHAR(20),
  menu_item_name VARCHAR(50),
  menu_cogs_usd NUMBER(7,2),
  menu_price_usd NUMBER(7,2)
  );
Copy
INSERT INTO menu_items VALUES(1,'Beverage','Bottled Soda',0.500,3.00);
INSERT INTO menu_items VALUES(2,'Beverage','Bottled Water',0.500,2.00);
INSERT INTO menu_items VALUES(3,'Main','Breakfast Crepe',5.00,12.00);
INSERT INTO menu_items VALUES(4,'Main','Buffalo Mac & Cheese',6.00,10.00);
INSERT INTO menu_items VALUES(5,'Main','Chicago Dog',4.00,9.00);
INSERT INTO menu_items VALUES(6,'Main','Chicken Burrito',3.2500,12.500);
INSERT INTO menu_items VALUES(7,'Main','Chicken Pot Pie Crepe',6.00,15.00);
INSERT INTO menu_items VALUES(8,'Main','Combination Curry',9.00,15.00);
INSERT INTO menu_items VALUES(9,'Main','Combo Fried Rice',5.00,11.00);
INSERT INTO menu_items VALUES(10,'Main','Combo Lo Mein',6.00,13.00);
INSERT INTO menu_items VALUES(11,'Main','Coney Dog',5.00,10.00);
INSERT INTO menu_items VALUES(12,'Main','Creamy Chicken Ramen',8.00,17.2500);
INSERT INTO menu_items VALUES(13,'Snack','Crepe Suzette',4.00,9.00);
INSERT INTO menu_items VALUES(14,'Main','Fish Burrito',3.7500,12.500);
INSERT INTO menu_items VALUES(15,'Snack','Fried Pickles',1.2500,6.00);
INSERT INTO menu_items VALUES(16,'Snack','Greek Salad',4.00,11.00);
INSERT INTO menu_items VALUES(17,'Main','Gyro Plate',8.00,12.00);
INSERT INTO menu_items VALUES(18,'Main','Hot Ham & Cheese',7.00,11.00);
INSERT INTO menu_items VALUES(19,'Dessert','Ice Cream Sandwich',1.00,4.00);
INSERT INTO menu_items VALUES(20,'Beverage','Iced Tea',0.7500,3.00);
INSERT INTO menu_items VALUES(21,'Main','Italian',6.00,11.00);
INSERT INTO menu_items VALUES(22,'Main','Lean Beef Tibs',6.00,13.00);
INSERT INTO menu_items VALUES(23,'Main','Lean Burrito Bowl',3.500,12.500);
INSERT INTO menu_items VALUES(24,'Main','Lean Chicken Tibs',5.00,11.00);
INSERT INTO menu_items VALUES(25,'Main','Lean Chicken Tikka Masala',10.00,17.00);
INSERT INTO menu_items VALUES(26,'Beverage','Lemonade',0.6500,3.500);
INSERT INTO menu_items VALUES(27,'Main','Lobster Mac & Cheese',10.00,15.00);
INSERT INTO menu_items VALUES(28,'Dessert','Mango Sticky Rice',1.2500,5.00);
INSERT INTO menu_items VALUES(29,'Main','Miss Piggie',2.600,6.00);
INSERT INTO menu_items VALUES(30,'Main','Mothers Favorite',4.500,12.00);
INSERT INTO menu_items VALUES(31,'Main','New York Dog',4.00,8.00);
INSERT INTO menu_items VALUES(32,'Main','Pastrami',8.00,11.00);
INSERT INTO menu_items VALUES(33,'Dessert','Popsicle',0.500,3.00);
INSERT INTO menu_items VALUES(34,'Main','Pulled Pork Sandwich',7.00,12.00);
INSERT INTO menu_items VALUES(35,'Main','Rack of Pork Ribs',11.2500,21.00);
INSERT INTO menu_items VALUES(36,'Snack','Seitan Buffalo Wings',4.00,7.00);
INSERT INTO menu_items VALUES(37,'Main','Spicy Miso Vegetable Ramen',7.00,17.2500);
INSERT INTO menu_items VALUES(38,'Snack','Spring Mix Salad',2.2500,6.00);
INSERT INTO menu_items VALUES(39,'Main','Standard Mac & Cheese',3.00,8.00);
INSERT INTO menu_items VALUES(40,'Dessert','Sugar Cone',2.500,6.00);
INSERT INTO menu_items VALUES(41,'Main','Tandoori Mixed Grill',11.00,18.00);
INSERT INTO menu_items VALUES(42,'Main','The Classic',4.00,12.00);
INSERT INTO menu_items VALUES(43,'Main','The King Combo',12.00,20.00);
INSERT INTO menu_items VALUES(44,'Main','The Kitchen Sink',6.00,14.00);
INSERT INTO menu_items VALUES(45,'Main','The Original',1.500,5.00);
INSERT INTO menu_items VALUES(46,'Main','The Ranch',2.400,6.00);
INSERT INTO menu_items VALUES(47,'Main','The Salad of All Salads',6.00,12.00);
INSERT INTO menu_items VALUES(48,'Main','Three Meat Plate',10.00,17.00);
INSERT INTO menu_items VALUES(49,'Main','Three Taco Combo Plate',7.00,11.00);
INSERT INTO menu_items VALUES(50,'Main','Tonkotsu Ramen',7.00,17.2500);
INSERT INTO menu_items VALUES(51,'Main','Two Meat Plate',9.00,14.00);
INSERT INTO menu_items VALUES(52,'Dessert','Two Scoop Bowl',3.00,7.00);
INSERT INTO menu_items VALUES(53,'Main','Two Taco Combo Plate',6.00,9.00);
INSERT INTO menu_items VALUES(54,'Main','Veggie Burger',5.00,9.00);
INSERT INTO menu_items VALUES(55,'Main','Veggie Combo',4.00,9.00);
INSERT INTO menu_items VALUES(56,'Main','Veggie Taco Bowl',6.00,10.00);
INSERT INTO menu_items VALUES(57,'Dessert','Waffle Cone',2.500,6.00);
INSERT INTO menu_items VALUES(58,'Main','Wonton Soup',2.00,6.00);
INSERT INTO menu_items VALUES(59,'Main','Mini Pizza',null,null);
INSERT INTO menu_items VALUES(60,'Main','Large Pizza',null,null);
Copy
语言: 中文