How to get an average displacement of the last 5 lines that were NOT NULL?

I have a question involving a moving average, and it works fine using:

AVG (col_x) more (partition col_a, col_b, col_c lines between 5 preceding and 1 preceding arrested)

However, I want to change this option to give the average of the last 5 lines which were non-null.  Does anyone know how to do?

For example:

create table sample_table)

col_a varchar2 (10),

date of col_b,

col_c number (2),

col_x number (5.2)

);

insert into sample_table values ('X', trunc (sysdate), 1, 1);

insert into sample_table values ('X', trunc (sysdate), 2, 2);

insert into sample_table values ('X', trunc (sysdate), 3, 3);

insert into sample_table values ('X', trunc (sysdate), 4, 4);

insert into sample_table values ('X', trunc (sysdate), 5, 5);

insert into sample_table values ('X', trunc (sysdate), 6, 6);

insert into sample_table values ('X', trunc (sysdate), 7, 7);

insert into sample_table values ('X', trunc (sysdate), 8, null);

insert into sample_table values ('X', trunc (sysdate), 9, null);

insert into sample_table values ('X', trunc (sysdate), 10, null);

insert into sample_table values ('X', trunc (sysdate), 11, null);

insert into sample_table values ('X', trunc (sysdate), 12, null);

insert into sample_table values ('X', trunc (sysdate), 13, null);

insert into sample_table values ('X', trunc (sysdate), 14, null);

insert into sample_table values ('X', trunc (sysdate), 15, null);

Select t.*,

AVG (col_x) on myavg (partition col_a, col_b, col_c lines between 5 preceding and 1 preceding arrested)

of sample_table t

order by 1,2,3;

COL_A, COL_B, COL_C MYAVG COL_X

---------- ----------- ----- ------- ----------

X 15/01/2015 1 1.00

X 15/01/2015 2 2.00 1

X 15/01/2015 3 3.00 1.5

X 15/01/2015 4 4,00 2

X 15/01/2015 5 5.00 2.5

X 15/01/2015 6 6.00 3

X 15/01/2015 7 7.00 4

X 15/01/2015 8 5

X 15/01/2015 9 5.5

15/01/2015 X 10 6

X 15/01/2015 11 6.5

X 15/01/2015 12 7

15/01/2015 X 13

15/01/2015 X 14

X 15/01/2015 15

15 selected lines

What I really want is:

COL_A, COL_B, COL_C MYAVG COL_X

---------- ----------- ----- ------- ----------

X 15/01/2015 1 1.00

X 15/01/2015 2 2.00 1

X 15/01/2015 3 3.00 1.5

X 15/01/2015 4 4,00 2

X 15/01/2015 5 5.00 2.5

X 15/01/2015 6 6.00 3

X 15/01/2015 7 7.00 4

X 15/01/2015 8 4

X 15/01/2015 9 4

X 15/01/2015 10 4

X 15/01/2015 11 4

X 15/01/2015 12 4

X 15/01/2015 13 4

X 15/01/2015 14 4

X 15/01/2015 15 4

15 selected lines


concerning

Neil

Hello

Here's one way:

WITH got_avg AS

(

SELECT T.*

AVG (col_x) OVER (PARTITION BY col_a, NVL2 (col_x, 1, 0)

ORDER BY col_b, col_c

LINES BETWEEN 5 PRECEDING

AND 1 PRECEDING

) AS myavg

OF sample_table t

)

SELECT col_a, col_b, col_c, col_x

LAST_VALUE (myavg IGNORE NULLS) over (PARTITION BY col_a

ORDER BY col_b, col_c

) AS myavg

OF got_avg

ORDER BY col_a, col_b, col_c

;

Too bad, that we cannot use the IGNORE NULLS with AVG function.

Tags: Database

