我试图从数据表中获取最新价格。数据将如下所示:
零件价格日期
----- ----- --------
10525 .25 1/1/2010
10525 5.00 7/6/2014
10526 4.25 7/1/2014
我尝试使用连接,以便我可以得到" 10525 5.00 7/6/2014"的结果,但它给了我这个错误:
列前缀' orderdtl'与查询中使用的表名或别名不匹配。声明无法准备。"
如果我把联接拿出来,它就可以了。我哪里错了?
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 :(得分: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