0

我有以下选择语句:

SELECT  projectid,documentid,revisionno,configurationid,variable45,
        ISNULL(Variable45, (SELECT TOP 1 variable45 FROM pivot_table WHERE documentid = t.documentid and projectid=t.projectid
        and configurationid=t.configurationid and cast(revisionno as int) < cast(t.revisionno as int) AND Variable45 is NOT NULL 
        ORDER BY projectid desc,documentid desc ,revisionno desc,configurationid desc)) as NewCol
FROM    pivot_table t;

我尝试通过以下方式转换为更新,但我得到了错误的记录更新。谁能帮我解决我的问题:

UPdate PIVOT_TABLE
set variable45 = ((SELECT TOP 1 variable45 FROM pivot_table t WHERE t.documentid = documentid and t.projectid=projectid
        and t.configurationid=configurationid and cast(t.revisionno as int) < cast(revisionno as int) AND Variable45 is NOT NULL 
        ORDER BY revisionno desc)) where Variable45 is NULL;

数据库:SQLExpress2008。

请指教。谢谢你。

4

1 回答 1

0

好的,我想通了:

    UPdate pt
    set pt.variable45 = ((SELECT TOP 1 t.variable45 FROM pivot_table t WHERE 
t.documentid = pt.documentid and t.projectid=pt.projectid and t.configurationid=pt.configurationid and cast(t.revisionno as int) < cast(pt.revisionno as int) AND t.variable45 is NOT NULL 
            ORDER BY revisionno desc)) from PIVOT_TABLE pt where pt.variable45 is NULL;
于 2012-08-11T15:31:25.823 回答