select distinct(kh) ,xm,cph,klx,max(jcrq),sfgq,images from jcbb where ccrq is null group by kh,xm,cph,klx,sfgq,images order by max(jcrq) desc
用这条语句怎么么不能消除重复值呢?结果如下:kh      xm      cph     klx      jcrq                    sfgq     images  
8812 张文 43146 固定卡 2008-05-15 17:19:01.000 未过期 A29883.jpg
8812 张文 43146 固定卡 2008-05-15 17:19:00.000 未过期 A19579.jpg
8812 张文 43146 固定卡 2008-05-15 17:18:55.000 未过期 A72152.jpg
8812 张文 43146 固定卡 2008-05-15 17:17:24.000 未过期 A21557.jpg
8812 张文 43146 固定卡 2008-05-15 17:17:23.000 未过期 A26375.jpg
8812 张文 43146 固定卡 2008-05-15 17:17:22.000 未过期 A91560.jpg
665610 临时卡 临时卡 临时卡 2008-05-15 17:17:20.000 A71893.jpg
665610 临时卡 临时卡 临时卡 2008-05-15 17:17:17.000 A63013.jpg
665610 临时卡 临时卡 临时卡 2008-05-15 17:17:16.000 A67565.jpg
665610 临时卡 临时卡 临时卡 2008-05-15 17:17:14.000 A60650.jpg
我只要不同卡号按日期倒序排列的第一条记录啊。
kh      xm      cph     klx      jcrq                    sfgq     images  
8812 张文 43146 固定卡 2008-05-15 17:19:01.000 未过期 A29883.jpg
665610 临时卡 临时卡 临时卡 2008-05-15 17:17:20.000 A71893.jpg

解决方案 »

  1.   


    select * from jcbb a where a.ccrq is null  and a.jcrq =(select max(jcrq) from jcbb b where a.kh = b.kh ) 
      

  2.   

    select * from jcbb
    where not exists(select 1 from jcbb j where kh=jcbb.kh and jcrq>jcbb.jcrq)
      

  3.   

    select b.* from 
    (
    select kh,xm,cph,klx,max(jcrq) as jcrq,sfgq 
    from jcbb 
    where ccrq is null 
    group by kh,xm,cph,klx,sfgq 
    ) a
    inner join 
    jcbb b
    on a.kh=b.kh and a.jcrq=b.jcrq
    order by b.jcrq
    --
    try
    接分
      

  4.   

    select jcbb.kh,jcbb.xm,jcbb.cph,jcbb.klx,jcbb.jcrq,jcbb.sfgq,jcbb.images from jcbb,
    (select B.kh,jcrq = min(B.jcrq) FROM jcbb B group by B.kh)A
    where jcbb.kh = A.kh AND jcbb.jcrq = A.jcrq    
      

  5.   

    select * from jcbb a
    where not exists(select 1 from jcbb b
    where a.kh=b.kh and a.jcrq<b.jcrq)select * from jcbb a
    where a.jcrq in (select max(b.jcrq) from jcbb b where a.kh=b.kh)select * from jcbb a
    where a.jcrq  in (select top 1 b.jcrq from jcbb b where a.kh=b.kh
    order by b.jcrq desc)