2014-07-07 3 views
0

다음과 같은 경우가 있습니다. 나는 두 테이블을 합칠 필요가있다. 첫 번째 테이블은 마스터 레코드입니다. 두 번째 테이블은 내림차순 데이터로 정렬 한 다음 상위 레코드 만 마스터 레코드에 조인해야하는 다중 행 하위 레코드입니다. 내가 가지고 올 수있는 유일한 방법은 이것이다 :하위 쿼리의 위치에서 기본 집합의 값 사용

WITH SCD as (select top 100000 Part_Id as PartID, [Date_Created] 
     ,[Production_Part_Status_Id] 
     ,[Operations_Labor_Rollup] 
     ,[Operations_Materials_Rollup] 
     ,[Purchased_Subpart_Rollup] 
     ,[Production_Subpart_Rollup] from 
    Production_Part_Supplier_Cost_Detail order by Date_Created desc) 
SELECT 
    pp.Part_Number 
    ,pp.Part_Name 
    ,pp.Revision 
    ,pp.EAU_Quantity 
    ,pp.Drawing_Number 
    ,pp.Net_Weight 
    ,pp.Current_Price_Each 
    ,pp.Buyer_Stock_Id 
    ,pp.Current_Price_Each - 
     (COALESCE(Operations_Labor_Rollup, 0.0) + 
     COALESCE(Operations_Materials_Rollup, 0.0) + 
     COALESCE(Purchased_Subpart_Rollup, 0.0) + 
     COALESCE(Production_Subpart_Rollup, 0.0)) as [variance] 
    ,case when pp.Current_Price_Each = 0 or pp.Current_Price_Each IS null then 0 else 
     (COALESCE(Operations_Labor_Rollup, 0.0) + 
     COALESCE(Operations_Materials_Rollup, 0.0) + 
     COALESCE(Purchased_Subpart_Rollup, 0.0) + 
     COALESCE(Production_Subpart_Rollup, 0.0))/pp.Current_Price_Each end as [Variance Percent] 
    ,pp.Variance * pp.EAU_Quantity as [Annualized Variance] 
FROM 
    Production_Part AS pp 
left join (SELECT TOP 1 * FROM SCD where Part_Id = PartID) ppscd on ppscd.PartID = pp.Part_Id 
WHERE 
    pp.Buyer_Id = @buyer_id; 

내가 실행 해요 문제는 하위 쿼리의 WHERE 마스터 테이블의 값을 참조 할 수 없다는 것입니다. 이 문제를 어떻게 해결할 수 있습니까? 당신이 다른 장소에서 나타나는 TOPORDER BY 제한으로

답변

1

,이 가 작동 등장하지만 실제로는 실패 할 수도 있습니다. 나는 CTE에 TOP 100000을 추가하여 "쿼리 최적화 프로그램을 종료"했다고 생각하지만 실제로 무엇을했는지는 버그를 숨기는 것이 었습니다.

오히려 TOPORDER BY을 사용하는 것보다, 우리는 대신 ROW_NUMBER()를 사용할 수 있습니다

;WITH SCD as (select Part_Id as PartID, [Date_Created] 
     ,[Production_Part_Status_Id] 
     ,[Operations_Labor_Rollup] 
     ,[Operations_Materials_Rollup] 
     ,[Purchased_Subpart_Rollup] 
     ,[Production_Subpart_Rollup] 
     ,ROW_NUMBER() OVER (PARTITION BY Part_ID 
          ORDER BY Date_Created desc) as rn 
    from 
    Production_Part_Supplier_Cost_Detail) 
SELECT 
    pp.Part_Number 
    ,pp.Part_Name 
    ,pp.Revision 
    ,pp.EAU_Quantity 
    ,pp.Drawing_Number 
    ,pp.Net_Weight 
    ,pp.Current_Price_Each 
    ,pp.Buyer_Stock_Id 
    ,pp.Current_Price_Each - 
     (COALESCE(Operations_Labor_Rollup, 0.0) + 
     COALESCE(Operations_Materials_Rollup, 0.0) + 
     COALESCE(Purchased_Subpart_Rollup, 0.0) + 
     COALESCE(Production_Subpart_Rollup, 0.0)) as [variance] 
    ,case when pp.Current_Price_Each = 0 or pp.Current_Price_Each IS null then 0 else 
     (COALESCE(Operations_Labor_Rollup, 0.0) + 
     COALESCE(Operations_Materials_Rollup, 0.0) + 
     COALESCE(Purchased_Subpart_Rollup, 0.0) + 
     COALESCE(Production_Subpart_Rollup, 0.0))/pp.Current_Price_Each end as [Variance Percent] 
    ,pp.Variance * pp.EAU_Quantity as [Annualized Variance] 
FROM 
    Production_Part AS pp 
left join SCD ppscd on ppscd.PartID = pp.Part_Id and 
         ppscd.rn = 1 
WHERE 
    pp.Buyer_Id = @buyer_id; 
+0

완벽! 방금 베이컨을 구해 줬어! –

관련 문제