2013-01-04 66 views
0

我有一些宕機時間,我正在努力學習SQL。我遇到了一個問題,希望你能幫助我。通過唯一ID來計數事件

比方說,我有這樣一個令人難以置信的簡單表格,它具有每個日期的唯一ID(同樣,這是一個學習練習 - 否則我會使用實際日期而不是ID),然後是家務活我做的那一天:

day_id | chore 

123456 | Washed the dishes 

823454 | Cut the grass 

123456 | Washed the dishes 

123456 | Took out the trash 

324234 | Washed the dishes 

正如你所看到的,這是可能的,你可以做同樣的家務兩次在同一天(在這種情況下,我洗碗兩次123456)。

我怎樣才能得到獨特的天數我洗碗的天數(在這種情況下,答案應該是2)?

+0

集團由'chore'? –

回答

2
SELECT CHORE, COUNT(DISTINCT day_id) cnt 
FROM CHORES 
WHERE CHORE='Washed the dishes' -- remove this to get list of all chores/count 
GROUP BY CHORE 
+0

謝謝,rs。你能解釋一下COUNT函數之後的cnt嗎? – Duplosion

+0

它是該列的別名 –

3
SELECT COUNT(DISTINCT day_id) 
FROM Chores 
WHERE Chore='Washed the dishes' 
2
select count(distinct day_id) 
from your_table 
where chore = 'Washed the dishes'