-2

我有一个包含以下数据的表:

Id  Name    Value
1   John    100
2   John    -500
3   John    500
4   Smith   10
5   Smith   20
6   Smith   -20
7   Stuart  -10
8   Wills   25

我正在寻找可以删除 John -500 和 Smith -20 的有效 TSQL 查询(即,如果它们在同一组 [group by names] 中具有相似的正值,则记录具有负值)。

4

2 回答 2

1

我认为这就是你所需要的。( SQL 演示)

delete y
from mytable y join (
  select id,name, value
  from mytable x
  where value > 0) z on y.name = z.name and y.value = -1 * z.value 

select * from mytable
--SELECT RESULTS AFTER DELETING
ID  NAME    VALUE
1   John    100
3   John    500
4   Smith   10
5   Smith   20
7   Stuart  -10
8   Wills   25
于 2013-01-24T17:55:20.653 回答
0
delete a
from mytable a 
join mytable b
  on b.name = a.name
 and a.value < 0
 and b.value = -1 * z.value 

几乎和卡夫一样+1

于 2013-01-24T18:57:55.567 回答