Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
WindChaser
Posting Yak Master
225 Posts |
Posted - 2005-02-24 : 12:29:29
|
| Folks, I just need a bit of assistance with my bad syntax. I would just like to update a table based on information found in another table. UPDATE Table1 AS T1SET MyDate = (SELECT MIN(Date) FROM Table2 WHERE Account = T1.Account)But that doesn't work. What's the correct way to write the T-SQL statement? Thanks! |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-02-24 : 12:55:38
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(Account int, MyDate datetime)CREATE TABLE myTable00(Account int, [Date] datetime)GOINSERT INTO myTable99(Account, MyDate)SELECT 1, '12/31/2004' UNION ALLSELECT 2, '12/31/2003' UNION ALLSELECT 3, '12/31/2002'INSERT INTO myTable00(Account, [Date])SELECT 1, '12/31/2004' UNION ALLSELECT 2, '12/31/2003' UNION ALLSELECT 3, '12/31/2002' UNION ALLSELECT 1, '12/31/2001' UNION ALLSELECT 2, '12/31/1999' UNION ALLSELECT 3, '12/31/1998'GOSELECT * FROM myTable99SELECT * FROM myTable00GOUPDATE T1 SET MyDate = MIN_Date FROM myTable99 T1 JOIN (SELECT Account, MIN([Date]) AS MIN_Date FROM myTable00 GROUP BY Account) T2 ON T1.Account = T2.AccountGOSELECT * FROM myTable99SELECT * FROM myTable00GOSET NOCOUNT OFFDROP TABLE myTable99DROP TABLE myTable00GO[/code]Brett8-) |
 |
|
|
WindChaser
Posting Yak Master
225 Posts |
Posted - 2005-02-24 : 13:17:25
|
| Works like a charm. Thanks Brett !!! |
 |
|
|
|
|
|
|
|