MySQL 中的 STDDEV()函数
有时我们需要计算 MySQL 中某个表达式的总体标准差。
在 MySQL 中, STDDEV()函数可用于此目的。如果在给定表达式中找不到匹配的行,则返回 NULL。
句法 :
STDDEV(expr);
参数:此方法只接受一个参数。
- expr:我们要计算标准偏差的输入表达式。
返回:它返回总体标准差。
示例-1:
使用 STDDEV函数从给定的 StudentMarks 表中查找 sub1mark 列的标准偏差。
创建 StudentMarks 表:
CREATE TABLE StudentMarks
(
StudentId INT AUTO_INCREMENT,
StudentName VARCHAR(100) NOT NULL,
Roll INT NOT NULL,
Sub1Mark INT NOT NULL,
Sub2Mark INT NOT NULL,
Sub3Mark INT NOT NULL,
TotalMarks INT NOT NULL,
PRIMARY KEY(StudentId )
);
插入数据到表中:
INSERT INTO StudentMarks
(StudentName, Roll, Sub1Mark, Sub2Mark, Sub3Mark, TotalMarks)
VALUES
('Amit Jana', 10100, 85, 80, 95, 260),
('Labanya Mallick', 11000, 81, 89, 95, 265),
('Virat Sharma', 12000, 75, 83, 90, 248),
('Sayani Samanta', 13000, 95, 90, 99, 284),
('Riyanka Panda', 14000, 70, 87, 88, 245),
('Ritika Shah', 15000, 78, 89, 90, 257);
要验证使用以下命令如下。
SELECT * from StudentMarks;
输出 :
STUDENTID | STUDENTNAME | ROLL | SUB1MARK | SUB2MARK | SUB3MARK | TOTALMARKS |
---|---|---|---|---|---|---|
1 | Amit Jana | 10100 | 85 | 80 | 95 | 260 |
2 | Labanya Mallick | 11000 | 81 | 89 | 95 | 265 |
3 | Virat Sharma | 12000 | 75 | 83 | 90 | 248 |
4 | Sayani Samanta | 13000 | 95 | 90 | 99 | 284 |
5 | Riyanka Panda | 14000 | 70 | 87 | 88 | 245 |
6 | Ritika Shah | 15000 | 78 | 89 | 90 | 257 |
现在我们要找到 sub1mark 列的标准偏差。
SELECT STDDEV(Sub1Mark) as Sub1StandardDeviation
FROM StudentMarks;
输出 :
SUB1STANDARDDEVIATION |
---|
7.930251502246881 |
示例 2
现在我们要找到总分列的总体标准差。
SELECT STDDEV(TotalMarks) as StdDevOfTotalMarks
FROM StudentMarks;
输出 :
STDDEVOFTOTALMARKS |
---|
12.772583485297279 |
示例 3:在此示例中,我们将查找在“XYZ Digital”公司工作的员工收入的总体标准差。为了演示创建一个名为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 |
现在,我们将找到在“XYZ Digital”工作的员工的年收入的总体标准差。
SELECT STDDEV(Annual_Income) as StdDevOfAnnualIncome
FROM EmployeeDetails where WORKING_AT = 'XYZ Digital';
输出 :
STDDEVOFANNUALINCOME |
---|
49497.474683058324 |