Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

SQL VARP Function

by suresh

The SQL VARP Function is a SQL Aggregate Function that calculates the Statistical Variance for the population of total rows selected by the SQL SELECT Statement. The syntax of the SQL Server VARP Function is

SELECT VARP ([Column_Name])
FROM [Source]

TIP: SQL VARP Function only work on Numeric Columns, and it ignores Nulls.

SQL VARP Function Formula

The math formulas behind the VARP function to calculate the Statistical Variance for the population in SQL Server is

--Calculating the Mean or Average
Mean = Sum of each individual/Total number of items

--Calculating the Statistical Variance
Variance = ((OriginalValue – Mean)² + (OriginalValue – Mean)² +.... )/Total number of items

For this VARP Function, We use the below shown data

SQL VARP FUNCTION

SQL VARP Example

The VARP function returns the Variance for the population of the total number of records present in the specified column. For example, the following query will calculate the variance of total records present in the [Yearly Income] column from the Customers table.

SELECT VARP ([YearlyIncome]) AS [Income Variance]    
  
  FROM [Customer]

OUTPUT

SQL VARP FUNCTION 1

SQL VARP Function with Group By Clause

In most cases, we usually calculate the variance of products belongs to a particular category. In these situations, we use GROUP BY Clause to group the products by category. And then, use the VARP Function to calculate the variance for the population of products present in each group. Let us see the Example

SELECT [Occupation]
       ,VARP ([YearlyIncome]) AS [Income Variance]    
  FROM [Customer]
  GROUP BY [Occupation]

Above SQL Query will find, the Customers associated with the particular Department and calculates their income variance

OUTPUT

SQL VARP FUNCTION 2

ANALYSIS

We are taking the Skilled Manual profession and show you the output.

-- Calculating Mean
Mean = (60000 + 80000) / 2

Mean = 70000

--Calculating variance for Population
Variance for population = ( (60000 - 70000) + (80000 - 70000) ) / 2

Variance for population = 100000000

VARP Function in Having Clause

When we are grouping the data, In some cases, we usually check for the conditions against the aggregated data. In these circumstances, we use the SQL HAVING Clause along with Group By Statement. For instance, the following query group the Customers by their Occupation and then finds the Income variance of each group.

SELECT [Occupation]
       ,VARP ([YearlyIncome]) AS [Income Variance]    
 FROM [Customer]
 GROUP BY [Occupation]
 HAVING VARP ([YearlyIncome]) > 0

Below lines of code will check whether the aggregated amount (Variance for the population of Yearly income for each Group) is greater than 0 or not. If this is True, then the corresponding records displayed.

HAVING VARP ([YearlyIncome]) > 0

OUTPUT

SQL VARP FUNCTION 3

Placed Under: SQL

Trending Posts

SQL IDENTITY INSERT

Python Program to calculate Sum of Series 1²+2²+3²+….+n²

SQL SELECT INTO Statement

Java Program to find LCM of Two Numbers

SQL ERROR LINE

SQL Else If

Java endsWith Method

How to Create Pareto Chart in Tableau

SQL @@IDENTITY

SQL SET NOCOUNT ON

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy