在SQL中,rownumber窗口函數用于為結果集中的每一行分配一個唯一的數字。除了基本用法外,rownumber窗口函數還可以用于更高級的應用,例如:
SELECT
department,
employee,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM
employees;
SELECT
department,
employee,
salary
FROM
(
SELECT
department,
employee,
salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank
FROM
employees
) ranked
WHERE
rank <= 3;
SELECT
department,
employee,
salary
FROM
(
SELECT
department,
employee,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM
employees
) ranked
WHERE
rank = 1;
這些是rownumber窗口函數的一些高級應用示例,可以根據具體需求進行調整和擴展。通過靈活運用窗口函數,可以更有效地處理復雜的數據分析和查詢任務。