2012-12-21 7 views
35

제목은 모두 말해서 왜 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 절에서 윈도우 함수를 사용할 수없는 추론을 찾고 있습니다. CTE에 대한 필요가 없습니다

+1

창 작업 기능은 비 관계형 계층의 일부입니다 (관계형 이론에서는 정렬 된 데이터를 처리하지 않으므로). 따라서 그들은 다른 모든 것 다음에 평가됩니다. –

답변