Tsql 需要在sql中筛选数据的帮助吗

Tsql 需要在sql中筛选数据的帮助吗,tsql,sql-server-2008,Tsql,Sql Server 2008,我有一个表,其中包含s.noId和Amount和accCode s.no-------------id--------------Amount--------accCode 1----------------2---------------20-------------2.1 2----------------1---------------30-------------2.1 3--------------- 5---------------20-------------

我有一个表,其中包含
s.no
Id
Amount
accCode

  s.no-------------id--------------Amount--------accCode
   1----------------2---------------20-------------2.1
   2----------------1---------------30-------------2.1
   3--------------- 5---------------20-------------3.1
   4----------------1---------------30-------------2.1
   5----------------3---------------40-------------3.1
   6----------------2---------------20-------------2.1

我需要所有具有公共
金额
accCode
id
的记录。在这种情况下,我需要显示2号和4号,以及1号和6号的数据,因为它们具有相似的值。如果可能的话,
类似的数据最好是有序的
。这是通过Sql实现的吗?请给我一些提示,我已经被这个问题困住了。请提前回答。

一个解决方案可能是,假设您的表名为“test”

从测试t1、测试t2中选择t1.*.t2.[s.no]作为匹配sno
其中t1.id=t2.id和t1.amount=t2.amount
t1.acccode=t2.acccode和t1[s.no]t2[s.no]
按t1.id、t1.Amount、t1.accCode、[s.no]订购

类似的数据依次出现:您更准确的意思是什么?将
更改为

select t1.*, t2.[s.no] as MatchSNo from test t1, test t2
where t1.id = t2.id and t1.amount = t2.amount
and t1.acccode = t2.acccode and t1.[s.no] <> t2.[s.no]
order by t1.id, t1.Amount, t1.accCode, [s.no]