SQL VAR_SAMP function
Sample variance: divides by n-1 and needs at least two non-NULL values.
- Категория
- Aggregates
- Returns
numeric- In this category
- 23 of 52
Синтаксис
VAR_SAMP(x) | VAR(x)
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Different name or syntax | VAR is the SQL Server spelling |
| PostgreSQL | Да | Supported under this name. |
| MySQL | Да | Supported under this name. |
| SQLite | Not built in | not built in; use an extension or SUM((x-avg)*(x-avg))/(COUNT(x)-1) |
| Oracle | Да | Supported under this name. |
Примеры
PostgreSQL
SELECT var_samp(price) FROM products WHERE category_id = 7;
SQL Server
SELECT VAR(price) FROM products WHERE category_id = 7;
Полезно знать
- One non-NULL value returns NULL; two or more divide by count-1.
- NULLs are skipped, so the denominator counts only present values.
- In SQLite the whole family is missing; an extension or a manual SUM((x-avg)*(x-avg))/(COUNT(x)-1) formula fills the gap.
Questions about VAR_SAMP
VAR_SAMP vs VAR_POP?
The divisor: n-1 versus n, and the one-row result: NULL versus 0.
What is VAR_SAMP in SQL Server?
VAR.
How do I get standard deviation from it?
Take the square root, or call STDDEV_SAMP directly.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.