号码   时间
aaa    2005/12/12
bbb    2006/1/12
aaa    2007/3/3
ccc    2007/3/1
aaa    2007/4/1
bbb    2007/1/1
ddd    2005/1/1
... ...
要求写出一条语句,查出号码中出现次数最多的前2位和最后出现的时间!
就是要这么个结果
号码   时间
aaa  2007/4/1
bbb  2007/1/1

解决方案 »

  1.   

    select top 2 号码,时间 from (select 号码,max(时间),count(1) as cs from table1 group by 号码) t order by cs desc
      

  2.   

    Select
    A.号码,
    Max(A.时间) As 时间
    From
    TabelName A
    Inner Join
    (Select TOP 2 号码 From TabelName Group By 号码 Order By Count(号码) Desc) B
    On A.号码 = B.号码
      

  3.   

    select * from ta 
    where 号码 in(select top 2 号码 from ta group by 号码 order by count(1)desc)
      

  4.   

    Or
    Select
    A.号码,
    Max(A.时间) As 时间
    From
    TabelName A
    Where 号码 In (Select TOP 2 号码 From TabelName Group By 号码 Order By Count(号码) Desc) 
      

  5.   

    寫複雜了Select TOP 2 号码, Max(时间) As 时间 From TabelName Group By 号码 Order By Count(号码) Desc, 时间 Desc
      

  6.   

    select top 2 munber,count(*) as a,max(time) from table 
    group by munber 
    order by a desc
      

  7.   

    declare @t table(col1 char(3),col2 datetime)
    insert @t select 'aaa','2005/12/12'
    union all select 'bbb','2006/1/12'
    union all select 'aaa','2007/3/3'
    union all select 'ccc','2007/3/1'
    union all select 'aaa','2007/4/1'
    union all select 'bbb','2007/1/1'
    union all select 'ddd','2005/1/1'select top 2 col1,max(col2) from @t group by col1 order by count(col1) desc
      

  8.   

    Select Top 2 P_code,Max(P_date)
     From Table_Pqs
     Group By P_Code
     Order By Count(P_Code) Desc
      

  9.   

    create table jj
    (
       code  varchar(4),
       s_time datetime
    )insert jj select 'aaa','2005/12/12'
    union all select 'bbb','2006/1/12'
    union all select 'aaa','2007/3/3'
    union all select 'ccc','2007/3/1'
    union all select 'aaa','2007/4/1'
    union all select 'bbb','2007/1/1'
    union all select 'ddd','2005/1/1'select top 2 code ,max(s_time) from jj
    group by code
    order by count(code) desc
      

  10.   

    楼主的意思是次数最多出现,但未必是最大的max。
    恩,这个不好办。
      

  11.   

    select   b.id,max(a.t_time) from tbl a
    inner join  (select  distinct top 2 id,count(id) as num from tbl
    group by id
    order by count(id) desc) b
    on a.id=b.id
    group by b.id
      

  12.   

    select max(时间),号码,count(号码) from 表名 group by 号码
      

  13.   

    select top 2 max(时间),号码,count(号码) from 表名 group by 号码