select cardno,max(iotime),type from table where type = 'in' group by cardno,type order by cardno

解决方案 »

  1.   

    我不明白是不是因为Access数据库的原因,系统提示:
    试图执行的查询中不包含作为合计函数一部分的特定表达式 'TYPE' 。
      

  2.   

    结果是这样吧:
    001    13:29:56.000 IN
    002    13:26:22.000 IN
    003    13:28:01.000 IN
      

  3.   

    不是,没有001,因为001最大时间的type为"OUT"
      

  4.   

    那liupingsk(往事随风.......) 的语句就不对了吧!
      

  5.   

    好像这一句才是对的,是不是因为ACCESS跟MY-SQL的语法是有区别的??
    select carno,max(iotime),'IN' as TYPE from table where type='IN' group by carno
      

  6.   

    create table temp(CardNO char(8),     IOtime datetime,          TYPE char(4))insert into temp values('001',      #13:20:30#,          'IN')
    insert into temp values('001',      #13:20:30#,          'IN')
    insert into temp values( '002',      #13:25:20#,          'OUT')
    insert into temp values( '002',      #13:26:22#,          'IN')
    insert into temp values( '001',      #13:27:30#,          'OUT')
    insert into temp values( '003',      #13:28:01#,          'IN')
    insert into temp values( '001',      #13:29:56#,          'IN')
    insert into temp values( '001',      #13:30:33#,          'OUT')测试
    select * from temp 
    where
    exists(select 1 from 
       (
       SELECT cardno,max(iotime) as iotime1 from temp group by cardno
       ) A where cardno=A.cardno and iotime=A.iotime1
    ) and type='in'结果
    CardNO     IOtime          TYPE 
     002      13:26:22          IN
     003      13:28:01          IN
      

  7.   

    谁来运行一下上面的代码,我怎么运行出来的不是
     002      13:26:22          IN
     003      13:28:01          IN
      

  8.   

    To:qizhanfeng(glacier)
       不好意思,你的是对的!
      

  9.   

    楼上写的正确,不过应该注意一下,在 ACCESS 中TYPE是关键字,改一下为:
    select * 
      from temp a
    where exists(select 1 
                   from ( select cardno,max(iotime) as iotime1 
                            from temp 
                          group by cardno
                         ) b 
                   where a.cardno=b.cardno 
                     and a.iotime=b.iotime1
                 ) 
        and [type]='IN'
      

  10.   

    感觉还是有些问题,
    改一下
    select b.*
      from (select cardno,max(iotime) as iotem
              from temp 
            group by cardno ) a inner join temp b on b.cardno=a.cardno and b.iotime=a.iotime
    where [type]='IN'
      

  11.   

    sorry,偶前面没看清,改正:
    select a.cardno,a.iotime,#test_1.type
    from
    (
    select cardno,max(iotime) as iotime
    from #test_1
    group by cardno
    ) a ,#test_1
    where a.cardno=#test_1.cardno and a.iotime=#test_1.iotime and #test_1.type='in'
      

  12.   

    select * from temp as t1
    where
    iotime=(select max(iotime) from tmpe
    where cardno=t1.cardno) and
    type='in'
    order by cardno
    我试过了,这种方法能行!
      

  13.   

    create table t(cardNO char(8) ,IOtime datetime, TYPE char(4))
    insert into t
    select  '001','13:20:30 ','IN' union all
    select ' 002','13:25:20','OUT' union all
    select '002','13:26:22','IN'   union all
    select '001','13:27:30','OUT'  union all
    select '003','13:28:01','IN'   union all
    select '001','13:29:56','IN'   union all
    select '001','13:30:33','OUT'
    goselect *
    from t t1
    where IOtime=(select max(IOtime) from t where t.cardNO=t1.cardNO)
    and TYPE='IN'
    order by cardNO asc
    drop table t
    我的方法和楼上的一样啊