Using analytical to find contiguous runs values

Hi all

I have a table which may appear like this:

create table TST as select 'key1' k, 1 double t;
Insert in TCT values ('key1', 2);
insert into values of tst ("key1", 3);
Insert in TCT values ('key2', 4);
Insert in TCT values ('key2', 5);
Insert in TCT values ('key1', 6);
Insert in TCT values ('key3', 7);
insert into values of tst ("key3", 8);
insert into values of tst ("key3", 9);
Insert in TCT values ('key2', 10);
insert into values of tst ("key1", 11);


I want to order by t
I want to have a third column which increments each time k Exchange when orderd by t:

Key1, 1, 1
Key1, 2, 1
Key1, 3, 1
Key2, 4, 2
Key2, 5, 2
Key1, 6: 3
Key3, 7, 4
Key3, 8, 4
Key3, 9: 4
Key2, 10, 5
Key1, 11, 6


for example I want to identify the extent of k, when ordered by t

Note that l does not have to be progressive by one every time:


Key3, 8, 4
Key3, 9: 4
Key2, 10, 5
Key1, 11, 6
Key1, 13: 6
Key1, 17, 6
Key2, 19: 7

I had hoped that with the help of an analytic function might be self-referential, but it seems logical that it cannot:
SELECT
  k, t, 
  --when the k of this row = k of prev row, use prev row's contig_id else add one, and make it this row;s contig_id
  case when k = lag(k) over(order by t) then lag(contig_id) over(order by t) else lag(contig_id) over(order by t) + 1 end as contig_id
I also tried to create a sequence:
create sequence dseq;
SELECT
  k, t, 
  --when the k of this row = k of prev row, use seq currval else use seq nextval
  case when k = lag(k) over(order by t) then dseq.currval else dseq.nextval end as contig_id
While this code does not work, I hope, it helps to illustrate my goal


Also, I do not fear what the specifier of contiguous block. I've used here a number that increments, but I wouldn't be all that uniquely identifies a contiguous block of k. Finally, I want to produce a report as well:


from_t, to_t, k
1, 3, key1
4, 5, key2
6, 6, key1
7, 9, key3
10: 10, key2
11: 17, key1





