这是我的表
Id | ReceiverId | SenderId | Text
-----------+---------------+--------------+-----------
1 | 5 | 1 | text
2 | 5 | 1 | text
3 | 1 | 5 | text
4 | 2 | 5 | text
5 | 2 | 5 | text
6 | 5 | 3 | text
7 | 5 | 4 | text
9 | 5 | 6 | text
10 | 5 | 4 | text
11 | 10 | 5 | text
12 | 5 | 10 | text
13 | 10 | 5 | text
14 | 5 | 10 | text
如何根据[ReceiverId,SenderId]对和按ID排序 降序顺序选择没有重复的行。那就是:[5,1] = [1,5]是重复的。 OR [5,1] = [5,1]也是副本。
所以最终的结果应该是:
Id | ReceiverId | SenderId | Text
-----------+---------------+--------------+-----------
14 | 10 | 5 | text
9 | 5 | 6 | text
7 | 5 | 4 | text
6 | 5 | 3 | text
5 | 2 | 5 | text
3 | 1 | 5 | text
答案 0 :(得分:2)
假设在记录中,您只需检查SenderId
和ReceiverId
(顺序无关紧要)就认为是相同的,那么您需要具有最大Id
的记录(这可能是最新的)。然后,此查询将为您提供结果:
select Id, ReceiverId, SenderId, [Text]
from MyTable t
where t.Id in (
select top 1 tt.Id
from MyTable tt
where (tt.SenderId = t.SenderId and tt.ReceiverId = t.ReceiverId) or
(tt.SenderId = t.ReceiverId and tt.ReceiverId = t.SenderId)
order by tt.Id desc
)
order by t.Id desc
将MyTable
替换为您的表名。
答案 1 :(得分:1)
one of the displayed URL's a substring "test"