MySQL 中的 VAR_SAMP()函数
MySQL 中的VAR_SAMP()函数用于计算表达式的样本方差。
句法 :
VAR_SAMP(expr);
参数:此方法只接受一个参数。
- expr :我们要从中计算样本方差的输入表达式。
返回:它返回样本方差。
示例-1:
使用 VAR_SAMP函数从给定的 Player 表中查找 RunScored 列的样本方差。
创建玩家表:
CREATE TABLE Player
(
PlayerId INT AUTO_INCREMENT,
PlayerName VARCHAR(100) NOT NULL,
RunScored INT NOT NULL,
WicketsTaken INT NOT NULL,
PRIMARY KEY(PlayerId)
);
插入数据到表中:
INSERT INTO Player
(PlayerName, RunScored, WicketsTaken )
VALUES
('
KL Rahul', 52, 0 ),
('Hardik Pandya', 30, 1 ),
('Ravindra Jadeja', 18, 2 ),
('Washington Sundar', 10, 1),
('D Chahar', 11, 2 ),
('Mitchell Starc', 0, 3);
要验证使用以下命令如下。
SELECT * from Player ;
输出 :
PLAYERID | PLAYERNAME | RUNSCORED | WICKETSTAKEN |
---|---|---|---|
1 | KL Rahul | 52 | 0 |
2 | Hardik Pandya | 30 | 1 |
3 | Ravindra Jadeja | 18 | 2 |
4 | Washington Sundar | 10 | 1 |
5 | D Chahar | 11 | 2 |
6 | Mitchell Starc | 0 | 3 |
现在我们要找到 RunScored 列的样本方差。
SELECT VAR_SAMP(RunScored ) as Run_Variance
FROM Player ;
输出 :
RUN_VARIANCE |
---|
341.7666666666667 |
示例 2 :
现在我们要找到 WicketsTaken 列的样本方差。
SELECT VAR_SAMP(WicketsTaken) as Wicket_Variance
FROM Player ;
输出 :
WICKET_VARIANCE |
---|
1.1 |
示例 3 :
在此示例中,我们将查找在公司“PQR Soln”中工作的员工收入的样本方差。为了演示创建一个名为 EmloyeeDetails 的表。
CREATE TABLE EmployeeDetails(
Employee_Id INT AUTO_INCREMENT,
Employee_Name VARCHAR(100) NOT NULL,
Working_At VARCHAR(20) NOT NULL,
Work_Location VARCHAR(20) NOT NULL,
Joining_Date DATE NOT NULL,
Annual_Income INT NOT NULL,
PRIMARY KEY(Employee_Id )
);
插入数据到表中:
INSERT INTO
EmployeeDetails(Employee_Name, Working_At, Work_Location, Joining_Date, Annual_Income )
VALUES
('Amit Khan', 'XYZ Digital', 'Kolkata', '2019-10-06', 350000 ),
('Shreetama Pal', 'ABC Corp.', 'Kolkata', '2018-12-16', 500000 ),
('Aniket Sharma', 'PQR Soln.', 'Delhi', '2020-01-11', 300000 ),
('Maitree Jana', 'XYZ Digital', 'Kolkata', '2019-05-01', 400000 ),
('Priyanka Ojha', 'ABC Corp.', 'Delhi', '2019-02-13', 350000 ),
('Sayani Mitra', 'XYZ Digital', 'Kolkata', '2019-09-15', 320000 ),
('Nitin Dey', 'PQR Soln.', 'Delhi', '2019-10-06', 250000 ),
('Sujata Samanta', 'PQR Soln.', 'Kolkata', '2020-10-06', 350000 ),
('Sudip Majhi', 'ABC Corp.', 'Delhi', '2018-10-30', 600000 ),
('Sanjoy Kohli', 'XYZ Digital', 'Delhi', '2019-04-18', 450000 ) ;
要验证使用以下命令如下。
Select * FROM EmployeeDetails;
输出 :
EMPLOYEE_ID | EMPLOYEE_NAME | WORKING_AT | WORK_LOCATION | JOINING_DATE | ANNUAL_INCOME |
---|---|---|---|---|---|
1 | Amit Khan | XYZ Digital | Kolkata | 2019-10-06 | 350000 |
2 | Shreetama Pal | ABC Corp. | Kolkata | 2018-12-16 | 500000 |
3 | Aniket Sharma | PQR Soln. | Delhi | 2020-01-11 | 300000 |
4 | Maitree Jana | XYZ Digital | Kolkata | 2019-05-01 | 400000 |
5 | Priyanka Ojha | ABC Corp. | Delhi | 2019-02-13 | 350000 |
6 | Sayani Mitra | XYZ Digital | Kolkata | 2019-09-15 | 320000 |
7 | Nitin Dey | PQR Soln. | Delhi | 2019-10-06 | 250000 |
8 | Sujata Samanta | PQR Soln. | Kolkata | 2020-10-06 | 350000 |
9 | Sudip Majhi | ABC Corp. | Delhi | 2018-10-30 | 600000 |
10 | Sanjoy Kohli | XYZ Digital | Delhi | 2019-04-18 | 450000 |
现在,我们将找到在“PQR Soln”中工作的员工的年收入样本方差。
SELECT 'PQR Soln.' AS 'Company_Name',
VAR_SAMP(Annual_Income) as VarianceOfAnnualIncome
FROM EmployeeDetails where WORKING_AT = 'PQR Soln.';
输出 :
COMPANY_NAME | VARIANCEOFANNUALINCOME |
---|---|
PQR Soln. | 2500000000 |