SQL ROUND函数
SQL的ROUND函数将指定的数字四舍五入到特定的小数位数。
ROUND字符串函数的语法
SELECT ROUND(Number, Decimal_places, Operation) AS Alias_Name;
在这个ROUND函数中,有以下三个参数:
- Number: 这是需要四舍五入的十进制数字。
- Decimal_places: 这个参数可以是正数或负数的整数,表示需要进行四舍五入的小数位数。
- Operation: 这个参数是可选的。
在结构化查询语言中,我们也可以使用ROUND函数来处理表格的整数列,如下所示的代码块:
SELECT ROUND(Column_Name, Decimal_places, Operation) AS Alias_Name FROM Table_Name;
在这个语法中,我们使用了已经存在的SQL表格的ROUND函数。在这里,我们需要定义要执行ROUND函数的表格的名称和列。
ROUND字符串函数的示例
示例 1: 下面的round函数将数字四舍五入到小数点前-1位:
SELECT ROUND(945.451) AS Round_-1value;
输出:
Round_-1Value
---
950.00
示例 1: 下面的round函数用第三个参数将数字四舍五入到小数点后2位,并且该参数不能为零:
SELECT ROUND(145.415) AS Round_-1value;
输出:
Round_-1Value
---
145.41
示例3: 此示例使用数学ROUND函数与SQL表格:
在这第三个示例中,我们将通过新表格执行ROUND函数与表格的整数列:
以下是在SQL中创建新表格的语法:
CREATE TABLE Name_of_New_Table
(
First_Column_of_table Data Type (character_size of First Column),
Second_Column_of_table Data Type (character_size of the Second column ),
Third_Column_of_table Data Type (character_size of the Third column),
......,
Last_Column_of_table Data Type (character_size of the Last column)
);
以下CREATE语句创建了 Product_Details 表,用于存储产品的价格和数量:
CREATE TABLE Product_Details
(
Product_ID INT NOT NULL,
Product_Name Varchar(50),
Product_Quantity INT,
Purchasing_Price INT,
Selling_Price INT,
Release_Date Date,
Product_Rating INT
);
以下多个INSERT查询将产品及其销售和采购价格的记录插入Product_Details表:
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (104, P1, 10.250, 945.2548, 1050.2547, 2022-04-30, 8);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (202, P4, 15.500, 45.248, 75.5725, 2022-01-28, 5);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (103, P2, 18.250, 255.248, 475.725, 2022-02-18, 4);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (111, P7, 25.250, 5.958, 15.955, 2021-12-25, 9);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (210, P6, 12.650, 50.958, 70.955, 2021-10-15, 11);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (212, P8, 19.750, 110.850, 250.955, 2022-01-10, 3);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (112, P10, 24.950, 550.654, 835.657, 2022-04-11, 8);
以下SELECT语句显示了上述 Product_Details 表中插入的记录:
SELECT * FROM Product_Details;
Product_ID | Product_Name | Product_Quantity | Purchasing_Price | Selling_Price | Release_Date | Product_Rating |
---|---|---|---|---|---|---|
104 | P1 | 10.250 | 945.2548 | 1050.2547 | 2022-04-30 | 8 |
202 | P4 | 15.500 | 45.248 | 75.5725 | 2022-01-28 | 5 |
103 | P2 | 18.250 | 255.248 | 475.725 | 2022-02-18 | 4 |
111 | P7 | 25.250 | 5.958 | 15.955 | 2021-12-25 | 9 |
210 | P6 | 12.650 | 50.958 | 70.955 | 2021-10-15 | 11 |
212 | P8 | 19.750 | 110.850 | 250.955 | 2022-01-10 | 3 |
112 | P10 | 24.950 | 550.654 | 835.657 | 2022-04-11 | 8 |
查询1: 以下SELECT查询使用了以上Product_Details表的Product_Quantity列的ROUND函数:
SELECT Product_ID, Product_Name, Product_Quantity, ROUND(Product_Quantity, -1) AS Round_-1quantity FROM Product_Details;
输出:
Product_ID | Product_Name | Product_Quantity | Round_-1quantity |
---|---|---|---|
104 | P1 | 10.250 | 10.000 |
202 | P4 | 15.500 | 20.000 |
103 | P2 | 18.250 | 20.000 |
111 | P7 | 25.250 | 30.000 |
210 | P6 | 12.650 | 10.000 |
212 | P8 | 19.750 | 20.000 |
112 | P10 | 24.950 | 20.000 |
查询2: 下面的SELECT查询在上面的Product_Details表中使用了ROUND函数与Product_ID大于103的商品的Purchasing_Price和Selling_Price列:
SELECT Product_ID, Purchasing_Price, ROUND(Purchasing_Price, 2, 0) AS Round_2purchase, Selling_Price, ROUND( Selling_Price, 2, 0) AS Round_2selling FROM Product_Details WHERE Product_ID > 103;
输出:
Product_ID | Purchasing_Price | Round_2purcahse | Selling_Price | Round_2Sellling |
---|---|---|---|---|
104 | 945.2548 | 945.2500 | 1050.2547 | 1050.2500 |
202 | 45.248 | 45.250 | 75.5725 | 75.5700 |
111 | 5.958 | 5.960 | 15.955 | 15.960 |
210 | 50.958 | 50.960 | 70.955 | 70.960 |
212 | 110.850 | 110.850 | 250.955 | 250.960 |
112 | 550.654 | 550.650 | 835.657 | 835.660 |
查询3: 下面的SELECT查询使用了上述Product_Details表的Product_Quantity列的ROUND函数:
SELECT Product_ID, Product_Quantity, ROUND(Product_Quantity, 1, 1) AS Round_1_1quantity FROM Product_Details;
本函数使用round函数中的第三个参数非零值,即1。
输出:
Product_ID | Product_Quantity | Round_1_1quantity |
---|---|---|
104 | 10.250 | 10.200 |
202 | 15.500 | 15.500 |
103 | 18.250 | 18.200 |
111 | 25.250 | 25.200 |
210 | 12.650 | 12.600 |
212 | 19.750 | 19.700 |
112 | 24.950 | 24.900 |