解决方案 »
- oracle怎样查看当前用户下所有的序列
- 求字符串合并问题,谢谢
- 求助:oracle继承对象查询的问题
- 如何在oracle里某个表中查询是否有按某个字段创建的索引?
- Oracle10G 在windows的安装问题
- 请教一个查询语句:未知表结构除了已知一个字段。查询除掉这个字段的所有纪录。
- ORA错误
- 在ORACLE中建快照时storage(initial 512 next 512 pctincrease 10) refresh fast sysdate + 1时总是报错
- 急!关于用full join产生sql语句报错的问题,请高手赐教!!!
- 盼高手帮助解决
- 虚心求教一个异常的解决方案: ORA-00937: not a single-group group function
- 求sql高手帮忙优化一个oracle的sql。
mysql> create table t3(id char(4), time datetime, address char(2));
Query OK, 0 rows affected (0.00 sec)mysql> insert into t3 values('0202', '2010-6-2 8:37:02', 'A');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0058', '2010-6-2 8:37:02', 'A');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0084', '2010-6-2 8:37:01', 'A');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('B300', '2010-6-2 8:37:02', 'A');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0058', '2010-6-2 8:37:29', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0084', '2010-6-2 8:37:29', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('B300', '2010-6-2 8:37:06', 'A');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('8AF6', '2010-6-2 8:37:16', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0202', '2010-6-2 8:37:57', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0058', '2010-6-2 8:37:57', 'B');
Query OK, 1 row affected (0.02 sec)mysql> insert into t3 values('0084', '2010-6-2 8:37:57', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('8AF6', '2010-6-2 8:37:57', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('B300', '2010-6-2 8:37:56', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('8AF6', '2010-6-2 8:38:24', 'B');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('B300', '2010-6-2 8:38:07', 'A');
Query OK, 1 row affected (0.01 sec)mysql> insert into t3 values('0202', '2010-6-2 8:38:24', 'A');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0009', '2010-6-2 8:38:24', 'A');
Query OK, 1 row affected (0.00 sec)mysql> insert into t3 values('0058', '2010-6-2 8:38:24', 'A');
Query OK, 1 row affected (0.00 sec)mysql> select id, time, address from t3 where id in
-> (
-> select a.id from (select id, max(time) t, address from t3 where address='A' group by id) a , (select id, min(time) t, address from t3 where address='B' group by id) b where a.id = b.id and a.t<b.t
-> union
-> select distinct id from t3 a where not exists (select * from t3 b where b.address!=a.address and b.id = a.id)
-> )
-> order by id, address, time;
+------+---------------------+---------+
| id | time | address |
+------+---------------------+---------+
| 0009 | 2010-06-02 08:38:24 | A |
| 0084 | 2010-06-02 08:37:01 | A |
| 0084 | 2010-06-02 08:37:29 | B |
| 0084 | 2010-06-02 08:37:57 | B |
| 8AF6 | 2010-06-02 08:37:16 | B |
| 8AF6 | 2010-06-02 08:37:57 | B |
| 8AF6 | 2010-06-02 08:38:24 | B |
+------+---------------------+---------+
7 rows in set (0.00 sec)mysql>
SQL> create table t3(id char(4), time date, address char(2));表已创建。SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';会话已更改。SQL> insert into t3 values('0202', '2010-6-2 8:37:02', 'A');已创建 1 行。SQL> insert into t3 values('0058', '2010-6-2 8:37:02', 'A');已创建 1 行。SQL> insert into t3 values('0084', '2010-6-2 8:37:01', 'A');已创建 1 行。SQL> insert into t3 values('B300', '2010-6-2 8:37:02', 'A');已创建 1 行。SQL> insert into t3 values('0058', '2010-6-2 8:37:29', 'B');已创建 1 行。SQL> insert into t3 values('0084', '2010-6-2 8:37:29', 'B');已创建 1 行。SQL> insert into t3 values('B300', '2010-6-2 8:37:06', 'A');已创建 1 行。SQL> insert into t3 values('8AF6', '2010-6-2 8:37:16', 'B');已创建 1 行。SQL> insert into t3 values('0202', '2010-6-2 8:37:57', 'B');已创建 1 行。SQL> insert into t3 values('0058', '2010-6-2 8:37:57', 'B');已创建 1 行。SQL> insert into t3 values('0084', '2010-6-2 8:37:57', 'B');已创建 1 行。SQL> insert into t3 values('8AF6', '2010-6-2 8:37:57', 'B');已创建 1 行。SQL> insert into t3 values('B300', '2010-6-2 8:37:56', 'B');已创建 1 行。SQL> insert into t3 values('8AF6', '2010-6-2 8:38:24', 'B');已创建 1 行。SQL> insert into t3 values('B300', '2010-6-2 8:38:07', 'A');已创建 1 行。SQL> insert into t3 values('0202', '2010-6-2 8:38:24', 'A');已创建 1 行。SQL> insert into t3 values('0009', '2010-6-2 8:38:24', 'A');已创建 1 行。SQL> insert into t3 values('0058', '2010-6-2 8:38:24', 'A');已创建 1 行。SQL> select id, time, address from t3 where id in
2 (
3 select a.id from (select id, max(time) t from t3 where address='A' group by id) a , (select id, min(time) t from t3 where address='B' group by id) b where a.id = b.id and a.t<b.t
4 union
5 select distinct id from t3 a where not exists (select * from t3 b where b.address!=a.address and b.id = a.id)
6 )
7 order by id, address, time;ID TIME AD
---- ------------------- --
0009 2010-06-02 08:38:24 A
0084 2010-06-02 08:37:01 A
0084 2010-06-02 08:37:29 B
0084 2010-06-02 08:37:57 B
8AF6 2010-06-02 08:37:16 B
8AF6 2010-06-02 08:37:57 B
8AF6 2010-06-02 08:38:24 B已选择7行。SQL>
B300 2010-6-2 8:37:06 A
B300 2010-6-2 8:37:56 B
B300 2010-6-2 8:38:07 A因为存在
B300 2010-6-2 8:37:06 A
B300 2010-6-2 8:37:56 B所以 B300 也应该算作是符合要求的一条id,但你的结果好像没有列出来呀?!
select id,time,lead(time) over(partition by id order by id) time1,
address,lead(address) over(partition by id order by id) address1
from(
select * from testA order by id,time,address
)
) where address = 'A' and address1 = 'B'
0058 2010-6-2 8:37:29 B0084 2010-6-2 8:37:01 A
0084 2010-6-2 8:37:29 B0202 2010-6-2 8:37:02 A
0202 2010-6-2 8:37:57 BB300 2010-6-2 8:37:06 A
B300 2010-6-2 8:37:56 B
2 (
3 select * from
4 (
5 select t3.*,
6 lag(address) over(partition by id order by time) b_address,
7 lag(time) over(partition by id order by time) b_time
8 from t3
9 )
10 where address = 'B ' and b_address = 'A '
11 )
12 union all
13 select id,b_time,b_address from
14 (
15 select * from
16 (
17 select t3.*,
18 lag(address) over(partition by id order by time) b_address,
19 lag(time) over(partition by id order by time) b_time
20 from t3
21 )
22 where address = 'B ' and b_address = 'A '
23 )
24 order by id,time
25 ;
ID TIME ADDRESS
---- -------------------- -------
0058 2010-6-2 8:37:02 A
0058 2010-6-2 8:37:29 B
0084 2010-6-2 8:37:01 A
0084 2010-6-2 8:37:29 B
0202 2010-6-2 8:37:02 A
0202 2010-6-2 8:37:57 B
B300 2010-6-2 8:37:06 A
B300 2010-6-2 8:37:56 B
8 rows selected
SQL> select * from t3;
ID TIME ADDRESS
---- -------------------- -------
0202 2010-6-2 8:37:02 A
0058 2010-6-2 8:37:02 A
0084 2010-6-2 8:37:01 A
B300 2010-6-2 8:37:02 A
0058 2010-6-2 8:37:29 B
0084 2010-6-2 8:37:29 B
B300 2010-6-2 8:37:06 A
8AF6 2010-6-2 8:37:16 B
0202 2010-6-2 8:37:57 B
0058 2010-6-2 8:37:57 B
0084 2010-6-2 8:37:57 B
8AF6 2010-6-2 8:37:57 B
B300 2010-6-2 8:37:56 B
8AF6 2010-6-2 8:38:24 B
B300 2010-6-2 8:38:07 A
0202 2010-6-2 8:38:24 A
0009 2010-6-2 8:38:24 A
0058 2010-6-2 8:38:24 A
18 rows selected
(
select * from
(
select t3.*,
lag(address) over(partition by id order by time) b_address,
lag(time) over(partition by id order by time) b_time
from t3
)
where address = 'B' and b_address = 'A'
)
union all
select id,b_time,b_address from
(
select * from
(
select t3.*,
lag(address) over(partition by id order by time) b_address,
lag(time) over(partition by id order by time) b_time
from t3
)
where address = 'B' and b_address = 'A'
)
order by id,time
不能过滤掉,因为是按时间从早到晚(升序)的顺序计算,只要出现了配对情况,就算是一对符合条件的记(算是就近原则吧,先关注和前面记录的匹配情况)。 B300 2010-6-2 8:37:02 A
B300 2010-6-2 8:37:06 A
B300 2010-6-2 8:37:56 B
B300 2010-6-2 8:38:07 A
SELECT
a.id
FROM
(
SELECT
id,
MAX(SUBSTR(TIME,1,10)) ma
FROM
table_name
WHERE
addree = 'B'
GROUP BY
id
) AS b,
(
SELECT
id,
MIN(SUBSTR(TIME,1,10)) mi
FROM
table_name
WHERE
addree='A'
GROUP BY
id
)AS a
WHERE
a.id=b.id
AND b.ma>=a.mi;
9、10楼正解!