標題說這一切,爲什麼我不能在SQL Server的where子句中使用窗口函數?爲什麼在where子句中沒有窗口函數?
這個查詢非常有道理:
select id, sales_person_id, product_type, product_id, sale_amount
from Sales_Log
where 1 = row_number() over(partition by sales_person_id, product_type, product_id order by sale_amount desc)
但它不工作。有沒有比CTE /子查詢更好的方法?
編輯
爲了什麼它的價值,這是一個CTE查詢:
with Best_Sales as (
select id, sales_person_id, product_type, product_id, sale_amount, row_number() over (partition by sales_person_id, product_type, product_id order by sales_amount desc) rank
from Sales_log
)
select id, sales_person_id, product_type, product_id, sale_amount
from Best_Sales
where rank = 1
編輯
+1與子查詢顯示的答案,但我真的我正在尋找不能在where子句中使用窗口函數的原因。
窗口化函數是非關係層的一部分(因爲關係理論不處理有序數據);因此他們在一切之後被評估。 –