2014-07-22 2 views
0

데이터 테이블에서 가장 최근 가격을 얻으려고합니다. 나는 "10,525 5.00 2014년 7월 6일"의 결과를 얻을 수 있도록 조인 사용하려고SQL Left Join Subquery Issue

Part Price Date

----- ----- --------

10525 .25 1/1/2010

10525 5.00 7/6/2014

10526 4.25 7/1/2014

, 그러나 그것은 나에게이 오류 제공 : 데이터는 같이 나올 것

The column prefix 'orderdtl' does not match with a table name or alias name used in teh query. Statement(s) could not be prepared."

가입을 취소하면 정상적으로 작동합니다. 내가 어디로 잘못 가고 있니?

SELECT orderdtl.partnum, orderdtl.docunitprice, orderhed.orderdate 
FROM mfgsys80.dbo.orderdtl AS orderdtl, mfgsys80.dbo.orderhed AS orderhed 
INNER JOIN(
    SELECT orderdtl.partnum, MAX(orderhed.orderdate) AS 'maxDate' 
    FROM mfgsys80.dbo.orderdtl AS orderdtl, mfgsys80.dbo.orderhed AS orderhed 
    GROUP BY orderdtl.partnum 
) AS Temp 
    ON orderdtl.partnum = Temp.partnum AND orderhed.orderdate = Temp.maxDate 
WHERE orderdtl.ordernum = orderhed.ordernum AND ((orderdtl.custnum=74)) 
ORDER BY orderdtl.partnum 

답변

0

쿼리 구성이 잘못되었습니다. 그것은이어야합니다

SELECT orderdtl.partnum, orderdtl.docunitprice, orderhed.orderdate 
FROM mfgsys80.dbo.orderdtl AS orderdtl 
JOIN mfgsys80.dbo.orderhed AS orderhed 
ON orderdtl.ordernum = orderhed.ordernum 
INNER JOIN(
    SELECT ol.partnum, MAX(od.orderdate) AS 'maxDate' 
    FROM mfgsys80.dbo.orderdtl ol 
    JOIN mfgsys80.dbo.orderhed od 
    ON ol.ordernum = od.ordernum 
    GROUP BY ol.partnum 
) Temp 
ON orderdtl.partnum = Temp.partnum 
AND orderhed.orderdate = Temp.maxDate 
WHERE orderdtl.custnum=74 
ORDER BY orderdtl.partnum 
+0

당신의 도움에 감사드립니다! 내 SQL은 꽤 녹슬었지만 당신이 한 것은 내가 그것을 보았을 때 의미가있다. – Error