SQL> select * from test;ID S DAY
--- - -------------------
001 A 2005-04-06 07:07:00
002 A 2005-04-06 07:08:00
001 B 2005-04-06 07:09:00
001 A 2005-04-06 07:10:00
001 B 2005-04-07 07:11:00
002 B 2005-04-06 07:12:00已选择6行。SQL> select id,status,max(day) from test where id=&id and status=decode(&status,
'A','B','B','A') and day<&day group by id,status;
输入 id 的值: '001'
输入 status 的值: 'B'
输入 day 的值: to_date('2005-04-06 07:09:00','yyyy-mm-dd hh24:mi:ss')
原值 1: select id,status,max(day) from test where id=&id and status=decode(&s
tatus,'A','B','B','A') and day<&day group by id,status
新值 1: select id,status,max(day) from test where id='001' and status=decode(
'B','A','B','B','A') and day<to_date('2005-04-06 07:09:00','yyyy-mm-dd hh24:mi:s
s') group by id,statusID S MAX(DAY)
--- - -------------------
001 A 2005-04-06 07:07:00SQL> select id,status,max(day) from test where id=&id and status=decode(&status,
'A','B','B','A') and day<&day group by id,status;
输入 id 的值: '001'
输入 status 的值: 'A'
输入 day 的值: to_date('2005-04-06 07:10:00','yyyy-mm-dd hh24:mi:ss')
原值 1: select id,status,max(day) from test where id=&id and status=decode(&s
tatus,'A','B','B','A') and day<&day group by id,status
新值 1: select id,status,max(day) from test where id='001' and status=decode(
'A','A','B','B','A') and day<to_date('2005-04-06 07:10:00','yyyy-mm-dd hh24:mi:s
s') group by id,statusID S MAX(DAY)
--- - -------------------
001 B 2005-04-06 07:09:00
--- - -------------------
001 A 2005-04-06 07:07:00
002 A 2005-04-06 07:08:00
001 B 2005-04-06 07:09:00
001 A 2005-04-06 07:10:00
001 B 2005-04-07 07:11:00
002 B 2005-04-06 07:12:00已选择6行。SQL> select id,status,max(day) from test where id=&id and status=decode(&status,
'A','B','B','A') and day<&day group by id,status;
输入 id 的值: '001'
输入 status 的值: 'B'
输入 day 的值: to_date('2005-04-06 07:09:00','yyyy-mm-dd hh24:mi:ss')
原值 1: select id,status,max(day) from test where id=&id and status=decode(&s
tatus,'A','B','B','A') and day<&day group by id,status
新值 1: select id,status,max(day) from test where id='001' and status=decode(
'B','A','B','B','A') and day<to_date('2005-04-06 07:09:00','yyyy-mm-dd hh24:mi:s
s') group by id,statusID S MAX(DAY)
--- - -------------------
001 A 2005-04-06 07:07:00SQL> select id,status,max(day) from test where id=&id and status=decode(&status,
'A','B','B','A') and day<&day group by id,status;
输入 id 的值: '001'
输入 status 的值: 'A'
输入 day 的值: to_date('2005-04-06 07:10:00','yyyy-mm-dd hh24:mi:ss')
原值 1: select id,status,max(day) from test where id=&id and status=decode(&s
tatus,'A','B','B','A') and day<&day group by id,status
新值 1: select id,status,max(day) from test where id='001' and status=decode(
'A','A','B','B','A') and day<to_date('2005-04-06 07:10:00','yyyy-mm-dd hh24:mi:s
s') group by id,statusID S MAX(DAY)
--- - -------------------
001 B 2005-04-06 07:09:00
ID STATUS DATE USE WAY
001 A 2005-04-06 07:07 中国 北京
002 A 2005-04-06 07:08 泰国 曼谷
001 B 2005-04-06 07:09 美国 纽约
001 A 2005-04-06 07:10 湖北 武汉
001 B 2005-04-07 07:11 江西 加拿大
002 B 2005-04-06 07:12 吉林 兰州TABLE2
USER WAY OVER
中国 北京 5
泰国 曼谷 6
美国 纽约 7
湖北 武汉 8
江西 加拿大 9
吉林 兰州 10
简单说 现在已只某条 B 状态的已知 ID 数据的数据,求它配套的状态为 A ,相同 ID 的数据!
比如:已知 第三条数据,如何找到 第一条数据!取得 USE,WAY 到另一个表 table 2 取得对应的数据 OVER 更新到 第三条记录对应的数据上 。
EG 想得到
ID STATUS OVER
001 B 5
select USE,WAY from table1 a where (id,date) in (select id,max(date) from a where a.id=&id));
不知道楼主说的是不是这样的?