目录
UPPER()将文本转换为大写
SELECT vend_name, UPPER(vend_name) AS vend_name_upcase
FROM Vendors
ORDER BY vend_name;
常用的文本处理函数
DATEPART()函数,此函数返回日期的某一部分。
SELECT order_num
FROM Orders
WHERE DATEPART(yy, order_date) = 2020;
常用的数值处理函数
AVG()通过对表中行数计数并计算其列值之和,求得该列的平均值.
SELECT AVG(prod_price) AS avg_price
FROM Products;
COUNT()函数进行计数。
SELECT COUNT(*) AS num_cust
FROM Customers;
MAX()返回指定列中的最大值。
SELECT MAX(prod_price) AS max_price
FROM Products;
MIN()返回指定列中的最小值。
SELECT MIN(prod_price) AS min_price
FROM Products;
SUM()用来返回指定列值的和(总计).
SELECT SUM(quantity) AS items_ordered
FROM OrderItems
WHERE order_num = 20005;
ELECT语句可根据需要包含多个聚集函数。
SELECT COUNT(*) AS num_items,
MIN(prod_price) AS price_min,
MAX(prod_price) AS price_max,
AVG(prod_price) AS price_avg
FROM Products;
SELECT vend_id, COUNT(*) AS num_prods
FROM Products
GROUP BY vend_id;
GROUP BY子句必须出现在WHERE子句之后,ORDER BY子句之前。
WHERE过滤行,而HAVING过滤分组。
SELECT cust_id, COUNT(*) AS orders
FROM Orders
GROUP BY cust_id
HAVING COUNT(*) >= 2;
编写代码,应该注重编程逻辑,先思考,然后再动手编写自己的代码,这样可能会事半功倍。