SQL COS函数
COS是数学中的一个SQL函数,它返回指定数字的余弦值。
COS函数的语法
SELECT COS(Number) AS Alias_Name;
在COS语法中,我们必须传入想要返回其余弦值的十进制数。
在结构化查询语言中,我们也可以使用COS函数与表的字段一起使用,如下所示:
SELECT COS(column_Name) AS Alias_Name FROM Table_Name;
在这个语法中,我们必须定义我们想要执行COS函数的表格的名称和列。
COS函数的例子
例子1: 这个例子返回给定查询中数字的余弦值。
SELECT COS(101) AS Cos_Value_of_101;
输出:
Cos_Value_of_101
---
0.89200
示例2: 此示例返回以下SELECT查询中2的余弦值:
SELECT COS(2) AS Cos_Value_of_2;
输出:
Cos_Value_of_2
---
0.41614683654714241
示例 3: 此示例返回数字8的余弦值:
SELECT COS(8) AS Cos_Value_of_8;
输出:
Cos_Value_of_8
---
0.98935824662338179
示例4: 这个示例返回255的Cos_Value:
SELECT COS(255) AS Cos_Value_of_255;
输出结果:
Cos_Value_of_255
---
-0.50639163492449091
示例5: 返回90的Cos_Value的示例:
SELECT COS(90) AS Cos_Value_of_90;
输出:
Cos_Value_of_90
---
-0.448073616
示例6: 此示例返回NULL的Cos_Value:
SELECT COS(NULL) AS Cos_Value_of_NULL;
输出:
Cos_Value_of_NULL
---
\-
示例7: 此示例返回21到30之间所有数字的余弦值:
SELECT COS(21) AS 21, COS(22) AS 22, COS(23) AS 23, COS(24) AS 24, COS(25) AS 25, COS(26) AS 26, COS(27) AS 27, COS(28) AS 28, COS(29) AS 29, COS(30) AS 30;
输出:
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
---|---|---|---|---|---|---|---|---|---|
0.8366 | -8.8513 | -0.846 | -0.9055 | -0.1323 | 0.7625 | 0.9563 | 0.2709 | 0.6636 | -0.988 |
示例8: 此示例使用SQL表的COS函数。
在此示例中,我们将通过新表执行COS函数来创建新表:
以下显示了在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, NULL, 2022-04-30, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (202, P4, 15.500, 45, 75, 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, 25, NULL, 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, 15, 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, 15.500, 50, 70, 2021-10-15, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (212, P8, 19.750, 110, 250, 2022-01-28, 4);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (112, P10, 10.250, 550, 835, 2022-04-11, NULL);
以下 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 | NULL | 2022-04-30 | NULL |
202 | P4 | 15.500 | 45 | 75 | 2022-01-28 | 5 |
103 | P2 | 18.250 | 25 | NULL | 2022-02-18 | 4 |
111 | P7 | 25.250 | 5 | 15 | 2021-12-25 | 9 |
210 | P6 | 15.500 | 50 | 70 | 2021-10-15 | NULL |
212 | P8 | 19.750 | 110 | 250 | 2022-01-28 | 4 |
112 | P10 | 10.250 | 550 | 835 | 2022-04-11 | NULL |
查询1: 以下SELECT查询在上面的Product_Details表中使用了COS函数和Product_Quantity列:
SELECT Product_ID, COS(Product_ID) AS Cos_Value_of_Product_ID FROM Product_Details;
这个查询返回表中每个产品id的余弦值。
输出:
Product_ID | Cos_Value_of_Product_ID |
---|---|
104 | -0.3216 |
202 | 0.80641 |
103 | 0.622 |
111 | -0.8654 |
210 | 0.4677185 |
212 | -0.99834 |
112 | -0.8899956 |
查询2: 以下SELECT查询使用了上述Product_Details表中的COS函数、Purchasing_Price和Selling_Price列:
SELECT Purchasing_Price, COS(Purchasing_Price) AS Cos_Value_of_PurchasingPrice, Selling_Price, COS(Selling_Price) AS Cos_Value_of_SellingPrice FROM Product_Details;
这个查询显示了每个产品的购买和销售价格的余弦值。
输出:
Purchasing_Price | Cos_Value_of_PurchasingPrice | Selling_Price | Cos_Value_of_SellingPrice |
---|---|---|---|
945 | 0.58053 | NULL | |
45 | 0.85090 | 75 | -0.387781 |
25 | -0.132351 | NULL | |
5 | -0.9589 | 15 | -0.650287 |
50 | -0.262374 | 70 | -0.773890 |
110 | -4.42426 | 250 | -0.97052 |
550 | -0.21948 | 835 | -0.61599 |