我想为每个clientid只保留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)
答案 0 :(得分:2)
:
DELETE from CLIENTS
where CLIENT_ID = 'xxx' and
rownum > 1000
答案 1 :(得分: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
答案 2 :(得分:1)
我认为“在Oracle中”的答案会删除最新的条目。小心那个!