|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Trouble with writing a query
Ok, here's a doozie that has me completely stumped. I've tried everything I can think of. Here is my data:
Code:
CHECK_POINT PROCESS
1 A
2 A
3 B
4 B
5 B
6 B
6 C
7 B
10 A
11 A
15 B
16 B
16 A
16 C
17 B
17 A
18 B
18 A
19 B
21 C
I need to group together all rows that which are the same process over consecutive checkpoints. Desired result set: Code:
PROCESS START STOP A 1 2 A 10 11 A 16 18 B 3 7 B 15 19 C 6 6 C 16 16 C 21 21 |
|
#2
|
|||
|
|||
|
Try this
select cp2.checkvalue, cp1.checkid, cp2.checkid from checkpoint cp1 inner join checkpoint cp2 on cp1.checkvalue = cp2.checkvalue where cp1.checkid<> cp2.checkid group by cp1.checkvalue ... You will have to adjust the inner joins for Oracle's non-ANSII style.
__________________
El éxito consiste en una serie de pequeñas victorias día a día MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html |
|
#3
|
|||
|
|||
|
This seems to work.
Check out AskTom.oracle.com for more examples of analytics create table x (check_point number, process varchar2(1)); insert into x values ( 1,'A'); insert into x values ( 2,'A'); insert into x values (10,'A'); insert into x values (11,'A'); insert into x values (16,'A'); insert into x values (17,'A'); insert into x values (18,'A'); insert into x values ( 3,'B'); insert into x values ( 4,'B'); insert into x values ( 5,'B'); insert into x values ( 6,'B'); insert into x values ( 7,'B'); insert into x values (15,'B'); insert into x values (16,'B'); insert into x values (17,'B'); insert into x values (18,'B'); insert into x values (19,'B'); insert into x values ( 6,'C'); insert into x values (16,'C'); insert into x values (21,'C'); select process, start_cp, check_point from (select process, case when lag_cp is null and lead_cp is null then check_point else lag(check_point) over ( partition by process order by check_point) end start_cp, check_point, lag_cp, lead_cp from (select process, check_point, decode( lag(check_point) over (partition by process order by check_point), check_point-1, lag(check_point) over ( partition by process order by check_point)) lag_cp, decode( lead(check_point) over (partition by process order by check_point), check_point+1, lead(check_point) over ( partition by process order by check_point)) lead_cp from x) where lag_cp is null or lead_cp is null) where start_cp is not null and (lag_cp is not null or start_cp = check_point) |
|
#4
|
|||
|
|||
|
Holy cow, that is slick. Just when I think I'm getting the hang of analytics, I see how much I've still got to learn.
Thanks! |
![]() |
| Viewing: Dev Shed Forums > Databases > Oracle Development > Trouble with writing a query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|