2011-11-11 70 views
0

我有列的表格:ID和創建(日期時間)如何使用SQL獲得具有相同值的行數?

 
id created 
6 2011-11-04 20:32:09.673 
5 2011-11-04 20:32:09.673 
4 2011-11-04 20:29:55.000 
3 2011-11-04 20:29:55.000 

如何寫SQL,將返回的有沒有,有2個或者2個以上等於創建日期的記錄數。它可以是3個獨立的sqls。

謝謝

+0

不太理解你的問題......你想所有的ID顯示在完全相同的創建日期/時間多次出現?正如你所顯示的記錄子集沒有顯示ID 1和2,因爲它們每個都有不同的日期/時間並且不相同。 – DRapp

+0

或..你是否要求...的X數量的條目,你要計數,沒有一個重複的創建日期/時間值... – DRapp

回答

1

如果我正確理解你的問題,我相信這應該做的伎倆,返回出現一次以上任意創建日期。

這一個將獲得那些具有大於2

SELECT 
    created 
    ,COUNT(*) as [occurrences] 
FROM 
    tableName 
GROUP BY 
    created HAVING COUNT(*) > 2 

交換的> 2爲= 2以獲得那些具有恰好2和= 1來獲得那些只有1發生。

+0

我知道了,這是有幫助的。謝謝 – Sergejs

0

嘗試:

select case reccount 
      when 1 then 'No matches' 
      when 2 then 'Exactly 2 matches' 
      else 'More than 2 matches' 
     end as number_of_matches, 
     count(*) as distinct_dates, 
     sum(reccount) as record_count 
from (select created, 
      count(*) reccount 
     from mytable 
     group by created) v 
group by 
case reccount 
      when 1 then 'No matches' 
      when 2 then 'Exactly 2 matches' 
      else 'More than 2 matches' 
     end 
相關問題