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 마스터 테이블의 값을 참조 할 수 없다는 것입니다. 이 문제를 어떻게 해결할 수 있습니까? 당신이 다른 장소에서 나타나는 TOP
및 ORDER BY
제한으로
완벽! 방금 베이컨을 구해 줬어! –