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 中主要用于篩選分組后的結果,幫助你更好地分析和處理數據。