select
    t.phone,
    t.re,
    [count] = (select count(1) from 表A where phone=t.phone)
from
    表A t
where
    t.type = 1
    and
    not exists(select 1 from 表A where phone=t.phone and id>t.id)
order by
    t.phone

解决方案 »

  1.   

    上面的SQL有点问题,修正一下:select
        t.phone,
        t.re,
        [count] = (select count(1) from 表A where phone=t.phone and type=t.type)
    from
        表A t
    where
        t.type = 1
        and
        not exists(select 1 from 表A where phone=t.phone and type=t.type and id>t.id)
    order by
        t.phone
      

  2.   

    select phone,max(re) as re,count(*) as count from 表A where type = 1 group by phone
      

  3.   

    select phone
           ,max(re) as 're'
           ,count(1) as 'count'
    from A
    where type=1
    group by phone
    order by phone