find the cumulative sum of the previous columns

Hello

How can I get the cumulative sum of a particular column in the preceding lines in the current line.
e.g. I have a table produced,

Date | Orders
__________________
1 JANUARY 10 | 3
JANUARY 2, 10. 5
JANUARY 3, 10. 1

I want to select the date and orders between the 1st and the 3rd. Something like...
Select the Date, the orders for products where Date between to_date('1-JAN-10') and to_date('3-JAN-10')

But I want the output like this...

Date | Orders
__________________
1 JANUARY 10 | 0
JANUARY 2, 10. 3
JANUARY 3, 10. 8

Similarly, when I select it between 2 and 3, I want the output voltage...

Date | Orders
_________________
JANUARY 2, 10. 3
JANUARY 3, 10. 8

and so on. In the output, orders here are the cumulative sum of the previous columns.

Published by: eric clapton, November 8, 2010 20:45

Hello

Use the analytical SUM function.
You want to filter by date after the cumulative SUM is calculated, so calculate the SUM in a subquery, then filter by date no later than:

WITH     got_cm_orders     AS
(
     SELECT     dt
     ,     orders
     ,     SUM (orders) OVER (ORDER BY dt) - orders
               AS cm_orders
     FROM     products
)
SELECT     dt
,     orders
WHERE     dt     BETWEEN     DATE '2010-01-02'
          AND     DATE '2010-01-03'
;

Tags: Database

