3

想要从表中删除所有行,除了最近的 1 行。

此查询适用于 SQL Server 2008 标准版:

DELETE S1 
FROM StateLogs S1, StateLogs S2 
WHERE S1.NodeId = S2.NodeId AND S1.NodeId = {0} AND S1.Modified < S2.Modified

但在 SQL Server Compact Edition 3.5 上失败

解析查询时出错。
[令牌行号= 1,令牌行偏移量= 11,错误令牌= FROM]

紧凑版的等价物是什么?

编辑

表设计http://pastebin.com/Akwpypkm

4

2 回答 2

4

您的删除语句似乎有语法错误,请尝试将查询从

DELETE S1 FROM ...

DELETE FROM ...

更新:

尝试使用以下语句代替您的语句:

DELETE FROM StateLogs WHERE Id NOT IN (SELECT TOP (1) Id FROM StateLogs ORDER BY Modified DESC)

Also just a heads up that you may have problems with insert/update operations against the table based on the schema you've provided because your primary key is larger than the maximum: http://msdn.microsoft.com/en-us/library/ms191241(v=sql.105).aspx

This could pretty cause mysterious runtime failures in your application.

于 2012-11-26T05:34:45.907 回答
0

尝试这样的事情,

DELETE  a
FROM    [StateLogs] a
        LEFT JOIN
        (   
            SELECT NodeID, MAX([Modified]) maxDate
            FROM [StateLogs]
            GROUP BY NodeID
        ) b ON a.nodeID = b.NodeID AND
                a.[Modified] = b.maxDate
WHERE   b.NodeID IS NULL

更新 1

DELETE  
FROM    [StateLogs] 
WHERE   NodeID NOT IN
        (
            SELECT NodeID
            FROM
                    (   
                        SELECT NodeID, MAX([Modified]) maxDate
                        FROM [StateLogs]
                        GROUP BY NodeID
                    ) s
        )
于 2012-11-26T05:33:12.420 回答