2

我只想为每个客户端保留 1000 个条目。下面的代码可以满足我的要求,但不会遍历 clientid,而是保留 1000 个客户端。

有没有办法在sql中做到这一点?有人告诉我我需要一个光标,但我希望不需要。

DECLARE @ids TABLE ( id int )
DECLARE @clients TABLE ( clientid varchar(20) )

INSERT INTO @clients (clientid)
SELECT select distinct clientid FROM tRealtyTrac 

INSERT INTO @ids (id)
SELECT top 1000 id FROM tRealtyTrac WHERE clientid in (select clientid from @clients)

DELETE trealtytrac WHERE id NOT IN (select id from @ids)
4

3 回答 3

2

在甲骨文中:

DELETE from CLIENTS
where CLIENT_ID = 'xxx' and
rownum > 1000
于 2009-07-23T21:40:08.237 回答
2

这是 SQL Server 2005 或更高版本吗?怎么样的东西

INSERT INTO @ids (id)
SELECT id FROM (
    SELECT id, RANK() OVER (PARTITION BY clientid ORDER BY id) AS Rank FROM tRealtyTrac
) t
WHERE t.Rank <= 1000
于 2009-07-23T21:51:38.790 回答
1

我认为“in Oracle”的答案会删除最新的条目。小心那个!

于 2009-07-23T21:55:07.723 回答