chrisryhal
asked on
Update table from select statement
I have the following select statement.
SELECT partnum, max(tranDate) lastTranDate, min(tranDate) DATEADD,count(*) FROM parttran WHERE trantype in ('DMR-MTL', 'INS-MTL', 'PLT-MTL', 'PUR-MTL','STK-MTL','MFG-C US','MFG-P LT','MFG-S TK','PLT-A SM','PUR-I NS','PUR-S TK','PUR-U KN','STK-A SM') group by partnum having max(tranDate) < dateadd(year, -2, getdate())
Based on the "PartNum" that are presented, I need to do an update statement like so
Update Part Set CheckBox01 = '1'
So my quesiton, how can I update a table from the result of this select statement.
SELECT partnum, max(tranDate) lastTranDate, min(tranDate) DATEADD,count(*) FROM parttran WHERE trantype in ('DMR-MTL', 'INS-MTL', 'PLT-MTL', 'PUR-MTL','STK-MTL','MFG-C
Based on the "PartNum" that are presented, I need to do an update statement like so
Update Part Set CheckBox01 = '1'
So my quesiton, how can I update a table from the result of this select statement.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, Greg. I was typing and did not see your comment. That is a good example of the IN syntax. Using CTE helps to transition from existing code easily also.
maybe this:
Update Part Set CheckBox01 = '1'
where some_column in (your select statement, but just the required id in select part...)
Update Part Set CheckBox01 = '1'
where some_column in (your select statement, but just the required id in select part...)
No problem, Kevin. Actually, it doesn't say what version of SQL it is. Mine will only work on 2005 and higher. Yours will work on SQL 2000 as well.
Greg
Greg
ASKER
Its 2005
ASKER
Both soutions worked. Thanks again