How can can i subtract the value of the column in each row ?

I want to subtract  the value  of the column in each row if the row is not enough then continue to next row.
For example 
ID             QTY
A               20
B               40       
C               60
I want to update this table by subtract  the value  of the column (QTY) out 70 so the result i want will be 
ID              QTY
A                20 - 70 = -50 -->  0 this row will be updated to 0 and 50 will continue to next row   
B                40 - 50  = -10 -->  0   this row will be updated to 0 and 10 will continue to next row
C                60 - 10  = 50  -->  Stop loop
How can i write the sql query for this operation , Thanks

Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. You have no idea,
do you? Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
This is minimal polite behavior on SQL forums. What did you try on your own before posting? I will bet that you did nothing! You expect other people to do your job or homework for you. 
>> I want to subtract the value of the column in each row if the row is not enough then continue to next row. <<
This makes no sense. Rows have no ordering; that is a spreadsheet. There is no such thing as a generic “id” in RDBMS. And an identifier is not a sequence which would have an ordering. 
CREATE TABLE Foobar
(something_seq INTEGER NOT NULL PRIMARY KEY,
 onhand_qty INTEGER NOT NULL);
Learn how to use the SUM()OVER() and LAG() aggregate functions, post what you tried for yourself and then we will help you. 
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL

Similar Messages

  • How to update the value of a column when a row is deleted

    Hi,
    My requirement is basically i have a table .Below is a sample data along with sample data
    Column A Column B ColumnC
    1 SAM 0
    2 RAM 0
    Now if i run a delete statement saying
    delete from Table where ColumnA =1 then i want to see the value of ColumnC to be updated as 1.The row shouldnt be deleted however the value of ColumnC should be changed to 1.
    Can someone please help me with any suggestions? I can create a trigger but i havent worked with triggers so not sure on how to use it.
    Please let me know if i am unclear or if you need any details.
    Thanks

    As I understand what you need is an INSTEAD OF DELETE trigger like below
    CREATE TRIGGER Trg_TableName
    ON TableName
    INSTEAD OF DELETE
    AS
    BEGIN
    UPDATE t
    SET ColumnC = 1
    FROM TableName t
    WHERE EXISTS (SELECT 1
    FROM DELETED
    WHERE ColumnA = t.ColumnA
    END
    this will set ColumnC to 1 and will not do an actual delete of the row.
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Match values of 2 columns with other rows

    I want to compare the values of 2 columns with all rows in the sheet.
    If there is a match I want to place a character in the next column, so I can filter on it
    But I need to keep one row for each unique value. (match of 2 columns)

    Hi,
    I'm marking the reply as answer as there has been no update for a couple of days.
    If you come back to find it doesn't work for you, please reply to us and unmark the answer.
    Thanks
    George Zhao
    Forum Support
    Come back and mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback on our support, please click "[email protected]"

  • How can i display the values in the vector in a jsp using jstl

    in a task i am recieving a vector in a jsp... how can i display those vector values in the jsp using jstl.... plz help me
    thanks in advance

    <%
    here you got vector say; v
    pagecontext.setAttribute("varname",v);
    %>
    <c:forEach var="i" items="${varname}">
    <c:out value="${i}">
    </c:forEach>

  • How can I get the value of "Warehose" column in a form

    How can I get the value of "Warehouse" column in the form below (I mean what table that contain this value):
    Production Supervisor >> Batches >> (Button) Material Details >> (Button) Line Allocations
    Well, for more clearly! My problem is I must have the Unit Cost of Items, so I've got it in the cm_cmpt_dtl (table), but if I want to, I must create a relation that require 2 filed, they're Item_ID and Whse_Code.
    There's no problem with Item_ID, but Whse_Code seem to be the Mission Impossible (hix, I hate that film!!!!)
    I wonder if It was right to post this topic here. But anyway I just post my question here, hope I could get some help.

    wow, many, many, many.... and many thanks!
    Just add a tiny modify to check out the Batch_type
    doc_id = (select batch_id from gme_batch_header where batch_no='&batch_number' and batch_type = 0)
    Because the batch_no can be duplicated as we also create Batch and Filrm Planned Order.
    Many thanks for your support!
    P/S: Sorry for my terrible English :P

  • How can i transfer a field value in the main report to its sub-report?

    <p><font face="Arial" size="2">How can i transfer a field value in the main report to its sub-report?</font></p><p><font face="Arial" size="2">Please eloberate with example if possible!</font></p><p><font face="Arial" size="2">Thanks...</font></p><p> </p>

    <p>You can do a couple of things - one would be to pass the information using the data linking expert.  Right click on the subreport, choose &#39;Change Subreport Links&#39; and select the field(s) you are wanting to pass to the subreport.  CRW will build parameters and a record selection formula for you in the subreport, and if that&#39;s what you want, then great.  But you can also remove the selection formula from the subreport and work with the parameter fields in the subreport however you would like.</p><p>Alternatively, you can look to passing Shared variables back and forth from the main and subreport.  this link talks about the method to do this: http://diamond.businessobjects.com/node/251</p><p> </p>

  • How can we get the value of the key field in a custom data model using governance API?

    Dear Team,
    How can we get the value of the key field in a custom data model, to be used for manipulation of the change request fields using governance API?
    Any kind of help would be sincerely appreciated.
    Thanks & Regards,
    Tushar.

    Hi Michael,
    Thanks for direction. Let me give more context on this as I'm interested to get more details..One of the issue was to read cross entity field values on UI based on user action and set other entity field behaviour...It is similar to what is being posted here.
    For ex: Reading MTART from Basic Data UIBB in MM MDG UI and set the field properties in some other custom entities say ZZETEST. This cannot be done using UI BADI as it only supports single entity at a time and not cross entity. So alternatively we found a solution where we can enhance existing PLMB feederclass cl_mdg_bs_mat_feeder_form by reading the model and the entity as needed as it it proved that it supports cross entity UI field behaviours and so business requirements.
    This is a workaround for now.
    So the question is How do we achive it using governance API for cross entity field behiaviours.?or what is the right way doing this.
    Can we do that using governance API and its' methods?
    In the Governance API doc you provided below has referring to below external model as part of gevernance API.
    The active or inactive data (before or during the derivation or the check) can be read
    with the external data model interface IF_USMD_MODEL_EXT with the method READ_CHAR_VALUE and
    the corresponding READ_MODE parameter. To avoid unnecessary flushes (derivations), the NO_FLUSH
    parameter should b
    e set to ‘X’.
    Thanks
    Praveen

  • How can i compare the values of the objects in a class?

    hi ,
    i am testing how to test the values of the two objects
    this is the code i have return below.....
    class Sample{
              String name;
              int age;
              Sample(String name,int age){
                   this.name=name;
                   this.age=age;
    public class sample2{
              public static void main (String []arg){
                   Sample one = new Sample("cat",20);
                   Sample two = new Sample("cat",20);
    how can i test that object one and two are equal.
    can anyone clarify me about this?

    hansbig wrote:
    Um, this part doesn't work, it will never return true.
      public boolean equals(Object obj) {
    if (obj == this) {
    return true;
    It will :) try for example objectOne.equals(objectOne); and add a System.out.println() at that part. It will return there.
    >
    but this part, woah dude, that's some pretty slick code you got here:
    I couldn't figure out how to do the compare inside the class, but I think I see how this works.
    Not sure how I would do it for an object with more than one value to check though.
        if (obj instanceof EqualsExample) {               //Not the best implementation considering possible subclasses but the easiest.
    EqualsExample temp = (EqualsExample) obj;
    return (this.name.equals(temp.name)); //Always use the equals function when comparing strings!
    } else {
    return false;
    You could add them in one massive return (this.something == temp.something && etc). Or you could split it in a series of if statements. It's really dependant on what kind of variables there are in your class.
    Maybe you have an ArrayList you need to compare but also an id. then it would make more sense to check the id first and then possibly loop through the arraylist.
    Hope it's a bit more clear :)
    Edited by: hms on Jan 16, 2008 11:25 AM

  • How can I get the value of the previous item

    Hi,
    I have a block with some records in it.
    I want to capture the value of the previous item of the cursor position
    for eg I have emp_no, and emp_name
    If I click emp_name it goes to different block ang brings up emp info. for that particular employee. but to get that I need emp_no
    I'm taking emp_name by giving
    emp_name := system.cursor_value
    how can I capture emp_no value
    Thanks in advance

    Hello,
    try this:
      name := :blk.emp_name ;
      num  := :blk.emp_no ;
      sal  := :blk.emp_sal ;
      etc...Francois

  • How can I filter the Values for the dropdowns in webdynpro

    I have given data source to table, but as there are some repetitive values in table of dropdowns, I want to filter the Values for the dropdowns in webdynpro.
    How can I do this?

    I have given datasource to table in properties of table.
    I have 2 different dropdowns for different columns in the table.
    if the size of the table is 20 I get all the values populated in the dropdowns.
    In these values, there are some repetetive values which I dont want to appear.
    I can display it by making loop and can select from the values.
    the problem is how to set the new values to that drop down again using context?
    eg
    dropdowns have,
    presales activity
    presales activity
    presales activity
    presales activity
    presales activity
    I need to filter the variable element and put back it in dropdown.
    Thanks and regards
    Hanif Kukkalli

  • How can I define that the value of a column should always be in UPPER case

    Hi,
    I want to make sure that the value in a column is always in UPPER case.
    Can we give this condition while creating or altering a table.
    A trigger can do it easily, but I was wondering if we could define a column to have values with upper case all the time, independent of what values are inserted. I mean if we give a lower case value in the insert statement, it should be converted automatically to upper case & stored.
    I want something like
    Alter table MY_TABLE Modify ( col1_upper varchar2(25) default UPPER(Col1_upper));
    But the above statement does not work as it references the col of the table.
    Thanks
    Sunil

    Well, you can put a check constraint on to prevent someone from putting lowercase data in:
    alter table my_table add constraint my_table_check_upper check (col1_upper = upper(col1_upper));However, I know of no way to modify the data being inserted/updated in a table without a trigger.
    Richard

  • How I can obtain the value of a column in a component table?

    thank you
    i have a table component, and which not bound a table database, it only display data, and i desire obtain the value of a column.
    thanks you sorry my english

    Hi!
    Try to use getValue(FieldKey fieldKey, RowKey rowKey) method of your dataprovider.
    Thanks,
    Roman.

  • Can i change the value of the discrete states in EMI_CB_CalculateResetStates?

    can i change the value of the discrete states in EMI_CB_CalculateResetStates?
    The help of LabVIEW said that calculates the values to which to reset the states of an external model in it.But it didn`t said how to reset the discrete states in it.EMI_GetNextDiscreteStates said it can be called only within the EMI_CB_CalculateDiscreteStates function.So if the discrete states can be reset in EMI_CB_CalculateResetStates,how to do it?
    THX

    What is EMI_CB_CalculateResetStates?  If I knew what you were asking about I could give a little more inteligent answer.
    Kelly Bersch
    Certified LabVIEW Developer
    Kudos are always welcome

  • Can we set a default value for the container variable in BPM?

    Can we set a default value for the container variable.?
    Suppose if i have a loop step and i have given a container variable i=5 as end condition.What value will it take during its first execution?Can we set the value for container before a recieve step?

    Hi
    Define Container Variable of Type integer and Category Simple Type .Use Condtion in Loop.
    In Container Operation Step Assign value and Use Expression to
    Increase or Decrease Valus according to your operation.
    look Pattern 4 in this blog to understand Container Operation
    /people/sharathchandra.girmaji/blog/2008/09/11/bpm-with-patterns-explained-part-1

  • Why can't i detect the value of the first column ??

    I need to retreive a list of values from the database.
    But everytime i can only retreive the values from the second rows onwards.
    String sQuery = "SELECT * FROM XTABLE WHERE XGROUP = '" + sGroup + "'";
    resultSet rs = stmt.executeQuery (sQuery);
    while (rs.next())
    sValue = rs.getString (config.sRecpMobile);
    there is supposed to be three columns that matches the query but only two are returned.
    Can anyone help me ?
    Thank you in advance.

    1. right after String sQuery = "....."; use the
    System.out.println(sQuery);
    2. cut and paste the query string that was output to the console.
    3. paste it into SQL*Plus or whatever you database utility program is, and run it.
    4. These results won't lie.
    Probably...
    5. You will realize the error in your SQL. The query you are sending to the database is actually correct, but not what you thought. You will slap yourself in the face in disgust, fix the query and continue programming...and pretend it wasn't your fault! :>)
    Jamie

Maybe you are looking for

  • When I try to sync my ipod, itunes tells me certain songs could not be found

    I have just tried to sync my ipod on itunes, and a screen comes up to tell me that over 100 songs from my library cannot be synced as they could not be found....... They all appear in my music library, and in their relevant playlists, only now they a

  • Check for duplicate record in SQL database before doing INSERT

    Hey guys,        This is part powershell app doing a SQL insert. BUt my question really relates to the SQL insert. I need to do a check of the database PRIOR to doing the insert to check for duplicate records and if it exists then that record needs t

  • Itunes doesn't see my Ipod video

    I downloaded the new Itunes 7.0.2 because I bought the new 30gb video Ipod. Its the second day I spend in trying to connect Itunes with Ipod. It is working as harddisk but Itunes says that I have to reinstall Itunes in order to make the connection. I

  • OpenHub Destination

    I have created the OpenHub destination and can extract a DSO down to my PC in .csv format, however I want to be able to save the file there in an excel format. So I was looking around and I saw this function module GUI_DOWNLOAD. The question then is

  • Get an error when run eclipse

    Hi after download sdk1.3, sdkee1.3.1, eclipse2.0.2 and when run eclipse i got the following error JVM terminated. Exit code=2 c:\WINNT\syste,32\javaw.exe -cp E:\program Files\eclipse\startup.jar org.eclipse.core.launcher.main -os win32 -ws win32 -arc