在SQL語句中,可以使用rank()函數與其他窗口函數配合使用來實現更復雜的查詢需求。rank()函數通常用于為查詢結果集中的行賦予一個排名,常與order by子句一起使用。以下是一些與rank()函數配合使用的例子:
SELECT department, sales, RANK() OVER(ORDER BY sales DESC) AS sales_rank
FROM sales_data;
SELECT department, sales, RANK() OVER(ORDER BY sales DESC) AS sales_rank,
DENSE_RANK() OVER(ORDER BY sales DESC) AS dense_sales_rank
FROM sales_data;
SELECT department, sales,
RANK() OVER(ORDER BY sales DESC) AS sales_rank,
(sales - LAG(sales) OVER(PARTITION BY department ORDER BY month)) / LAG(sales) OVER(PARTITION BY department ORDER BY month) AS sales_growth_rate
FROM sales_data;
通過與其他窗口函數的配合使用,可以實現更加靈活和復雜的查詢需求,幫助分析數據并生成更有意義的報告。