Similar Questions

  • bougth a second hand iPhone 6 how find the previous owner using the serial number?

    I bought a second hand iPhone 6 and he holds a lock to iCloud how to find the previous of this iPhone owner so that I can email him?

    I don't have a serial number of the phone how to use it so that I can follow the former owner?

    Unfortunately, you can not. Take it to where you bought it for a refund.

    See you soon

    Pete

  • Cannot find the left column that used to be on the documents and photo files.

    Cannot find the left column that used to be on the documents and photo files. On this missing column I know was there before, had options fax, copy and other things we could do with these files. I want to get that back. Thank you.

    With my Documents folder open, go to Toolsand then click on Options files . On the general tab, tasks section, choose Show tasks in folders.

    I would like to know if it works. Thank you.

  • Is there a way to get the first Pro CS6 with still now I can't find the previous, offered versions with the subscription of the CC 2015?

    Is there a way to get the first Pro CS6 with still now I can't find the previous, offered versions with the subscription of the CC 2015? What is recommended for creating DVDs with interactive menus?

    Instructions again, cloud or version https://forums.adobe.com/thread/1992717 serial number
    -a of notes on different versions of cloud and step by step on these differences
    -contains information about the separate library download which is necessary
    -CS6 is the last reminder, see here #8 why http://forums.adobe.com/thread/1337952?tstart=0

    Still and 10 Windows https://forums.adobe.com/thread/2015461 use the compatibility mode of Windows 8
    problem/solution to install Encore CS6 https://forums.adobe.com/thread/1934087

  • Query to find the previous activity in STM

    I have a data base to reflect a State of Transition (STM) Machine. It contains 3 tables: activity, node, and Transition.

    Tables of knots and Transition are the configuration of the STM. They define the different possible States and the possible transitions between States.

    The way that I take through the STM is recorded in the activity Table. A record is created for each visited node.

    I need a query that uses the id of my current activity to find my previous activity, i.e. the activity that precedes me directly. The following rules apply:
    -My Id is meaningless
    -It should work for different configurations of the transition and the node tables, but:
    -The starting node is always the node "A".
    -There is no recursive transition, for example ('B', 'B');
    -There is no circular transitions, for example ('B', 'C') and ('C', 'B')
    -Transition and node tables will never change between the creation of the first activity and the execution of the query.
    -The path reflected in the activity table is always a valid path.
    -My current activity is always the last activity.

    For all data below, find the previous activity of activity with id = 4. The correct answer is "C".
    DROP TABLE Transition;
    DROP TABLE Activity;
    DROP TABLE Node;
    
    CREATE TABLE Node
    (
         Id VARCHAR2(1) PRIMARY KEY
    );
    
    CREATE TABLE Activity
    (
         Id Number(8,0) PRIMARY KEY,
         Node_Id VARCHAR2(1)
    );
    
    CREATE TABLE Transition
    (
         FromNode_Id VARCHAR2(1),
         ToNode_Id VARCHAR2(1)
    );
    
    ALTER TABLE Activity
    ADD FOREIGN KEY
    (Node_Id) REFERENCES Node(Id);
    
    ALTER TABLE Transition
    ADD FOREIGN KEY
    (FromNode_Id) REFERENCES Node(Id);
    
    ALTER TABLE Transition
    ADD FOREIGN KEY
    (ToNode_Id) REFERENCES Node(Id);
    
    INSERT INTO Node VALUES ('A');
    INSERT INTO Node VALUES ('B');
    INSERT INTO Node VALUES ('C');
    INSERT INTO Node VALUES ('D');
    
    INSERT INTO Transition VALUES ('A','B');
    INSERT INTO Transition VALUES ('B','C');
    INSERT INTO Transition VALUES ('B','D');
    INSERT INTO Transition VALUES ('C','D');
    
    INSERT INTO Activity VALUES (1,'A');
    INSERT INTO Activity VALUES (2,'B');
    INSERT INTO Activity VALUES (3,'C');
    INSERT INTO Activity VALUES (4,'D');
    Desired output:
    ID
    -
    C

    Hello

    Assuming that all Activity_id tells us nothing on the way, but all lines of activity together to form a single path, we can get results this way:

    WITH     all_paths    AS
    (
         SELECT     'A' || SYS_CONNECT_BY_PATH (a.Node_id, '/')
                  || '/'     AS node_id_path
         FROM     Transition  t
         JOIN     Activity    a  ON  a.Node_Id  = t.ToNode_Id
         WHERE     LEVEL             = (
                                SELECT  COUNT (*)
                             FROM     activity
                             ) - 1
         START WITH  t.FromNode_Id  = 'A'
         CONNECT BY  t.FromNode_Id  = PRIOR t.ToNode_Id
    )
    SELECT       REGEXP_SUBSTR ( node_id_path
                     , '([^/]+)/' || (
                                        SELECT  Node_Id
                                 FROM    activity
                                 WHERE   Id     = 4  -- target_id
                                    )
                             || '/'
                   , 1
                   , 1
                   , NULL
                   , 1
                   )     AS prev_id
    FROM       all_paths
    ;
    

    This means, there is some character (I used "/" above) that we know never appears in Activity.Node_Id.

    Since there are no loops in Transition, it cannot be more than 1 way that involves all activity lines. If there are N lines in operation, this full path will be one that extends to LEVEL = N - 1. (It is not N, N - 1, because the join between the activity and the Transition will always leave 1 line in activity with Node_Id = 'A'). As soon as we have the full path (which is node_id_path in 1 row, produced by all_paths), we just need to guess what was the Node_Id, which corresponds to the id of the target (4 in this example) and find the previous of the delimited node_id_path node_id.

  • How to find the same column name in different tables in the same schema

    Hello

    I find the 'ename' column name in different tables in the same schema.

    Thank you
    Nr

    Hello

    Try this query

    Select column_name, table_name from user_tab_columns where column_name = 'ENAME ';

    Award points and end the debate, if your question is answered or mark if she was...

    Kind regards

    Lacouture.

  • Find the previous quarter

    Hello
    I'm looking for a function that will help me find the previous quarter, then the format base quarter is YYYY | » Q'|| Q.
    For example, when quater_base = 2011Q 1, I need the function returns quarter_previous = 2010Q 4.

    I think I need to convert YYYY | » Q'|| Q on a date and then subtract from 91 days or use add months, but I need help conversion YYYY | » Q'|| Q for a date.
    Thank you very much in advance for your help.
    TERI

    Hi, Teri,

    Welcome to the forum!

    user13426561 wrote:
    Hello
    I'm looking for a function that will help me find the previous quarter, then the format base quarter is YYYY | » Q'|| Q.
    For example, when quater_base = 2011Q 1, I need the function returns quarter_previous = 2010Q 4.

    I think I need to convert YYYY | » Q'|| Q on a date and then subtract 91 days or use add months,.

    Q1 is normally 90 days long. If you subtract 91 days March 1, you return of the last two quarters.
    Q2 is always long for 92 days. If you subtract 91 days from May 31, you have not changed shifts.
    Better use ADD_MONTHS.

    but I need help conversion YYYY | » Q'|| Q for a date.

    If s is a string of 6 characters such as '2011 Q 1' (i.e. Q1 2011), then

    ADD_MONTHS ( TRUNC ( TO_DATE ( SUBSTR (s, 1, 4)
                        , 'YYYY'
                        )
                 , 'YEAR'
                 )
            , 3 * ( TO_NUMBER (SUBSTR (s, 6, 1))
               - 1
               )
            )
    

    is the start DATE of this quarter.
    Subtract 3 months ago, and convert it to a string such as "2010 Q 4":

    TO_CHAR ( ADD_MONTHS ( TRUNC ( TO_DATE ( SUBSTR (s, 1, 4)
                               , 'YYYY'
                               )
                        , 'YEAR'
                        )
                   , 3 * ( TO_NUMBER (SUBSTR (s, 6, 1))
                      - 2     -- NOTE: subtracting 2, to get previous quarter
                      )
                     )
         , 'YYYY"Q"Q'
         )
    

    These strings come Is it possible that you could change the strings to have a month ('01', '04', '07' or 10') instead of the district, or to include both? This work would be much simpler!

    It's a shame that you can't use 'Q' in TO_DATE.
    You might consider a user-defined function to perform the conversion above. It will make your code much cleaner, but it will be slower.

    Thank you very much in advance for your help.
    TERI

    Published by: Frank Kulash, January 4, 2011 18:30
    Corrected the two expressions

  • Find all the lines from the previous column value difference lines

    I have an interesting requirement. There is a DATE column and a user in one table and I have to find all the lines for all users for which the previous and the current line has lagged in lets say more than 30 minutes. The rows are already sorted in time.

    For example in the following table, we need to ID 4 and 6 for user 1.


    Date of the user ID
    1 1 today 1 hr. 0 Min. 0 sec.
    today 2 1 1 hour 1 min 0 sec
    Today 3 1 1 hour 29 min 0 s
    * 4-1 today 1 hour 59 min 3s *.
    5 1 today 2 hours 10 min 2 sec
    * 6 1 today 2 hours 50 min 7 s *.

    Published by: user733179 on March 5, 2009 12:00

    Hello

    To obtain a separate calculation for each value of the usr, start the analytical clause with "BY usr PARTITION":

    WITH     got_dif     AS
    (
         SELECT     id,     dt,     usr
         ,     (dt - LAG (dt)
                    OVER ( PARTITION BY  usr
                              ORDER BY          dt
                            )
              )             -- difference in days
                  * 24 * 60  AS minutes_dif
         FROM     table_x
    --     WHERE     ...     -- if needed
    )
    SELECT     id,     dt,     usr
    FROM     got_dif
    WHERE     minutes_dif     > 30
    ;
    
  • Compare the value of the column with the previous column and Populate values

    Hi all


    Please help me to get the required sql

    I have a table T1 as example below

    Id1 Id2 Column1 Column2
    1 1 name1 null
    1 2 name2 XYZ
    1 3 name3 null
    1 4 name4 abc
    1 5 name5 null



    If the value is null, ishould then fill with the previous value. until he finds a following value.
    The result should be as below

    Id1 Id2 Column1 Column2
    1 1 name1 null
    1 2 name2 XYZ
    1 3 name3 XYZ
    1 4 name4 abc
    1 5 abc name5

    Please help me to get the required sql

    Thanks in advance

    Hello

    Here's one way:

    SELECT    id1
    ,       id2
    ,       column1
    ,       LAST_VALUE (column2  IGNORE NULLS)
                     OVER ( PARTITION BY  id1          -- if needed
                                ORDER BY         id2
                       )        AS column2
    FROM      t1
    ORDER BY  id1, id2                         -- if needed
    ;
    

    I'm guessing only a few things, as what makes a line 'previous' or 'next '.

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all tables and also post the results desired from these data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using. «LAST_VALUE...» IGNORE NULLS"requires that Oracle 10.1 or higher.

  • find the previous owner info (icloud)

    Hi, how can I find the name and surname of the previous owner of my second hand iphone?

    I have the apple ID, but find my iphone is on and I do not know password!

    Thank you

    You can't get from Apple or anyone here. You must contact the former owner, either through the person, you bought the phone you can not. Confidentiality issues prohibit Apple or the cellular carrier to release any of this information. If the person you bought isn't the former owner, then there is the possibility that the unit you have purchased has been stolen.

  • Where can I find the previous version of the BIOS to 2.54?

    I accidentally updated the BIOS for my Lenovo E430. I want to go back to the BIOS, so I need the previous version. No one knows the number of the previous version and where I can find it on the Lenovo Web site?

    Nice day.

    Please take a look at the support site for your machines:

    http://support.Lenovo.com/us/en/products/laptops-and-netbooks/ThinkPad-edge-laptops/ThinkPad-edge-E4...

    If you select the CD or bootable Windows Update link and scroll to the bottom of the resulting page, you'll see a grid of earlier versions, as well as their documentation files.

    As with any update BIOS, please read the documentation carefully.

    I hope this helps a little.

    Kind regards.

  • Windows 7 Image Backup - where can I find the previous image after new backup?

    My previous hard drive died, and I replaced it.

    Good news: I did a backup of the system before the death of completed HDD.  The image is under "WindowsImageBackup" folder on my external drive.

    Then I replaced the HARD drive, picked up at the factory reset.

    Before restoring the image backup, I took a step extra stupid: I thought that I should save its current state before as I have restore the previous state in case any thing wrong, so I started to sign--> system and security--> backup and restore--> backup now

    Bad news: the new backup image sub-folder of "WindowsImageBackup" even erased my previous image!  They have different timestamps, so different names, but the previous image has disappeared after a new creation.  In fact, I killed the new backup process immediately after that I was disappearing from the previous image.  He has not finished, but I can't find my previous image!

    BTW, there is a lot of space on my drive.  It is only 20% used!

    Are there ways I can retrieve my previous image?

    BTW, I use Windows 7 Home Premium.  By default, all parameters are as I do factory reset.

    Suggestions and help is very appreciated!

    Hello

    With the publication of the description, I understand that you have accidentally deleted the system image backup file. I've certainly you will help answer your query.

    I wish to inform you that there is no way to get the previous system back image - which has already been replaced however, you can try a few steps to get the old image to the top, although there is no guarantee that it will work but still, you can try and see if it helps.

    First of all, I suggest you to rename the WindowsImageBackup to WindowsImageBackup. Old then follow the screenshot and select the option highlighted in yellow color select another backup to restore files of.

    Hope that this help, please write us back for any further assistance on this point, we will be happy to help you further.

  • % of column based on one of the lines in the previous column

    Hello.

    I need to build the report that follows in the responses:
    Column 1 is sales and expenses.
    Column 2 is all lines in column 1 divided by the first row of the column 1 (costs divided by sales) * 100

    ----------------Value---%
    Sales... 1000 100
    Purchase-400 - 40
    Staff costs. -100-10
    Advertising - 50-5
    Result... 450 45

    My problem accessing the sales column line 1 och, use it in a formula in column 2.
    Someone knows a way to solve this in the answers?

    Oracle BI Server is 10.1.3
    Joel

    Published by: joel_s on August 19, 2011 07:35

    Published by: joel_s on August 19, 2011 07:36

    Published by: joel_s on August 19, 2011 07:36

    Published by: joel_s on August 19, 2011 07:37

    Hi Joel,

    If you want it in % of the total, then the formula will be col1_measure/sum(case when dimension = sales then col1_measure else 0 end).

    If you want a percentage of a group (for example account_id) then the formula will be col1_measure/sum(case when dimension = sales then col1_measure else 0 end by account_id).

    Kind regards

    Robert

    Published by: Robert Tooker August 19, 2011 08:37

    Published by: Robert Tooker August 19, 2011 08:40

  • Where can I find the Version column in the repository Designer?

    I use Oracle Designer 10.1.2.6 and need to know where to find the version current # for a module.


    That's what I'm looking for:

    Designer-Version.png

    I thought I could find it at sdd_mod, which has Short_Name, but alas, it wasn't there.


    FYI, I recently got good help here to find the Description, which is located in CDI_TEXT

    Designer-Description.png



    Hello Wim,

    Check the VLABEL into the sdd_object_versions table.

    Kind regards

    Mark

  • The ATI driver installation cannot find the previous installation?

    Hello

    Yestreday, I wanted to update my current graphics driver AMD high definition and so downloaded the latest version. Before installation, the installation wizard stated that no previous directory was found and would create a new. That didn't make much sense to me, as I already had the previous version of the driver on my laptop. After a bit of research, I realized that the previous version was installed in the program (x 86), and the new system would be installed in Program Files instead

    My question is; I still want to update the driver and how do I go about this? Must delete the existing one and install the new drivers?

    My laptop model is dv7 - 4198ca with ATI Radeon HD 5470 Switchable Graphics.

    Thanks in advance.

    I have a laptop different but the same 5470 video card. Update all video pilots made me a lot of problems, I had to restore a former win7 restore point to get the Panel to catalyst and switchable graphics to work again.

    During the update, I was warned with the same message as you (no previous directory).

    I suggest, if you dare, to update the drivers ONLY (for version 1.771 I guess) choose updated manual and then uncheck the other characteristics (catalyst, frame MS C++, etc). I also suggest you to create a manual restore point before starting.

Maybe you are looking for

  • Error reading of my own music: "Media Player cannot download Music Rights.

    Split of: "bemedia player can not download usage rights" Hello, my Media Player is defective and a big pain in the back. I get the message ' Media Player cannot download music rights ", but it does if I want to play my own music ay n! It's the music,

  • Reboot in 10 minutes or 4 hours?

    Or whenever the heck I chose! Seriously, why can't I just see the stupid restart message updated whenever I feel like I want to. I can't leave my computer for a long time without the fear of it restart because of stupid updates and possibly data loss

  • Best printer for perforated vinyl

    Hi im a designer and im interested in printing on vinyl (preferably perforated vinyl) I am looking for a small printer not too much just make some small designs with good picture and details. Would be nice if it could also cut the design. I hope you

  • The blackBerry Smartphones Email reconciliation

    I have my gmail account set up on my "BOLD" and everything works beautifully except a question of reconciliation, I have.  In my email reconciliation options, for "on conflicts', I selected"victories of the mailbox".  From my understanding, this mean

  • New analysis of data warehouses... at the level of the Cluster with PowerCLI 5?

    HelloSince vSphere has been provided (I think) the option to open a new analysis of the HBA on every host in the cluster ESXs is available in the GUI if you click with the right button on the cluster, select analyze again to data warehouses.I have re