小弟最近在写一个考勤系统与CRM同步的软件,但发现考勤系统中有好多个重复记录,我想利用SQL语句查出当天重复记录中最小的日期的记录,并显示出来,小弟SQL是初学者,希望大家帮帮忙,谢谢!
例:
表"刷卡记录"
字段:
ID        卡号       刷卡日期 
---------------------------------
1.        55303     2009-08-01 08:31
2.        55303     2009-08-01 08:32
3.        55303     2009-08-01 08:33
我想得到的结果是:
ID        卡号       刷卡日期 
---------------------------------
1.        55303     2009-08-01 08:31

解决方案 »

  1.   

    select 卡号,min(刷卡日期 ) as 刷卡日期  
    from tb group by 卡号
      

  2.   

    select * 
    from tb t
    where not exists(select * from tb where t.刷卡日期>刷卡日期)
      

  3.   

    select * 
    from tb t
    where not exists(select * from tb where 卡号=t.卡号 and t.刷卡日期>刷卡日期)
      

  4.   

    select * from tb a 
    where not exists(select 1 from tb where 卡号=a.卡号 and  刷卡日期<a.刷卡日期)
      

  5.   

    select * 
    from tb t
    where not exists(select * from tb where 卡号=t.卡号 and t.刷卡日期>刷卡日期)
      

  6.   

    SELECT * FROM TB T1
    WHERE NOT EXISTS(
    SELECT 1 FROM TB T2 WHERE T1.卡号=T2.卡号 AND T1.刷卡日期 >T2.刷卡日期 
      

  7.   

    喵了个咪的,怎么都这么快>_<
      

  8.   

    select * 
    from tb t
    where 刷卡日期=(select min(刷卡日期) from tb where t.卡号=卡号)
      

  9.   

    1、select 卡号,min(刷卡日期 ) as 刷卡日期  
    from tb group by 卡号
    2、select * from tb t where 
    not exists
    (select* from tb where卡号=t.卡号 and t.刷卡日期>刷卡日期) 
      

  10.   

    */
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    go--一、按name分组取val最大的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
    --方法3:
    select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          3           a3:a的第三个值
    b          5           b5b5b5b5b5
    */
      

  11.   

    小弟最近在写一个考勤系统与CRM同步的软件,但发现考勤系统中有好多个重复记录,我想利用SQL语句查出当天重复记录中最小的日期的记录,并显示出来,小弟SQL是初学者,希望大家帮帮忙,谢谢! 
    例: 
    表"刷卡记录" 
    字段: 
    ID        卡号      刷卡日期 
    --------------------------------- 
    1.        55303    2009-08-01 08:31 
    2.        55303    2009-08-01 08:32 
    3.        55303    2009-08-01 08:33 
    4.        55303    2009-08-02 09:01
    5.        55303    2009-08-03 08:33我想得到的结果是: 
    ID        卡号      刷卡日期 
    --------------------------------- 
    1.        55303    2009-08-01 08:31 
    小弟又加了10分,很多朋友的代码写的都很好,但小弟要统计的是每天重复记录中的最小日期。
    小弟要查询的是2009-08-01当天每个重复卡号的最小日期时间。