大家好!
我有一个表TableTime如下:
Barcode Createdate Result
KK1 2007-09-23 01:21:33 NG
KK1 2007-09-23 02:22:33 NG
KK1 2007-09-23 04:36:21 NG
KK2 2007-09-23 05:36:21 OK
KK2 2007-09-23 06:36:21 OK
KK3 2007-09-23 04:34:21 NG
KK1 2007-09-23 04:58:21 OK
KK3 2007-09-23 08:34:21 NG
........
我想找出数据库中同一条码的时间最后的记录,
如表中的结果就应该是:
KK1 2007-09-23 04:58:21 OK
KK2 2007-09-23 06:36:21 OK
KK3 2007-09-23 08:34:21 NG请问这个语句该怎么写?
我有一个表TableTime如下:
Barcode Createdate Result
KK1 2007-09-23 01:21:33 NG
KK1 2007-09-23 02:22:33 NG
KK1 2007-09-23 04:36:21 NG
KK2 2007-09-23 05:36:21 OK
KK2 2007-09-23 06:36:21 OK
KK3 2007-09-23 04:34:21 NG
KK1 2007-09-23 04:58:21 OK
KK3 2007-09-23 08:34:21 NG
........
我想找出数据库中同一条码的时间最后的记录,
如表中的结果就应该是:
KK1 2007-09-23 04:58:21 OK
KK2 2007-09-23 06:36:21 OK
KK3 2007-09-23 08:34:21 NG请问这个语句该怎么写?
解决方案 »
- Oracle 数据库怎么给数据库里面当前时间年份增加1
- oracle按时间查询问题
- 一个PL/SQL问题
- sql update 关联修改
- null值问题,急
- 安装oracle遇到的问题(急)
- !!!急急急,有没有函数像sum一样,对varchar2进行累加呀,每一条记录用逗号分开
- 假设库中有这样的一张表,其中包含startTime, endTime,两个日期类型的。求出所有记录的工作时间长度
- 数据库里sysdate是0005-3-7,怎么处理成2005-3-7,真的蛮急的
- 控制文件恢复
- oracle---sql得到时间差,带有小数~~怎么去掉小數~~急~~~~
- 跨数据库解决方案CowNewSQL发布新版本
where Barcode,Createdate in (
select Barcode ,max(Createdate)
from TableTime
group by Barcode )
即上面查询的结果是
KK3 2007-09-23 08:34:21 NG
from
tabletime a,
(select Barcode ,max(Createdate) creatdate
from TableTime
group by Barcode
) b
where
a.Barcode=b.Barcode
and a.Createdate=b.Createdate
select *
from (
select tt.*,
row_number() over(partition by Barcode order by Createdate desc) as rn
from TableTime tt
)zz
where rn = 1;-------------------------------------------------------------------------如果是NG的情况: select *
from (
select tt.*,
row_number() over(partition by Barcode order by Createdate desc) as rn
from TableTime tt
)zz
where rn = 1
and tt.Result = 'NG';
insert into TableTime
select 'KK1','2007-09-23 01:21:33','NG' from dual
union all
select 'KK1','2007-09-23 02:22:33','NG' from dual
union all
select 'KK1','2007-09-23 04:36:21','NG' from dual
union all
select 'KK2','2007-09-23 05:36:21','OK' from dual
union all
select 'KK2','2007-09-23 06:36:21','OK' from dual
union all
select 'KK3','2007-09-23 04:34:21','NG' from dual
union all
select 'KK1','2007-09-23 04:58:21','OK' from dual
union all
select 'KK3','2007-09-23 08:34:21','NG' from dual;
select * from TableTime A where not exists(select 1 from TableTime T where A.Barcode=T.Barcode and A.Createdate<T.Createdate)
order by A.barcode
测试结果如下:
KK1 2007-09-23 04:58:21 OK
KK2 2007-09-23 06:36:21 OK
KK3 2007-09-23 08:34:21 NG
楼主测试下