So if you can jump directly to that, that would be great. I had considered this sql to do this:
SELECT
  min(t) as from_t,
  max(t as to_t,
  max(k) as k,
FROM
  <subquery>
GROUP BY
  contiguous_identifier
I'm wrong just get a unique identifier for each contiguous block of k

Hello

SQL> select * from tst;

K             T
---- ----------
key1          1
key1          2
key1          3
key2          4
key2          5
key1          6
key3          7
key3          8
key3          9
key2         10
key1         11
key1         15
key1         17
key3         19

14 rows selected.

SQL> with RNs as (
  2    select tst.*,
  3      row_number() over(order by t) as rn_t,
  4      row_number() over (order by k, t) as rn_k_t
  5    from tst
  6  )
  7  select min(t) as from_t,
  8         max(t) as to_t,
  9         k
 10  from RNs
 11  group by k, rn_t - rn_k_t
 12  order by from_t
 13  ;

    FROM_T       TO_T K
---------- ---------- ----
         1          3 key1
         4          5 key2
         6          6 key1
         7          9 key3
        10         10 key2
        11         17 key1
        19         19 key3

7 rows selected.

Kind regards
Dima

Tags: Database

Similar Questions

  • How to find the ASCII value of the TAB?

    Hi friends,

    I'm trying to find the ASCII value of the TAB character using ASCII standard as:

    SELECT ASCII ('< TAB >') FROM DUAL;

    I'm running the command above in SQL * more.

    Is not treated as TAB.
    It is considered space and give the ASCII value of the space - 32.

    Please let me know how?

    Version of Database - 9i

    Concerning
    Anthony Alix.

    Its only editor that gives you the problem; Try it on TOAD or sql developer, it works perfectly.

    Is there a particular reason you want in sql * more?

  • Find the average value of a buffer

    Hello

    In my VI Panel, I have a digital control, a button and a digital display.

    When the user, enter a number in numerical order and press the button, the digital display should show the average value of all the past 5 values entered in the digital control, for example:

    1st entry: 10-online average = 10 (as sum = 10)

    2nd type: 0-online average = 5 (as sum = 10 + 0 = 10)

    3rd enter: 5-online average = 5 (you get the idea...)

    4 enter: 1-average online = 4

    5 enter: 14-online medium = 6

    6 enter: 2-online average = 4.4 (sum = 0 + 5 + 1 + 14 + 2 = 22, because the 1st value is thrown)

    How to write this VI?

    Thank you!!

    You can also use arrays to hold the data in a registry change and find the average value of the last five items.

    Good luck

  • Find the minimum value of the table in time...

    Firstly that it is not "find the min value in the table.

    It is:

    I have a table that changes its values at each iteration, and I want to compare the new values with previous values and make a new array with the minimum values...

    In C++, it would be like this:

    int main() {}

    int array [8];

    int min [8];

    int i;

    for (i = 0; i<>

    min [i] = 1000000; Initialize minimum table with a high value

    }

    {while (1)}

    Array = update_array ();

    for (i = 0; i<>

    If (array [i]<>

    min [i] = array [i];

    }

    }

    do things...

    }

    return 0;

    }

    IM new to labview and it always confuses me a lot...

    Thanks in advance.

    Using the function Max & Min regular (not table Max & min as you know) with tables and a good will of employment registry change.  Max Min &, as most of the digital functions, is polymorphic and works witt tables as well as scalars.

    Lynn

  • Find facilities running

    I'm trying to update AVG 2014 on Windows 8.1. A message appears and says that there is a current and ocmplete installation installation before continuing with the upgrade. But I can't find the installation running. Where can I find facilities running?

    You can download a utility from Microsoft Sysinternals suite called process explore. You can kill (stop) the AVG installer with it.

    Alternatively, you can do so by holding down Ctrl + Alt keys and tapping on the del key to call up the Task Manager. Use the Task Manager to stop the installation. It's a bit safer to use process Explorer but much less interesting.  It is if you're a bit of a geek.

  • PL/SQL loop to find the minimum value

    I want to find the minimum value from a table using the pl/sql loop

    I have data format below.

    1 ABC 2

    1 ABC 20

    1 ABC 500

    1 ABC 2000

    XYZ 2 250

    JKL 3 300

    MNP 3 350

    3 KLM 400

    I wrote the code below. His does not work. Please suggest.

    Set serveroutput on
    declare
    x number: = 3;
    y VARCHAR2 (3): = 'ABC ';
    fmin number: = 0;
    number of Fmax: = 0;
    Start
    for rec in (select sal from test
    where id = name and x = y) loop


    If rec.sal > fmin then
    fmin: = rec.sal;
    on the other
    FMAX: = rec.sal;
    fmin: = fmax.
    end if;
    end loop;
    dbms_output.put_line (Fmax);
    end;
    /

    I want to output 2 on top of code.

    Thanks in advance

    Why PL/SQL? SELECT MIN (SAL) OF TEST will be more rapid and much less coding. In any case:

    declare

    number of v_min_sal;

    Start

    for v_rec in a loop (select sal from emp)

    If nvl (v_min_sal, v_rec.sal + 1) > v_rec.sal

    then

    v_min_sal: = v_rec.sal;

    end if;

    end loop;

    dbms_output.put_line (' lowest salaray is "' |") v_min_sal | '"');

    end;

    /

    The lowest Salaray is "800".

    PL/SQL procedure successfully completed.

    SQL >

    SY.

  • Find the max value

    Hi team,

    I have the following query where I need to find the max value

    [code]

    with t as)

    Select 'L1' R_nm, R_Data 'Data', 'Obj' R_Obj, r_prec 'Wd', '2' double val

    Union of all the

    Select 'L1', 'Data', 'Obj', 'No', '4' double

    Union of all the

    Select 'L2', 'Data', 'Obj', 'No', '4' double)

    Select t.*, max (val) on rk (partition r_nm, r_data, r_obj) of t

    [/ code]

    My results should be

    [code]

    r_nm r_data r_obj r_prec rk val

    L1 Data Obj 1 2 Wd

    L1 data Obj 4 No.2

    L2 Data Yes 1 1 Obj

    [/ code]

    Thank you


    Hello

    It seems that you are not interested in the MAX at all.  Looks like you want to classify the lines, such as one that has the lowest value is assigned the number 1, the 2nd line with the lowest values gets 2,... and line with him gets N-th lowest value N.  If is not serious if the MAX is 4, 2 or 420.

    Here's a way to do it:

    SELECT T.*

    , RANK () OVER (PARTITION BY r_nm, r_obj)

    ORDER BY val

    ) AS rk

    T

    ORDER BY r_nm, r_obj

    val

    ;

    Depending on how you want to deal with links, you can use ROW_NUMBER or DENSE_RANK instead of RANK.

  • Rownum find less &amp; max values

    Hi all

    I have a doubt as to ROWNUM.
    Can I use rownum to find the n less values & n max values?
    CREATE TABLE t1(id NUMBER(10))
    SELECT ID FROM (SELECT id FROM t1 ORDER BY id ) WHERE rownum < 20.
    This will give me values less 19 first?

    THX
    Rod.

    Yes, this query returns the ID values of T1 small 19.

    Justin

  • How to use the checkbox to display the values of booolean?

    Hi all

    I'm learning to use APEX. I built an editable report after the 'top advanced tutorials', and I use static LOVs to insert Boolean values in two columns.
    The underlying table has two columns of type VARCHAR2 (1) NOT NULL with a a check constraint, so it must contain either 'Y' or 'n'), and the static LOV requires users to only choose beetween 'Yes' and «»

    I would use checkboxes to insert this field. Is this possible? Where can I find examples / tutorials about it?

    Thank you
    Andrea

    See this:

    http://www.inside-Oracle-apex.com/checkboxes-in-tabular-forms-the-easy-way/

    Denes Kubicek
    -------------------------------------------------------------------
    http://deneskubicek.blogspot.com/
    http://www.Opal-consulting.de/training
    http://Apex.Oracle.com/pls/OTN/f?p=31517:1
    -------------------------------------------------------------------

  • I am interested in the Apple Watch for fitness, but want to be able to use the phone when I run without carry my iPhone is possible

    I am interested in the Apple Watch for fitness, but to use the phone when I run it without having to carry my iPhone 6 s, is it possible?

    You can, but the Fitness app works better if you have the iPhone on your person.

    Use the activity on your Apple Watch - Apple Support

  • I always use Firefox, but when I run CCleaner, Internet Explorer files appear... How? Why?

    I never use Internet Explorer, always use Firefox, but when I run CCleaner, some files appear in Internet Explorer. Can't understand why?

    Note If you are using MS plugins like the Windows Media Player and Silverlight these plugins to store their data in the storage of the cache and cookies IE location.

  • Find most common value in a table of enums

    Hello

    I'm looking for a stylish way to find most common value in a table of enums

    My being of enum:

    0 - close

    1 - open

    2 - undefined

    For example, if my table contains:

    Close, close, open, close, close, open.

    The most common value would be 'Closed'

    I created a very personalized VI which allows me to get the desired result, but I'm really not proud of doing it this way, just simply because I need to change if I add a new value to my enum...

    If anyone can share some ideas to enlighten me, I would REALLY appreciate it.

    Thanks in advance!

    I don't know, I'd go the yard road, to propose, he could save a single iteration since you know 0 is the first point of departure, but you then must deal with the determination of lengths and dealing with missing values that return-1, etc..

    If you really expect of very large tables, I would have moved my previous version which is supposed to be quick to write in another simple method of counting.

  • Using excel, if I remove a value in a cell and you decide then he should not have been deleted, how I get it back?

    Using excel, if I remove a value in a cell and you decide then he should not have been deleted, how I get it back?

    Once you have removed the cell values and you have not saved the document again that you can always undo changes.

    Keyboard shortcut for undo is Ctrl + Z.

    MD MOEEN AJAZ KHAN - MCP, N +, A +.

  • When you open Illustrator I get the message: problem loading 'photoshopimport.aip"(connect). I can't access my fonts. I use an iMac 2012 27 "running on 10.10.2 Yosemite

    When you open Illustrator I get the message: problem loading 'photoshopimport.aip"(connect). I can't access my fonts. I use an iMac 2012 27 "running on 10.10.2 Yosemit

    Lucas,

    According to me, which should solve it:

    Open system preferences and go to accessibility. Then select display in the source on the left side list, then check the box transparency reduced.

    It was introduced in this #15 post by TheUlser:

    https://forums.Adobe.com/message/6854594#6854594

    It has been described on this page from Adobe:

    http://capitalmacservice.com/font-preview-working-Illustrator-CS-5-Yosemite-fix/

    It may also depend on the monitor, as shown in the message #29 by Loncar in the same thread.

  • How to use sqlplus to connect and run control?

    Hello, I have a little problem please help me
    I want to use sqlplus to connect and run the command
    for example: sqlplus test/test@DB select sysdate double;
    and then it will show on the screen (sysdate) data
    What should I do?

    Thank you

    Wrong tool. You should start with something like this:
    #! usr w
    use strict;
    Use DBI;

    SQL * more isn't a script tool.

Maybe you are looking for