简体   繁体   中英

Update in SQL from using a subquery

I am a novice SQL learner and trying to do an update statement as like before

UPDATE STG
SET STG.COL1 = SUB.COL1
SET STG.COL2 = SUB.COL2
FROM TABLE AS STG
INNER JOIN (SELECT ID, MIN([DATE]) AS COL1, MAX([DATE]) AS COL2
            FROM TABLE
            GROUP BY ID) AS SUB ON STG.ID = SUB.ID

but I'm getting these errors:

Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near '.'.

Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'AS'.

I am using

Microsoft SQL Server  2000 - 8.00.2066 (Intel X86) 
May 11 2012 18:41:14 
Copyright (c) 1988-2003 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)

Try this

 UPDATE STG
    SET STG.COL1 = SUB.COL1,STG.COL2 = SUB.COL2
    FROM TABLE  STG
    INNER JOIN (SELECT ID
    ,MIN([DATE]) AS COL1
    ,MAX([DATE]) AS COL2
    FROM TABLE
    GROUP BY ID 
    ) As SUB
    ON STG.ID = SUB.ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM