试图从具有最高日期的另一个表格更新一列。使用wheres更新另一个表格
Table 1
实施例:
PartNumber | Cost
1000 | .10
1001 | .20
Table 2
实施例:
PartNumber | Cost | Date
1000 | .10 | 2017-01-01
1000 | .50 | 2017-02-01
1001 | .20 | 2017-01-01
1002 | .50 | 2017-02-02
我想更新表1与表2,从最近的值,这将是0.50每个...的查询我用来更新这个工作很好,直到我意识到我没有抓住正确的成本,因为有倍数..我现在想抓住最高的修订版本。
我的查询:
UPDATE dex_mfgx..insp_master
SET dex_mfgx..insp_master.costperpart = t2.sct_cst_tot
FROM dex_mfgx..insp_master AS t1
INNER JOIN qad_repl..sct_det_sql AS t2
ON t1.partnum = t2.sct_part
WHERE t1.partnum = t2.sct_part and t2.sct_cst_date = MAX(t2.sct_cst_date) ;
我的错误:
Msg 147, Level 15, State 1, Line 6
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
不与具有或GROUPING多少运气,虽然我还没有大量使用他们..
任何有一个想法,有助于?
那么,它是MySQL还是SQL Server ?,请使用相应的标记 – Lamak
您是否试图用子查询获取MAX? –