2013-07-09 65 views
-3

我面對的是非常困難的pb,我沒有找到實現這一目標的方法,我想知道有多少次發生等於member1(id1)的發生次數,正如你在這裏看到的, id2有4次平等發生ID1和ID3的只有1 egual occurence:is this query COUNT is impossible?

我的表測試:

id classement aptitude A  B C 
1 2440    oui  2  9 1 
2 2440    oui  2  9 5 
3 1760    oui  1  8 5 
4 2440   oui  2  9 1 

在本例,結果expeted應該是:

id count 
4  5 
2  4 
3  1 

請告訴我這種查詢可以實現它,請,有可能嗎?!

+5

不明白你想要什麼 –

+2

'id2有4個同樣的發生'?這怎麼樣?你能解釋一下這個4怎麼來 –

+0

id2和ID1在這一行中都有4次出現:classement,aptitude,A,B等於id1和id2 – jess

回答

2

嘗試:

select T2.id, 
     coalesce((T1.classement=T2.classement),0)+ 
     coalesce((T1.aptitude=T2.aptitude),0)+ 
     coalesce((T1.A=T2.A),0)+ 
     coalesce((T1.B=T2.B),0)+ 
     coalesce((T1.C=T2.C),0) match_count 
from Temp T1 
cross join Temp T2 
where T1.id = 1 and T2.id <> 1 
order by 2 desc 

SQLFiddle here

+0

非常感謝,我正在做一些測試來比較解決方案 – jess

+0

@ mark,你的解決方案看起來不錯,只有在字段不爲NULL時纔有可能計數嗎? – jess

+0

@jess:你可以用'0'合併每個條件 - 查看更新後的答案。 –

-1

這可能會實現:

select a.id, 
     length((case when a.aptitude = b.aptitude then '1' else '' end) || 
       (case when a.classement = b.classement then '1' else '' end) || 
       (case when a.A = b.A then '1' else '' end) || 
       (case when a.B = b.B then '1' else '' end) || 
       (case when a.C = b.C then '1' else '' end)) count 
    from test a, 
     (select * 
      from test 
     where id = 1) b 
where a.id != 1 
+0

謝謝你們,我正在做一些測試,以copare兩種解決方案 – jess

相關問題