2011-08-31 87 views
1

我有一个表,其中包含s.noIdAmountaccCode需要帮助,以过滤sql中的数据

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 

我需要所有的都有一个共同的AmountaccCodeid记录。在这种情况下,我需要显示S.NO 2和4以及1和6的数据,因为它们具有相似的值。如果可能的话,similar data come orderly会更好。这是一个可能通过Sql?请提供一些提示我提前卡住了这个。

+0

类似的数据来有序:你是什么意思更精确? –

回答

0

一种解决方案可能是,假设你的表名为“测试”

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] 
+0

将'<>'更改为'<'并且您只获得每个结果一次(而不是将1,6和6,1分别作为两个单独的行)。 –