SQL语句菜鸟问题,高手们进来抢分啦!!!!!

AskValue 2004-11-22 09:08:03
表结构如下:
CardNO IOtime TYPE
001 13:20:30 IN
002 13:25:20 OUT
002 13:26:22 IN
001 13:27:30 OUT
003 13:28:01 IN
001 13:29:56 IN
001 13:30:33 OUT
用一段Access数据库的SQL语句求出每一个编号最大时间值并且TYPE值为"IN",结果按CardNO升序排列。即按上表结果应为:
CardNO IOtime TYPE
002 13:26:22 IN
003 13:28:01 IN

...全文
239 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
long111 2004-11-24
  • 打赏
  • 举报
回复
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'
go

select *
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
我的方法和楼上的一样啊
liao5288 2004-11-23
  • 打赏
  • 举报
回复
select * from temp as t1
where
iotime=(select max(iotime) from tmpe
where cardno=t1.cardno) and
type='in'
order by cardno
我试过了,这种方法能行!
futulove 2004-11-22
  • 打赏
  • 举报
回复
偶抢
zhang_yzy 2004-11-22
  • 打赏
  • 举报
回复
感觉还是有些问题,
改一下
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'
zhang_yzy 2004-11-22
  • 打赏
  • 举报
回复
楼上写的正确,不过应该注意一下,在 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'
jackluo1981 2004-11-22
  • 打赏
  • 举报
回复
To:qizhanfeng(glacier)
不好意思,你的是对的!
jackluo1981 2004-11-22
  • 打赏
  • 举报
回复
谁来运行一下上面的代码,我怎么运行出来的不是
002 13:26:22 IN
003 13:28:01 IN
qizhanfeng 2004-11-22
  • 打赏
  • 举报
回复
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

jackluo1981 2004-11-22
  • 打赏
  • 举报
回复
帮你顶!
AskValue 2004-11-22
  • 打赏
  • 举报
回复
前边几位没认真读题哈~
AskValue 2004-11-22
  • 打赏
  • 举报
回复
好像这一句才是对的,是不是因为ACCESS跟MY-SQL的语法是有区别的??
select carno,max(iotime),'IN' as TYPE from table where type='IN' group by carno
jackluo1981 2004-11-22
  • 打赏
  • 举报
回复
那liupingsk(往事随风.......) 的语句就不对了吧!
AskValue 2004-11-22
  • 打赏
  • 举报
回复
不是,没有001,因为001最大时间的type为"OUT"
jackluo1981 2004-11-22
  • 打赏
  • 举报
回复
结果是这样吧:
001 13:29:56.000 IN
002 13:26:22.000 IN
003 13:28:01.000 IN
AskValue 2004-11-22
  • 打赏
  • 举报
回复
我不明白是不是因为Access数据库的原因,系统提示:
试图执行的查询中不包含作为合计函数一部分的特定表达式 'TYPE' 。
Selectau_fname 2004-11-22
  • 打赏
  • 举报
回复
呵呵

up
AskValue 2004-11-22
  • 打赏
  • 举报
回复
我来try try...
davorsuker39 2004-11-22
  • 打赏
  • 举报
回复
同意
ykily 2004-11-22
  • 打赏
  • 举报
回复
fenlin 2004-11-22
  • 打赏
  • 举报
回复
一楼正解
加载更多回复(3)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