我只想为每个客户端保留 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)