帮助删除具有困难t-sql查询的值

帮助删除具有困难t-sql查询的值,sql,sql-server,tsql,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%')

上述语句返回以下内容:

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
我只想保留具有第一个唯一ROWID的行,并删除其余的行。如何在第一次出现唯一的ROWID之后删除每条记录???

这可能会有所帮助

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
查询使用公共表表达式(CTE)获取行,并为每个rowid组添加一个序列号。顺序由rid排序决定。因此,第一个rowid具有最小的rid


delete语句将删除组中所有非第一行的行。

我假定您指的是第一行,如“具有最低的rid”,在这种情况下,应使用以下方法

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
也就是说,我的直觉反应是,您的初始查询可以进行某种程度的优化,使一切变得更简单。但是,我不知道没有模式。。。所以也许不是

;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
这应该可以完成任务:)
您使用一个通用的表表达式,一旦完成,就可以得到带有MIN(rid)的行。

Huh,我从未想过通过CTE从表中删除值。。。我必须记住这一点。虽然我读了“删除所有其他内容”来暗示“从结果中”。
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