Similar Questions

  • Question 1: Low virtual memory does that mean and how do I fix? Question 2: The corrupt system file that should not be removed. How do I correct?

    Question 1: what low virtual memory mean and how do I rectify.   I am invited this could change my access to some applications and the pc is very slow.

    Question 2: AVG pushes me that I have a corrupted system file, in a white space, I think, that should not be removed.  How do I rectify this?

    Uninstall AVG with tool for them to do it in safe mode.

    Reboot and install MSE.

    Without doubt, all problems solved. Let me know how it goes.

  • Does anyone know how to get updates of Photoshop CC 2015 for a machine that does not have Internet access?

    Hello.  As the title says I have several machines that do not have an Internet connection that I need to update Photoshop CC 2015.  From what I've read so far, the only way to update is through the app.  Is there another way to download updates on a different machine and then run them on computers running the PS?

    : http://prodesigntools.com/adobe-cc-2015-updates-links-windows.html

  • the neutrophilia help lines that do not have a match.

    I'm having a hell of a time trying to write a select statement that would choose to specific lines without match. hurt after a sample of my table and data below.

    creating the table

    CREATE TABLE "IDEA"
      (
        "CODE"        VARCHAR2(32 BYTE),
        "ACCOUNTCODE" VARCHAR2(32 BYTE),
        "DRCR"        VARCHAR2(20 BYTE),
        "ENTRYID"     VARCHAR2(32 BYTE),
        "JOURNALAMT"  NUMBER(17,2)
      )
    
    

    data in the table.

    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172863', '6.27')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172863', '6.28')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '1115', 'CR', '172863', '-12.55')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172899', '0.25')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172899', '0.25')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172899', '197.25')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172899', '197.25')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '1115', 'CR', '172899', '-394.50')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172903', '975')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '7038', 'DR', '172903', '975')
    INSERT INTO "IDEA" (CODE, ACCOUNTCODE, DRCR, ENTRYID, JOURNALAMT) VALUES ('Company', '1115', 'CR', '172903', '-1950')
    
    

    When I run the following query I have that would get the following result

    select
        --DRCR,
        ENTRYID,
        sum(JOURNALAMT)
       
    From idea
    group by entryid
    
    

    my top of the query output.

    "ENTRYID"                     "SUM(JOURNALAMT)"             
    "172863"                      "0"                           
    "172899"                      "0.5"                         
    "172903"                      "0"                          
    

    I want to identify the lines that make up the 50.

    Please note that there might be 3 or 4 lines per credit. so it could be a football match of the 4DR same 5DR or 1 CR 1 CR however will always be 1 CR.

    I thought to write a plsql program that would try to match every single possible combination for 1 credit, but it seems to me that there is an easier way.

    any help would be grateful.

    Hello

    Here is a function that attempts to find a combination of 'DR' ranks with the entry ID given, whose journalamts correspond exactly to the amount given.  If If is such a combination, it returns a string that contains a list delimited by amounts.  You coould easily change this to return a list delimited by primary keys, or whatever you want:

    FUNCTION to CREATE or REPLACE dr_list
    (entryid_wanted IN idea.entryid%TYPE
    journalamt_wanted in NUMBERS
    prev_rowid IN DEFAULT NULL ROWID
    )
    RETURN VARCHAR2
    IS
    "failure_message VARCHAR2 (50): = '(aucune correspondance trouvée);
    remainder_list VARCHAR2 (4000);
    return_list VARCHAR2 (4000);
    BEGIN
    IF journalamt_wanted > 0
    THEN
    return_list: = failure_message;   -Are likely to be replaced later

    -Find each line that may be part of a solution
    FOR (IN) r
    SELECT journalamt
    R_id AS ROWID
    The IDEA
    WHERE Drcr = "DR".
    AND entryid = entryid_wanted
    AND journalamt > 0 - if necessary
    LNNVL (ROWID, AND<=>
    ORDER BY ROWID
    )
    LOOP
    IF r.journalamt = journalamt_wanted
    SO - This line is a solution alone
    return_list: = r.journalamt;
    EXIT;
    On the OTHER - see if this line belongs to a bigger solution
    remainder_list: = dr_list (entryid_wanted
    , journalamt_wanted - r.journalamt
    r.r_id
    );
    IF remainder_list <> failure_message
    THEN
    return_list: = r.journalamt | ', ' || remainder_list;
    EXIT;
    END IF;
    END IF;
    END LOOP;
    END IF;

    RETURN return_list;
    END dr_list;
    /

    You can use the function in a query like this:

    SELECT EntryID

    SUM (journalamt) AS total_amt

    , dr_list (entryid, SUM (journalamt)) AS extra_drs

    The IDEA

    GROUP BY entryid

    ;

    and it could produce an output like this:

    ENTRYID TOTAL_AMT EXTRA_DRS
    ---------- ---------- ------------------------------
    172903 0
    172863 0
    172899.5.25,.25
    8 987 (no match)
    901 5 2, 2, 1

    Note that when you call the function, pass you the arguemnts only 2.  The 3rd argument is used only by the function when it calls itself.

  • Get the ROWID of lines that are not not duplicate in Oracle 10 g

    Hello

    given a table TAB1 in which the primary key is composed of the first 3 columns:

    ================================================
    colA___colB___colC___colD
    ================================================
    AA-----------------------01-----------------------20080101-----------------------100
    BX-----------------------32-----------------------20050325-----------------------366
    AA-----------------------01-----------------------20080102-----------------------645
    AA-----------------------01-----------------------20080707-----------------------765
    AB-----------------------02-----------------------20080806-----------------------336
    AB-----------------------02-----------------------20080705-----------------------543

    I want to be able to find the ROWID of these lines where colA | colB occurs only once in the table TAB1.

    So in the example above, I would like to know the ID of the containing colA = BX line and colB = 32 as "BX32" appears only once in this table (AA01 happens 3 times, AB02 occurs twice, so I'm not interested in them).

    The following does not work, but it's too SLOW:

    Select the TAB1 ROWID where colA | colB = (select colA | colB of TAB1 HAVING COUNT (*) = 1).

    Can someone please suggest an effective way to find this unique ROWID value in these circumstances?

    Published by: Nemesis on November 29, 2008 02:50

    Hi Justin:

    Thank you for your support, I didn't know that the separate symbol appears. Sorry for all :)

  • Collections by ignoring the lines that were not updated

    Hi all
    I have a report based on a collection. In the present report is a lov in which a user can assign a value to the recording. However, the user doesn't have to update all records before saving. Based on the examples of the collection I've seen, it seems to be an assumption that all records will be updated. I'm having trouble trying to update only the lines in the collection that the user has entered a value for.

    Any ideas would be greatly appreciated.

    Thank you

    Don't know what you have in mind. If you are thinking to add another value of the column to the collection and handling, it should work better than the box. You will have at least a NULL value or a value of text associated with each line in your collection.

  • How to get back my data for the health and the watch Apps once I've restored my phone?

    How to get back my data for the health and the watch Apps once I've restored my phone?

    From the backup, you're going to be restoration.

    If you back up to iTunes, make sure that it is an encrypted backup.

  • How to get TS::SequenceContextPtr to customize the user interface

    I want the file of the custom, user interface sequence value therefore need to take stock of the TS:equenceContextPtr, anyone know how to get this point.

    Is there a reason that UIMessages are not enough?

    Just off the top of my head, here are a few concerns with the not not using UIMessages:

    1. How the user interface is unknown when the file of the sequence is developed to set the value.  Without an event or some sort of synchronization you can do exactly that.  UIMessages not only to ensure the right information, but they help also with synchronization.

    2. The engine does not know this information, and view of execution manager knows that the information for the current execution it is linked to (which changes throughout the life of a series of tests).  Basically, recall of front end, model, and other factors may spawn short executions briefly related to the Manager view of execution.  So if you take the context of the sequence of the current run is perhaps not the right one.

    3. Writing a user interface will be couple the user interface to your specific sequence file.  At this point, you can use the network variables or global variables or global queues as well.  It might even be better options.

    The situation you describe is exactly why the UIMessages were created.  Maybe you can help me understand why you are set on the use of pointers, you already have?  There might be a better solution for your case if I'm misunderstanding you.

    Thank you

  • Try to play a movie but I have WMP cannot play the file how to get to play or delete the Xvid codec to play?

    How to get to play or delete the Xvid codec to play? Movies in AVI format, I converted it to WMP, but he won't play again

    No guarantee but maybe you need the XviD Codec.

    (FWIW... it's always a good idea to create a system)
    Restore point before installing software or updates)

    XviD Codec
    http://www.xvidmovies.com/codec/

  • I did several back ups on my computer and I wonder how many of the discs that I have to hold. Can I get rid of all except the last backup?

    I did several back ups on my computer and I wonder how many of the discs that I have to hold. Can I get rid of all except the last backup?

    Thank you

    Scott

    Hi Scott,.

    You must keep all 10 backup.the disk new backups will contain only the last backups and will not have the previous backups.

  • How to get distinct records by using the ListAgg OBIEE report function?

    Hi all

    I get a correct result as mentioned below. But I don't see duplicates in my result here, in my example, I get duplicate for the name of the employee 'Pat '. So how to get Distinct values by using the LISTAGG function?

    Data set of sample with the Department and its employees

    Service employee

    ----------      ----------

    Marketing Michael

    Pat of marketing

    Pat of marketing

    Pat of marketing

    Purchase of Den

    Purchase of Alexander

    Purchase of Shelli

    Purchase of Sigal

    Guy of purchase

    Purchase of Karen

    Using the ListAgg function, we can convert it to:

    Employees of the Department

    -----------     -------------------------------------

    Marketing of Pat, Pat, Pat, Michael

    Purchase of Sigal, Shelli, Karen, Guy, Den, Alexander

    I tried a lot of things, but I'm not able to understand how exactly this can be achieved, if anyone has any idea or suggestions please do share, thanks in advance.

    After much research, I found the solution & I want to share what he finds very useful, we can create SQL logic in the Advanced tab, as below and after you click Rescan, and then you will get your desired results.

    SELECT saw_0, Evaluate_Aggr T1.dept ("ListAgg(%1,'' & '') intra group (about 1%)") ("as long as VarChar (1000), T1.emp) saw_1 FROM)

    SELECT 'emp_dept '. "the Department dept,

    'emp_dept '. "' employee ' emp

    IN THE "DOMAIN".

    GROUP BY dept, emp

    ) T1 GROUP FROM T1.dept ORDER BY saw_0

    Also note here that we have good anti-aliasing for columns parent (ex: saw_0, saw_1), another by mistake oracle bi server.

  • How to get Camera Raw to recognize the new Nikon D500 camera raw files?

    How to get Camera Raw to recognize the new Nikon D500 camera raw files?

    Devices supported by Adobe Camera Raw

    The D500 has been supported since camera 9.5 Raw which is only compatible with versions of Photoshop CS6 and Cloud.

    What version of Photoshop are you running?

  • How to get audio to play on the timeline by rubbing through

    How to get audio to play on the timeline during the treatment, the clip is not cut, clip does not display an audio wave, looked through the help did not answer. When the clip is imported without options rise so uncertain where the issue is, any help is greatly appreciated, ty

    There is an option in the Preferences / Audio: audio playback while rubbing.

  • Have on current PC, Win 7 64 bit CS5; How to get *.exe to transfer to the new computer after removal of old

    Have on current PC, Win 7 64 bit CS5; How to get *.exe to transfer to the new computer after removal of old?

    https://helpx.Adobe.com/Creative-Suite/KB/CS5-product-downloads.html

  • How can I get my document to save the last used rotated view?

    How can I get my document to save the last used rotated view?

    Possible with Adobe Acrobat, Adobe Reader No.

Maybe you are looking for

  • Mac book pro 2016 13 inch crash regularly

    Hello I received the new mac book pro 2016 13 inches (forcefully toutch trackpad) and 16 GB of RAM. Sinus a few days my mac often crash and asks me to press a key to reboot (kernel panic). I can't work because of these accidents, could someone help m

  • Paragraph in the Pages style?

    I'm trying to set up a paragraph style in a document with Pages where the first line is not indented, but all subsequent lines are indented? I searched using Pages and I cannot find this information. Does anyone know how to do this? Thanks for the he

  • Upgrade Satellite A100-200 RAM question

    Hello I have a Toshiba Satellite A100-200, I want to move to 4 GB.I install 2 x 2 GB kingston kvr667d2s5/2 GB pc-5300 ddr2 667 mhz. Every time I make a control sites Internet it tells me to install PC24200 ddr2 533.The above will work even as the 420

  • Help me get my laptop drivers

    I can't find my phone on the site of Hp, so I can't download the drivers it is my laptop: Name of product: notebook PC HP Pavilion g6 Product number: D8P33EA #ABV Serial number: 5CD31502KQ NAME: HP PAVILION G6 2315-SE. Please help me find my drivers

  • Need help with email accounts

    I'll put up my email accounts and I have both a gmail and AOL e-mail. When I click on the AOL tab and enter my email address and the password of the phone says I have this account. It is not on the phone well. Is there anyway to reset the page count