HAVE 语句在 SQL 中主要用于筛选分组后的结果,通常与 GROUP BY 和聚合函数一起使用
SELECT product_category, SUM(sales) as total_sales
FROM sales_data
GROUP BY product_category
HAVE total_sales > 1000;
SELECT product_category, COUNT(DISTINCT customer_id) as unique_customers
FROM sales_data
GROUP BY product_category
HAVE unique_customers > 5;
SELECT product_category, MAX(sales) as max_sales
FROM sales_data
GROUP BY product_category
HAVE max_sales > 500;
SELECT product_category, AVG(sales) as avg_sales
FROM sales_data
GROUP BY product_category
HAVE avg_sales < 200;
总之,HAVE 语句在 SQL 中主要用于筛选分组后的结果,帮助你更好地分析和处理数据。