2011-07-26 40 views
1

表佈局可以得到記錄集我想,尋找諮詢

id, inactive (Boolean), name 
12500, f, foo 
12345, f, foo 
12344, f, foo 
12343, f, foo 
12342, t, foo 
12..., t, foo (more records) 
12200, f, bar 
12005, f, bar 
12004, f, bar 
12003, f, bar 
12002, t, bar 
12..., t, bar (more records) 
..............(more records with different names) 

結果:

  • 的名字需要組
  • 只需要不活躍= F
  • 極品拳頭不活躍= f,id
  • 需要計算每個組有多少條記錄,inactive = f
從示例數據

所以以上我會得到的結果集的:

id, inactive (Boolean), name, count 
12343, f, foo, 157 
12003, f, bar, 197 
............. (any other names that fall into the above constraints) 

在正確的方向任何幫助將是巨大的

+0

首先建議:寫一個sql狀態噸。那麼當它不起作用時,請尋求幫助。 – Randy

+0

感謝Randy的建議,我確實寫了幾個查詢,沒有任何工作,因爲我想。因此我問的問題。我認爲有人可以更容易地看到所需的結果而不是我的查詢 –

回答

1

嘗試

select min (id), inactive, name, count(*) from tabke where inactive = 'f' group by inactive, name 

編輯:

select b.minid, a.inactive, a.name, a.cnt from 
(select inactive, name, count(*) cnt from table where inactive = 'f' group by inactive, name) a, 
(select inactive, name, min(id) minid from table where inactive = 'f' group by inactive, name) b 
where a.inactive = b.inactive and a.name = b.name 
+0

謝謝,我認爲我需要向羣組添加更多信息,但您的第一個解決方案效果很好 –