finding the gaps in between the sequences [message #627487] |
Wed, 12 November 2014 05:59 |
|
SW26477
Messages: 9 Registered: November 2014 Location: Location
|
Junior Member |
|
|
Hello All,
I have a query with finding the gap in between the sequences.
Below is the structure of my table
user_number | seq_number
1 | 101
1 | 102
1 | 109
1 | 110
1 | 111
2 | 201
2 | 202
2 | 203
2 | 207
2 | 208
. | .
. | .
. | .
. | .
Now I have to find the gap in between the sequences, what I mean by gap is for user_number '1' there is gap of 6 between seq_number '102' and '109'.
Below is the query which I am trying to write, but not giving the exact answer, it always gives the gap of 2:
select gap_start - gap_end gap
from(
select a.seq_number+1 as gap_start, b.seq_number-1 as gap_end
from my_table a, my_table b
where a.seq_number = b.seq_number
and a.seq_number - (select c.seq_number
from my_table c
where c.seq_number = a.seq_number-1) is null
and a.user_number = '1'
order by a.seq_number
);
Thanks.
Edited by Lalit : Added code tags
[Updated on: Wed, 12 November 2014 06:18] by Moderator Report message to a moderator
|
|
|
|
|
Re: finding the gaps in between the sequences [message #627492 is a reply to message #627490] |
Wed, 12 November 2014 06:24 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Does this help,
SQL> WITH DATA AS(
2 SELECT 1 user_number, 101 seq_number FROM dual UNION ALL
3 SELECT 1, 102 FROM dual UNION ALL
4 SELECT 1, 109 FROM dual UNION ALL
5 SELECT 1, 110 FROM dual UNION ALL
6 SELECT 1, 111 FROM dual UNION ALL
7 SELECT 2, 201 FROM dual UNION ALL
8 SELECT 2, 202 from dual union all
9 SELECT 2, 203 FROM dual UNION ALL
10 SELECT 2, 207 FROM dual UNION ALL
11 SELECT 2, 208 FROM dual)
12 SELECT user_number,
13 seq_number,
14 seq_number - rn diff
15 FROM
16 (SELECT user_number,
17 seq_number,
18 lag(seq_number) OVER(partition by user_number ORDER BY user_number, seq_number) rn
19 FROM DATA
20 )
21 WHERE seq_number - rn > 1
22 /
USER_NUMBER SEQ_NUMBER DIFF
----------- ---------- ----------
1 109 7
2 207 4
SQL>
Edit : Added partition by
[Updated on: Wed, 12 November 2014 06:26] Report message to a moderator
|
|
|
|
|
Re: finding the gaps in between the sequences [message #627524 is a reply to message #627500] |
Wed, 12 November 2014 10:21 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
@OP, I hope my solution is what you need. In addition, this is going to be a performance issue:
Quote:
and a.user_number = '1'
User_number is a NUMBER and not a string. I assume it to be a primary key being populated using a sequence. So, using single quotes around it will force Oracle to consider it a string, thus an implicit conversion would happen to convert it to a NUMBER.
Edit : typo
[Updated on: Wed, 12 November 2014 10:23] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: finding the gaps in between the sequences [message #627604 is a reply to message #627581] |
Thu, 13 November 2014 00:51 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Quote:Anyways so can we convert below code according to 8i?
No, if you have LAG in something that is analyzed by PL/SQL then you can't.
But is it smart to try to workaround good code to make it work in 8i and deliver bad code?
|
|
|