Home  >  Article  >  Database  >  Mean and mode in SQL Server

Mean and mode in SQL Server

WBOY
WBOYforward
2023-08-25 18:21:021450browse

SQL Server 中的均值和众数

Problem Statement

Mean is the average of a given data set, calculated by dividing the sum by the number of values ​​in the data set.

The mode of the data set is the value that appears most frequently in a series of data

If our data set is {1, 2, 3, 4}, the average value is − (1 2 3 4) / 4 = 2.5

If our data set is {1, 2, 3, 4, 1, 1, 1, 1}, the mode is − 1 because it occurs 5 times .

Example

  • First, create a table-
CREATE TABLE NUMBERS (
   value INT
)
  • Insert data into the table-
INSERT INTO NUMBERS VALUES (1);
INSERT INTO NUMBERS VALUES (2);
INSERT INTO NUMBERS VALUES (3);
INSERT INTO NUMBERS VALUES (4);
  • Use the following query to find the mean -
SELECT AVG(val) FROM NUMBERS;
  • Insert pattern-less rows with duplicate values-
INSERT INTO NUMBERS VALUES (1);
INSERT INTO NUMBERS VALUES (1);
INSERT INTO NUMBERS VALUES (1);
INSERT INTO NUMBERS VALUES (1);
  • Use the following query to find the pattern -
SELECT TOP 1 val
FROM NUMBERS
GROUP BY val
ORDER BY COUNT(*) DESC

The above is the detailed content of Mean and mode in SQL Server. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete