2014-06-10 56 views
1

我有一個表,看起來像這樣:需要一個計數,但多個其他領域

person trip_id date home  destination 
joe  1   3/10 chicago new york 
joe  2   4/10 chicago l.a. 
joe  3   5/10 chicago boston 
luther 4   3/12 new york chicago 
luther 5   3/18 new york boston 

我想得到這樣

person trips firstDate home 
joe  3  3/10  chicago 
luther 2  3/12  new york 

目前我有 選擇 人結果, count(trip_id)as trips, min(date)as firstDate from [table] group by person order by firstDate

我無法弄清楚如何在那裏回家。

家對於這個人來說總是獨一無二的。但我的DBMS不知道這一點。

是否有解決此問題的簡單方法?

欣賞它。

+0

你試圖在輸出中回家的是什麼,當你嘗試過時發生了什麼? – dma

回答

2
Select person, count(trip_id) as trips, min(date) as firstDate, home 
from [table] 
group by person, home 
order by firstDate 
+0

哇。不會有這樣的想法。謝謝! – user3727752