帮助删除难以进行t-sql查询的值

时间:2010-12-10 17:08:21

标签: sql sql-server tsql

select * from qvalues where rowid in (

select rowid  from qvalues where rowid in (select rowid from batchinfo where datapath not like  '%thc%' and datapath not like '%pain%' and datapath not like 'tf1' and datapath like '%excel short%') 
and (compound='etg')
and name='qc1'
group by rowid
having (COUNT(rowid)>1))
and name='qc1'
order by rowid,rid

上面的语句返回:

rid name    compound    rt  response    finalConc   qvalue  rowid
508898  QC1 ETG 33,463.34   3,388.83    719.38  100 17800
508900  QC1 ETG 33,463.34   3,388.83    617.09  100 17800
510382  QC1 ETG 41,657.33   4,662.37    742.01  100 17860
510384  QC1 ETG 33,463.34   3,388.83    617.09  100 17860
527192  QC1 ETG 106,578.46  15,116.28   894.79  100 18478
527194  QC1 ETG 33,463.34   3,388.83    617.09  100 18478
527198  QC1 ETG 110,313.48  16,699.65   877.02  100 18479
527200  QC1 ETG 33,463.34   3,388.83    617.09  100 18479
527204  QC1 ETG 107,128.41  15,052.29   852.51  100 18480
527206  QC1 ETG 33,463.34   3,388.83    617.09  100 18480
527210  QC1 ETG 116,257.86  16,929.16   841.9   100 18481
527212  QC1 ETG 33,463.34   3,388.83    617.09  100 18481
527216  QC1 ETG 102,569.68  17,601.24   991.17  100 18482
527218  QC1 ETG 33,463.34   3,388.83    617.09  100 18482
527222  QC1 ETG 120,925.71  15,204.38   715.48  100 18483
527224  QC1 ETG 33,463.34   3,388.83    617.09  100 18483
529873  QC1 ETG 213,327.65  26,499.14   691.04  100 18576
529875  QC1 ETG 33,463.34   3,388.83    617.09  100 18576
540030  QC1 ETG 69,627.37   9,401.47    814.5   100 18987
540032  QC1 ETG 33,463.34   3,388.83    617.09  100 18987
540182  QC1 ETG 56,768.32   7,682.65    921.53  100 18993
540184  QC1 ETG 33,463.34   3,388.83    617.09  100 18993
540186  QC1 ETS 92,853.28   2,428.01    14.06   100 18994

我想只保留具有第一个唯一ROWID的行并删除其余的行。如何在第一次出现一个唯一的ROWID后删除每条记录?

3 个答案:

答案 0 :(得分:1)

这可能有所帮助。

WITH cte AS
   (
   SELECT rowid,
      ROW_NUMBER() OVER (PARTITION BY rowid ORDER BY rid) AS sequence
   FROM qvalues
   WHERE rowid IN
        (SELECT rowid
        FROM batchinfo
        WHERE datapath NOT LIKE '%thc%'
            AND datapath NOT LIKE '%pain%'
            AND datapath NOT LIKE 'tf1'
            AND datapath LIKE '%excel short%'
        )
        AND (compound='etg')
        AND name='qc1'
   GROUP BY rowid
   HAVING COUNT(rowid)>1
   )

DELETE
FROM cte
WHERE sequence > 1

查询使用公用表表达式(CTE)来获取行,并为每个rowid组添加序列号。序列由命令rid确定。所以,第一个rowid具有最小的rid。

delete语句删除不是组中第一行的所有行。

答案 1 :(得分:0)

我假设你的意思是第一个,“有最低的摆脱”,在这种情况下,以下应该有效。

;with cte as (
  --put your query here
)
select c1.*
from cte c1
  join (
    select rowID, min(rid) minRID
    from cte
    group by rowID
  ) c2 on c1.rowID=c2.rowID
      and c1.rid = c2.minRID

那就是说,我的直觉反应是你的初始查询可以稍微优化,使一切变得更简单。但是,我不知道没有架构......所以也许不是......

答案 2 :(得分:0)

WITH cte AS 
(
select rowid  from qvalues where rowid in (select rowid from batchinfo where datapath not like  '%thc%' and datapath not like '%pain%' and datapath not like 'tf1' and datapath like '%excel short%') 
and (compound='etg')
and name='qc1'
group by rowid
having (COUNT(rowid)>1)
)
SELECT qvalues.* FROM qvalues INNER JOIN cte ON cte.rowid = qvalues.rowid
WHERE qvalues.rid = (SELECT MIN(rid) from cte where cte.rowid = qvalues.rowid)
ORDER BY rowid

这应该做的工作:) 您使用公用表表达式,一旦完成,您将获得MIN(rid)行。