VAR_SAMP

Returns the sample variance of values.

Note

Aliases to this function include VAR and VARIANCE for compatibility.

See also: VAR_POP

Syntax

-- As an aggregate
VAR_SAMP( expr )

VAR( expr )

VARIANCE( expr )

-- As a window function
VAR_SAMP ( expr ) OVER (
         [ PARTITION BY value_expression [, ...] ]
         [ ORDER BY value_expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
         [ frame_clause ]
      )

Arguments

Parameter

Description

expr

Numeric expression

Returns

Returns the sample variance with type DOUBLE.

Notes

  • When all rows contain NULL values, the function returns NULL.

  • The function also returns NULL when only one value is non-NULL.

Examples

For these examples, assume a table named nba, with the following structure:

CREATE TABLE nba
(
   "Name" text(40),
   "Team" text(40),
   "Number" tinyint,
   "Position" text(2),
   "Age" tinyint,
   "Height" text(4),
   "Weight" real,
   "College" text(40),
   "Salary" float
 );

Here’s a peek at the table contents (Download nba.csv):

nba.csv

Name

Team

Number

Position

Age

Height

Weight

College

Salary

Avery Bradley

Boston Celtics

0.0

PG

25.0

6-2

180.0

Texas

7730337.0

Jae Crowder

Boston Celtics

99.0

SF

25.0

6-6

235.0

Marquette

6796117.0

John Holland

Boston Celtics

30.0

SG

27.0

6-5

205.0

Boston University

R.J. Hunter

Boston Celtics

28.0

SG

22.0

6-5

185.0

Georgia State

1148640.0

Jonas Jerebko

Boston Celtics

8.0

PF

29.0

6-10

231.0

5000000.0

Amir Johnson

Boston Celtics

90.0

PF

29.0

6-9

240.0

12000000.0

Jordan Mickey

Boston Celtics

55.0

PF

21.0

6-8

235.0

LSU

1170960.0

Kelly Olynyk

Boston Celtics

41.0

C

25.0

7-0

238.0

Gonzaga

2165160.0

Terry Rozier

Boston Celtics

12.0

PG

22.0

6-2

190.0

Louisville

1824360.0

Simple variance

t=> SELECT "Team", VAR("Age") FROM nba GROUP BY 1 LIMIT 10;
Team                  | var_samp
----------------------+---------
Atlanta Hawks         |  17.8857
Boston Celtics        |   8.0667
Brooklyn Nets         |   9.1143
Charlotte Hornets     |    9.981
Chicago Bulls         |  17.5429
Cleveland Cavaliers   |   16.981
Dallas Mavericks      |   13.781
Denver Nuggets        |  22.4952
Detroit Pistons       |  19.7429
Golden State Warriors |  14.8095

Combine VAR with other aggregates

t=> SELECT "Age", AVG("Salary"), VAR("Salary"), VARP("Salary") FROM nba GROUP BY 1;
Age | avg      | var_samp           | var_pop
----+----------+--------------------+-------------------
 19 |  1930440 |        77933520000 |        38966760000
 20 |  2725790 |  2282859395272.472 | 2162708900784.4473
 21 |  2067379 | 1994733541375.9124 | 1889747565514.0222
 22 |  2357963 | 2302436984278.0986 |  2213881715652.018
 23 |  2034746 |  7443577634481.656 |  7252716669494.947
 24 |  3785300 |  23072496009900.81 | 22559773876347.457
 25 |  3930867 |  20779584044953.27 | 20307320771204.332
 26 |  6866566 | 37215743834431.336 |   36181973172363.8
 27 |  6676741 |  46676006374227.07 |  45509106214871.39
 28 |  5110188 |  18633264973532.18 |  18012156141081.11
 29 |  6224177 | 23723775390464.617 |  22845117042669.63
 30 |  7061858 |   29253707311827.5 | 28278583734766.582
 31 |  8511396 |  51411244177164.07 |  49074369441838.43
 32 |  7716958 |  55522406348186.13 |  51251452013710.28
 33 |  3930739 |  18959867656133.08 | 17605591394980.715
 34 |  7606030 | 31956804968680.668 |   28761124471812.6
 35 |  3461739 |  5591759623731.643 | 4892789670765.1875
 36 |  2238119 | 2402689863606.2666 |   2162420877245.64
 37 | 12777778 |    114814811851852 |  76543207901234.67
 38 |  1840041 | 2239993117998.6665 |      1679994838499
 39 |  2517872 |    4930720062924.5 |   2465360031462.25
 40 |  4666916 | 17267521020833.332 | 11511680680555.555