2011-11-11 73 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 
相关问题