删除没有min的行(rid)

时间:2010-12-14 22:12:57

标签: sql sql-server tsql sql-server-2008

select min(q.rid)
    from qvalues q
        inner join batchinfo b
            on q.rowid = b.rowid
                and b.instrument = 'tf1'
    group by q.rowid, q.name, q.compound
    having count(*) > 1
  1. 而不是选择min(rid)我如何删除除min(rid)以外的所有内容?
  2. 如何删除除max(rid)以外的所有内容?
  3. 请注意,我只想删除具有相同rowid,name和compound

    的值

1 个答案:

答案 0 :(得分:0)

begin transaction

delete from [table]
where rid != 
(select min(q.rid)
from qvalues q
    inner join batchinfo b
        on q.rowid = b.rowid
            and b.instrument = 'tf1'
group by q.rowid, q.name, q.compound
having count(*) > 1)
and rowid != 
(select q.rowid
from qvalues q
    inner join batchinfo b
        on q.rowid = b.rowid
            and b.instrument = 'tf1'
group by q.rowid, q.name, q.compound
having count(*) > 1)
and name != 
(select q.name
from qvalues q
    inner join batchinfo b
        on q.rowid = b.rowid
            and b.instrument = 'tf1'
group by q.rowid, q.name, q.compound
having count(*) > 1)
and compound != 
(select q.compound
from qvalues q
    inner join batchinfo b
        on q.rowid = b.rowid
            and b.instrument = 'tf1'
group by q.rowid, q.name, q.compound
having count(*) > 1)

删除除max之外的所有内容,使用max语法

执行相同操作