simple help with a delete statement

Hello guys
I have a table like this
CREATE TABLE FWENCUM
  (
    "FWENCUM_FUND"            VARCHAR2(6 BYTE),
    "FWENCUM_GRNT"            VARCHAR2(9 BYTE),
    "FWENCUM_ORG"             VARCHAR2(6 BYTE),
    "FWENCUM_PROG"            VARCHAR2(6 BYTE),
    "FWENCUM_ACCT"            VARCHAR2(6 BYTE),
    "FWENCUM_ACTV"            VARCHAR2(6 BYTE),
    "FWENCUM_LOCN"            VARCHAR2(6 BYTE),
    "FWENCUM_INDCOST_BUDGET"  NUMBER(17,2),
    "FWENCUM_GRANT_TOTAL"     NUMBER(17,2),
    "FWENCUM_FUND_TOTAL"      NUMBER(17,2),
    "FWENCUM_BUDGET_FUND"     NUMBER(17,2),
    "FWENCUM_YTD_FUND"        NUMBER(17,2),
    "FWENCUM_INDCOST_YTD"     NUMBER(17,2),
    "FWENCUM_INDCOST_COMM"    NUMBER(17,2),
    "FWENCUM_ERROR"           VARCHAR2(2 BYTE),
    "FWENCUM_AMOUNT_ENCUMBER" NUMBER(17,2)
  )
with these values
REM INSERTING into FWENCUM
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('240051','240051','07171','01','7623',' ',' ',131635,34865.08,96873.92,-104,null,null,null,null,-104);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420461','420420','04720','02','7623',' ',' ',15600,12287.6,0,124783.52,null,null,null,null,3312.4);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420462','420420','04330','02','7623',' ',' ',15600,7372.56,0,124783.52,null,null,null,null,8227.44);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420456','420420','23040','02','7623',' ',' ',26000,5810.01,0,124783.52,null,null,null,null,20189.99);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420421','420420','04100','02','7623',' ',' ',15600,12287.6,0,124783.52,null,null,null,null,3312.4);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420422','420420','06370','02','7623',' ',' ',31200,27066.52,0,124783.52,null,null,null,null,4133.48);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420423','420420','23050','02','7623',' ',' ',15600,10171.91,0,124783.52,null,null,null,null,5428.09);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420424','420420','04340','02','7623',' ',' ',15600,0,0,124783.52,null,null,null,null,15600);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420451','420420','23050','02','7623',' ',' ',26000,24078.7,0,124783.52,null,null,null,null,1921.3);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420452','420420','22070','02','7623',' ',' ',26000,4050.97,0,124783.52,null,null,null,null,21949.03);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420453','420420','18350','02','7623',' ',' ',26000,9502.1,0,124783.52,null,null,null,null,16497.9);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420454','420420','04340','02','7623',' ',' ',26000,426.38,0,124783.52,null,null,null,null,25573.62);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420455','420420','09060','02','7623',' ',' ',26000,1561.25,0,124783.52,null,null,null,null,24438.75);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420460','420420','04310','02','7623',' ',' ',15600,12287.6,0,124783.52,null,null,null,null,3312.4);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420458','420420','05690','02','7623',' ',' ',26000,0,0,124783.52,null,null,null,null,26000);
Insert into FWENCUM (FWENCUM_FUND,FWENCUM_GRNT,FWENCUM_ORG,FWENCUM_PROG,FWENCUM_ACCT,FWENCUM_ACTV,FWENCUM_LOCN,FWENCUM_INDCOST_BUDGET,FWENCUM_INDCOST_YTD,FWENCUM_INDCOST_COMM,FWENCUM_GRANT_TOTAL,FWENCUM_FUND_TOTAL,FWENCUM_BUDGET_FUND,FWENCUM_YTD_FUND,FWENCUM_ERROR,FWENCUM_AMOUNT_ENCUMBER) values ('420457','420420','18110','02','7623',' ',' ',26000,3543.58,0,124783.52,null,null,null,null,22456.42);
Basically, I want to delete all the lines where the amount of
 fwencum_amount_ecumber
more then the unique value under
fwencum_grant_total for that grant
by grant code in other words the expected output should be delete 15 ranks.
because the sum of fwencum_Amount_encumber granting 420420 would be higher then the 124,783.52 under the fwencum_grant_Total
I tried to do this, but I keep getting errors I get a little advice.

Here is my poor attempt
DELETE  fwencum
  WHERE  (select sum(f.fwencum_amount_encumber) from fwencum f) > (SELECT DISTINCT 
                                                                    FWENCUM_GRANT_TOTAL 
                                                                    FROM fwencum F, FWENCUM F2
                                                                    WHERE F.fwencum_grnt=F2.FWENCUM_GRNT);
Published by: mlov83 on Sep 22, 2011 19:24

Hello

Thanks for posting the CREATE TABLE and INSERT statemnts; It is very useful.
It seems that only 3 columns play no role in this issue. If so, why you send messages 16 colummns?

What you want (if I understand the problem) is to do a correlated subquery , where the subquery refers to something in the main charge and (potentially) so does something different for each line of the main declaration, like this:

DELETE     fwencum       m     -- m is for main
WHERE     fwencum_grant_total < (
                    SELECT  SUM (fwencum_amount_encumber)
                    FROM     fwencum
                    WHERE     fwencum_grnt     = m.fwencum_grnt
                     )
;

Tags: Database

Similar Questions

  • need help with the Update statement

    Hello
    I received a question in a course and I tried my best to respond, and now my brain is giving. I would really appreciate help with the update statement. I don't mind if you do not validate a solution, a little nudge in the right direction would be really useful. I'll post that I got.

    THE QUESTION
    / * For these agents disabled on more than seven missions, change their date of deactivation of the first date of deactivation of all the agents that have been activated in the same year as the agent that you update currently.
    */

    I have it divided into parts, here is my select statement to agents disabled on more than 7 missions, which produces the deactivation_dates in the agents table that I want to update...
    SELECT
    s.deactivation_date
    FROM
    (
    SELECT
    a.deactivation_date,
    count(m.mission_id) as nomissions
    FROM
    agents a
    INNER JOIN
    missions_agents m
    on
    a.agent_id=m.agent_id
    GROUP BY
    a.deactivation_date
    ) s
    WHERE
    s.nomissions>7 AND s.deactivation_date IS NOT NULL
    .. .and the code for the first date of deactivation for each year of activation agent
    select 
    a2.deactivation_date
    from
    agents a2
    where a2.deactivation_date= 
    (
    select min(a.deactivation_date)
    from 
    agents a
    where to_number(to_char(a.activation_date,'YYYY'))=to_number(to_char(a2.activation_date,'YYYY'))
    )
    ..... I am not real to marry these two statements together in the Update statement. I can't extract each date of deactivation produced in the first select statement and their match against the first date of deactivation in the year they have been activated for the second select statement.

    Any help greatly appreciated... :))

    I began to wonder how things would :)

    user8695469 wrote:
    First of all why he chooses the date the earliest of all agents

    UPDATE  AGENTS_COPY AC /* (1) */
    SET     DEACTIVATION_DATE = (
    SELECT  MIN(AGS.DEACTIVATION_DATE)
    FROM    AGENTS_COPY  AGS
    ,       AGENTS_COPY AC /* (2) */
    WHERE   TRUNC(AGS.ACTIVATION_DATE,'YEAR') = TRUNC(AC.ACTIVATION_DATE,'YEAR') /* (3) */
    )
    

    He recovers as soon as the subquery has not been correctly set in the SET clause. It seems you are trying to update a correlated, but we are still having a conceptual shift. I have added a few comments to your code above and below will explain.

    (1): when you do a correlated update it is useful to the table alias that you did right here.

    (2): this table statement is not necessary and is the reason why the FIRST deactivation date is selected. The alias that you use (3) refers to THIS table, not the one defined in the update statement. Remove the line indicated by (2) in the FROM clause and a correlated update will happen.

    and secondly why is it to update each row, when I thought that I'm just the lines where the agents are disabled and missions > 7? Pointers on where I'm wrong would be very appreciated. (SQL = stupid query language!) :)

    user8695469 wrote: then why is it to update each row, when I thought that I'm just the lines where the agents are disabled and missions > 7? Pointers on where I'm wrong would be very appreciated. (SQL = stupid query language!) :)

    
    WHERE EXISTS
    (
    SELECT
    a.agent_id,
    count(m.mission_id)
    FROM
    agents a
    /* INNER JOIN AC ON AC.AGENT_ID = A.AGENT_ID */
    INNER JOIN
    missions_agents m
    ON
    a.agent_id=m.agent_id
    GROUP BY
    a.agent_id,
    a.deactivation_date
    HAVING
    count(m.mission_id)>7 AND a.deactivation_date IS NOT NULL
    )
    

    Once again this problem is similar to the question above that a correlation update doesn't work. Test existence of lines in an EXISTS subquery. Since your subquery is not related to the table that you are trying to update, it will be always return a line and, therefore, it returns true for EACH LINE in the AGENTS table. To limit the game to only agents > 7 missions results, you need to add a join condition that references the table in your update statement. I added one above (with comments) as a sample.

    I recommend you look over all material that you have associated with correlated subqueries, including documents that I posted above. This seems to be what you're having the problem more with. If you need me to explain the concept of correlated queries any better please let me know.

    Thank you!

  • Help with the insert statement

    Hello

    I was wondering if someone could help write me a sql statement.

    Here is my table:
    CREATE TABLE "TEMP_INVOICE" 
       ("INVOICE" VARCHAR2(100 BYTE),
         "DATE_OF_DOCUMENT" DATE, 
         "DATE_OF_PAY_DAY" DATE, 
         "D" NUMBER, 
         "K" NUMBER
       );
    Here are the instructions for correct insertion. This time, I posted 2 examples with 2 numbers different otherwise.
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (1000,to_date('01.02.2012','dd.mm.yyyy'),to_date('01.03.2012','dd.mm.yyyy'),5000,0);
     
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (1000,to_date('01.04.2012','dd.mm.yyyy'),'','',1000);
     
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (1000,to_date('01.05.2012','dd.mm.yyyy'),'','',3000);
     
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (1000,to_date('01.06.2012','dd.mm.yyyy'),'','',1000);
    
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (2000,to_date('01.07.2012','dd.mm.yyyy'),to_date('01.09.2012','dd.mm.yyyy'),8000,0);
     
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (2000,to_date('01.10.2012','dd.mm.yyyy'),'','',5000);
     
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (2000,to_date('01.11.2012','dd.mm.yyyy'),'','',2000);
     
    insert into temp_invoice (invoice,DATE_OF_DOCUMENT,DATE_OF_PAY_DAY,d,k)  values (2000,to_date('01.12.2012','dd.mm.yyyy'),'','',1000);
    I want to do is make an insert allows you to table another call is MADE:
    CREATE TABLE "INVOICE" 
       ("INVOICE" VARCHAR2(100 BYTE),
        "DATE_OF_DOCUMENT" DATE, 
         "DATE_OF_PAY_DAY" DATE,
         "DATE_OF_PAYMENT_REC" DATE,
         "VALUE" NUMBER,
         "VALUE_DEDUCT" NUMBER,
        "DATE_FROM" DATE,     
         "DATE_TO" DATE
         );
    Statements in the INVOICE table should be like this:


    ........ The Bill... date_of_document... date_of_pay_day... date_of_payment_rec... value... value_deduct... Date_from... Date_to
    1......     1000...............1.1.2012.................1.3.2012................NULL............................ 5000... NULL... 1.3.2012...1.4.2012
    2......     1000...............1.4.2012.................NULL..................... 1.4.2012...1000... 4000... 2.4.2012... 1.5.2012
    3......     1000...............1.5.2012.................NULL..................... 1.5.2012...3000... 1000... 2.5.2012... 1.6.2012
    4......     1000...............1.6.2012.................NULL..................... 1.6.2012...1000


    Can someone help me with the sql statement that would insert data from table to table Bill temp_invoice as in the example?

    Thank you!

    PS

    I would try to explain.
    (1) the first statement that is to be inserted is original imply that at which is different DATE_OF_PAY_DAY to NULL.
    To this inserted negative of the original imply, we must add date_from that is exatly the same date_of_pay_day and date_to which is exactly the same as the date of the first payment. Payment which came first!
    (2) we have now in the second insert statement. It will be the first payment of lease with date_of_document and date_pf_payment_rec, which is the same as date_fo_document. Value field will be populated with the amount of payment received and value_deduct field will be the value of the original imply - value of the first payment. Date is date_of_document + 1 and date_to is the date of the next payment.
    (3) Insert us the next installment. Date_od_payment_rec is the same as date_of_document... value is the amount of the second payment and value_deduct's previous value_deduct which was of 4000 - value of this second payment. date_from date_of_payment_rec + 1 and date_to is the date of the next payment

    So we continue this same pattern until we reached the final payment when we finish insert with the statement:
    Date of the document (date of the last payment received) and even for date_of_payment_rec and field value with the amount of the payment receieved. The rest (value_deduct, date_from, date_to) is null.

    I really hope you understand what I'm trying to do here.

    If you have any other questions please.

    Published by: user13071990 on November 22, 2012 04:16

    Published by: user13071990 on November 22, 2012 04:16

    Hello

    user13071990 wrote:
    ... Here are the instructions for correct insertion. This time, I posted 2 examples with 2 numbers different otherwise.

    Ok!
    Be sure to post the results you want new data.

    You probably need to add "PARTITION BY the Bill" to all analytical clauses in my solution:

    INSERT INTO invoice
    ( invoice, date_of_document, date_of_pay_day, date_of_payment_rec
    , value,   value_deduct,     date_from,           date_to
    )
    SELECT       invoice
    ,       date_of_document
    ,       date_of_pay_day
    ,       CASE
               WHEN  k > 0
               THEN  date_of_document
           END          AS date_of_payment_rec
    ,       NVL (d, k)     AS value
    ,       NVL2 ( date_of_pay_day
                , NULL
                , SUM (d) OVER ( PARTITION BY  invoice
                                          ORDER BY      date_of_document
                         )
                - SUM (k) OVER ( PARTITION BY  invoice
                                          ORDER BY      date_of_document
                         )
                )     AS value_deduct
    ,       NVL ( date_of_pay_day
               , date_of_document + 1
               )          AS date_from
    ,       LEAD (date_of_document) OVER ( PARTITION BY  invoice
                                            ORDER BY      date_of_document
                               )
                          AS date_to
    FROM       temp_invoice
    ;
    

    Because I'm not an English speaker nativ, that I just posted what it should look like after the insert is successful.

    OK, so you can't explain as you want, but you still need to explain.

    ... @Frank Kulash: you are very close, but still not quite what I'm looking for.

    Point out where my solution is the production of incorrect results, and explain (as you can) how to get good results in these places.

  • Need help with the insert statement

    Hello

    I have a question on how to write a SQL statement.

    This is the table of "base":
    CREATE TABLE TEMP_TBL
    (
    id_nr NUMBER,
    DATE_DOK DATE,
    DATE_DUE DATE,
    DATE_DOK_PAY DATE,
    DEB NUMBER,
    KRD NUMBER
    );
    
    insert into temp_tbl (ID_NR,DATE_DOK,DATE_DUE,DATE_DOK_PAY,DEB,KRD)values('1',TO_DATE('11.01.2011','DD.MM.YYYY'),TO_DATE('25.02.2011','DD.MM.YYYY'),NULL,'423,24','0');
    insert into temp_tbl(ID_NR,DATE_DOK,DATE_DUE,DATE_DOK_PAY,DEB,KRD)values('2',TO_DATE('16.12.2011','DD.MM.YYYY'),TO_DATE('13.06.2011','DD.MM.YYYY'),NULL,'91270,15','0');
    insert into temp_tbl(ID_NR,DATE_DOK,DATE_DUE,DATE_DOK_PAY,DEB,KRD)values('3',TO_DATE('27.09.2011','DD.MM.YYYY'),TO_DATE('27.09.2011','DD.MM.YYYY'),NULL,'0','2000');
    and it comes to resoult in the target table. SQL statement must take care of the insert in a base of the target table table (example below is already provided with test data).
    create table table_sod
    (
    
          id_nr number
         ,date_from date
         ,date_to date
         ,deb_krd number
    
    );
    
    One thing to note here :  values in column deb_krd under insert 1 and 4 must be summarized in insert 4.
    
    insert into table_sod (id_nr,date_from,date_to,deb_krd) values('1',null,to_date('25.02.2011','dd.mm.yyyy'),'423,24');
    insert into table_sod(id_nr,date_from,date_to,deb_krd) values('2',to_date('26.02.2011','dd.mm.yyyy'),to_date('13.06.2011','dd.mm.yyyy'),'423,24');
    insert into table_sod(id_nr,date_from,date_to,deb_krd)values('3',null,to_date('13.06.2011','dd.mm.yyyy'),'91270,15');
    insert into table_sod(id_nr,date_from,date_to,deb_krd)values('4',to_date('14.06.2011','dd.mm.yyyy')to_date('27.09.2011','dd.mm.yyyy'),'91693,39');
    insert into table_sod(id_nr,date_from,date_to,deb_krd)values('5',null,to_date('27.09.2011','dd.mm.yyyy'),'2000');
    If someone could give me a helping hand how write correct insert statement I would be really gratefull.

    Thank you for your time!

    I came here with my own assumptions with this

    SQL> select rownum id_nr
      2       , date_from
      3       , date_to
      4       , case when date_from is null then deb
      5              else sum(case when date_from is not null then deb else 0end) over(order by id_nr, no)
      6         end deb_krd
      7    from (
      8            select id_nr
      9                 , case when lg_dt is null or ld_dt = date_due then  null else lg_dt+1 end date_from
     10                 , ld_dt date_to
     11                 , case when deb = 0 then krd else deb end deb
     12                 , no
     13              from (
     14                      select t1.*
     15                           , lead(t1.date_due) over(order by t1.id_nr, t2.no) ld_dt
     16                           , lag(t1.date_due) over(order by t1.id_nr, t2.no) lg_dt
     17                           , t2.no
     18                        from temp_tbl t1
     19                       cross
     20                       join (select 1 no from dual union all select 2 no from dual) t2
     21                       order by id_nr, no
     22                   )
     23             where ld_dt is not null
     24         )
     25  /
    
         ID_NR DATE_FROM DATE_TO      DEB_KRD
    ---------- --------- --------- ----------
             1           25-FEB-11      42324
             2 26-FEB-11 13-JUN-11      42324
             3           13-JUN-11    9127015
             4 14-JUN-11 27-SEP-11    9169339
             5           27-SEP-11       2000
     
    
  • Helps with the UPDATE statement

    Hello

    I have a table like this:
    create table test
    (id number,
    stat number,
    id_num number);
    data in the table:
    insert into table test (id,stat,id_num) values (1,112,'');
    insert into table test (id,stat,id_num) values (1,123,'');
    insert into table test (id,stat,id_num) values (2,134,'');
    insert into table test (id,stat,id_num) values (2,111,'');
    insert into table test (id,stat,id_num) values (3,112,'');
    insert into table test (id,stat,id_num) values (4,111,'');
    insert into table test (id,stat,id_num) values (4,12,'');
    insert into table test (id,stat,id_num) values (4,11,'');
    I want to update the column id_num with serial number under the same ID.
    Below is the table with coloumn successfully updated.
    insert into table test (id,stat,id_num) values (1,112,1);
    insert into table test (id,stat,id_num) values (1,123,2);
    insert into table test (id,stat,id_num) values (2,134,1);
    insert into table test (id,stat,id_num) values (2,111,2);
    insert into table test (id,stat,id_num) values (3,112,1);
    insert into table test (id,stat,id_num) values (4,111,1);
    insert into table test (id,stat,id_num) values (4,12,2);
    insert into table test (id,stat,id_num) values (4,11,3);
    Can someone give me a hint how to remove this?

    Thank you very much for your help!

    user13071990 wrote:
    Manik which is not a good solution because dat_document can also be duplicated.

    Alberto is no uniq key in the table, which is the main problem.

    It is not a good way to get the table without unique keys.

    To work around the problem, I suggest that, despite the fact that I do not use virtual ROWID.

    I did a test by inserting a date twice for the same id

    insert into test (id,dat_document,id_num) values (4,to_date('01.07.2012','dd.mm.yyyy'),'');
    

    Now, I used:

    MERGE INTO test a
         USING (SELECT rowid
                     , ROW_NUMBER () OVER (PARTITION BY id ORDER BY dat_document) AS val
                  FROM test) b
            ON (a.rowid = b.rowid)
    WHEN MATCHED
    THEN
       UPDATE SET id_num = val;
    
    SELECT *
      FROM test
    ORDER BY id, dat_document;
    
            ID DAT_DOCUMENT              ID_NUM
    ---------- --------------------- ----------
             1 01-01-2012 00:00:00            1
             1 01-02-2012 00:00:00            2
             2 01-03-2012 00:00:00            1
             2 01-04-2012 00:00:00            2
             3 01-05-2012 00:00:00            1
             4 01-06-2012 00:00:00            1
             4 01-07-2012 00:00:00            2
             4 01-07-2012 00:00:00            3
             4 01-08-2012 00:00:00            4
    

    Kind regards.
    Al

  • Need help with the IF statement.

    I am trying to write an IF statement using FormCalc in LiveCycle. I have the "tpe" cell that captures the total number of points. I would attribute levels based on the number of points obtained. For example, level I = 1001-1985; level II = 851-1000; level III = 551-850; level IV = < 551. I want to 'level' of cell to show I, II, III or IV, based on the points in the cell "tpe". How can I do this? I have zero familiarity with FormCalc or scripts in general. Can someone help me with this please? Would appreciate any help! Thank you!

    Just put the script in the field calculate level instead:

    If (tpe tpe and > 1000<= 1985)="">

    $ = "I"

    ElseIf (tpe > 850 and tpe<= 1000)="">

    $ = « II »

    ElseIf (tpe > 550 and tpe<= 850)="">

    $ = 'III'

    ElseIf (tpe<= 550)="">

    $ = « IV »

    else $ = «»

    endif

    Also just have a glance to your form - for the next time that you could save yourself a lot of time in your configuration through the use of tables for all of these fields, rather than making each a numeric field separated.

  • Help with iTunes Deleted titles

    When you attempt to delete a playlist, I managed to delete 16 000 songs in my iTunes library. All these tracks are now sitting in my garbage can so I was hoping I might be able to re-add-the as quickly and easily as I had managed to remove them. This doesn't seem to be possible. I can't drag the songs into iTunes. I tried with one, three, and even all the tracks at once and it just doesn't work.

    I can't restore the iTunes music or anywhere else that I'm not a Subscriber so my only hope is to get them to take out the trash and in iTunes. I tried to copy all tracks in a folder on my desktop, but this does not work either. Any advice would be greatly appreciated. Thank you

    Refer to this guide: http://www.macworld.co.uk/how-to/mac-software/recover-deleted-files-photos-mac-i pad-iphone-3510559 /

  • Help with Machines at State

    I implement my application using state machines, and I have the following problem.

    1. I have an initialized state that initialize my controls.

    2. then, I want the next State to check user input. (Boolean button will be be supported to determine what state is planned).

    3. in one of the States, I want the user to be able to change the values initialized and then return to the State of user input, in order to determine what state is expected

    I put in place the initialization state, but also the State of user input (use event structure for this report), but I don't know how to let the user may change the values of my control, the user acknowledges the change then the State goes back to the State of waiting for the next state user input.

    Thanks for any help

    Your user input is also what I would call an idle state.  If nothing happens, it just goes back to itself.  (Put some statement waiting there so that the idle state is not acting as a "greedy loop".)  If change is found, then instead of the called resting State, the initialization state is called again.  The initialization state is running and when it's done it goes back to idle input state.

  • help with if else statement and math.random

    Hi a little new to js and need to fill in a text field with the text selected randomly from a range when a button depending on what is put into another text field that is also randomly.

    I really hope that makes sense but here is code that I got so far

    var bg = this.getField("Background").valueAsString;

    var pt_sage = new array();

    pt_sage [0] = 'of the text based on background. "

    pt_Sage [1] = 'of the text based on background. "

    var i = Math.floor (2 * Math.random ())

    var pt_hermit = new array();

    pt_hermit [0] = "text".

    pt_hermit [1] = "text more»

    var v = Math.floor (2 * Math.random ())

    If (bg == 'Wise') {}

    this.getField("PersonalityTraits").value = pt_Sage [i];

    } else (bg == 'Hermit') {}

    this.getField("PersonalityTraits").value = pt_hermit [v];

    }

    I hope someone can help and thanks in advance

    You must change this:

    var pt_sage = new array();

    To do this:

    var pt_sage = new Array();

    Better yet, change it to:

    var pt_sage = ["a text based on the merits," "a text based on the"];

  • need help with the Merge statement

    I'm on: Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production

    I'm currently an UPDATE statement and then it works and is accurate, it takes 30 minutes. I heard that MERGE
    can do the same thing and is just as accurate and much faster!

    Here's the query I want to convert into a MERGE INTO statement. I tried to do it myself, but I get errors and don't know simply, since it's new for me.

    Basically I want to update table your on a corresponding condition in the table tt for 2 columns (GTP and UPDATE_DT), for UPDATE_DT I want to insert the Date current system.
    UPDATE /*+ PARALLEL (16) */  OLDER_Table ta
    
    SET (ta.GTP, ta.UPDATE_DT) = 
    
                    (SELECT /*+ PARALLEL (16) */ tt.GTP, SYSDATE
                     FROM NEWER_Table tt
                     WHERE ta.cust_id = tt.cust_id
                     AND ta.STAMP_DATE = tt.STAMP_DATE
                     AND ROWNUM = 1)
    
                     WHERE EXISTS (SELECT 1
                                   FROM NEWER_Table tt
                                   WHERE ta.cust_id = tt.cust_id
                                   AND ta.STAMP_DATE = tt.STAMP_DATE
                                   AND (NVL(ta.GTP, 'X') != NVL(tt.GTP, 'X'))); 
    Thank you!

    Hello

    Whenever you have a problem, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) of all of the tables involved.
    Also post the results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.
    If you ask on a DML statement, such as UPDATE, CREATE TABLE and INSERT statements need to re - create the tables as they are before the DML, and results will be the content of the or the tables changed when it's all over.
    See the FAQ forum {message identifier: = 9360002}

    MERGE peut be much faster than update; Sometimes, there is no significant difference in speed.
    Tables in multiple situations like this, I've never seen a case where the MERGER has been slower than the UPDATE, even if the UPDATE is sometimes easier to code and just as fast.

    Maybe that's what you want:

    MERGE INTO  older_table            dst
    USING  (
               SELECT  n.cust_id
            ,        n.stamp_date
            ,        n.gtp
            ,        ROW_NUMBER () OVER ( PARTITION BY  n.cust_id
                                               ,           n.stamp_date
                             ORDER BY      n.gtp
                              )                    AS r_num
            FROM        newer_table  n
            JOIN        older_table     o  ON   n.cust_id     = o.cust_id
                                 AND     n.stamp_date     = o.stamp_date
                           AND     NVL ( n.gtp
                                    , 'X'
                                 )          != NVL ( o.gtp
                                                  , 'X'
                                              )
    
           )                 src
    ON     (    src.cust_id            = dst.cust_id
           AND  src.stamp_date     = dst.stamp_date
           AND  src.r_num            = 1
           )
    WHEN MATCHED THEN UPDATE
    SET    dst.gtp          = src.gtp
    ,      dst.update_date     = SYSDATE
    ;
    

    As I can't test it, I can't be sure.

  • Help with a SQL statement...

    Hello...

    I have tried to find a way to write a SQL statement and may not seem to produce something that works. What I need is a set of results that returns each month that a given task is in an open State. The only columns that I have in my work table with are TASK_NM, OPEN_DT and CLOSED_DT.

    So if I have a line containing the following: TASK_NM = 'task one', OPEN_DT = April 18, 10 ' and CLOSED_DT = 14 October 10 '... I would like to see as a result set that looks like:

    TASK_NM YEAR JAN FEB MAR APR MAY JUNE JULY AUG SEP OCT NOV DEC
    the task of a 2010 0 0 0 1 1 1 1 1 1 1 0 0

    Is it still possible?

    Thanks in advance,
    David.

    Published by: user13027968 on March 10, 2011 14:19

    Published by: user13027968 on March 10, 2011 15:13

    Hello

    Welcome to the Forum!

    Whenever you have a question. Please post a small example of data (CREATE TABLE and INSERT statements) and the results desired from these data.
    For example, the sample data may be:

    CREATE TABLE     table_x
    (       task_nm          VARCHAR2 (10)
    ,     open_dt          DATE
    ,     closed_dt     DATE
    );
    
    INSERT INTO table_x (task_nm, open_dt, closed_dt) VALUES ('task a', DATE '2010-04-18', DATE '2010-10-14');
    INSERT INTO table_x (task_nm, open_dt, closed_dt) VALUES ('foo',    DATE '2010-10-31', DATE '2011-01-01');
    INSERT INTO table_x (task_nm, open_dt, closed_dt) VALUES ('foo',    DATE '2010-12-01', DATE '2011-02-21');
    INSERT INTO table_x (task_nm, open_dt, closed_dt) VALUES ('foo',    DATE '2011-04-01', DATE '2011-04-30');
    INSERT INTO table_x (task_nm, open_dt, closed_dt) VALUES ('bar',    DATE '2010-01-01', DATE '2010-01-01');
    

    From this data, you may want these results:

    TASK_NM     YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OVT NOV DEC
    ---------- ----- --- --- --- --- --- --- --- --- --- --- --- ---
    bar         2010   1   0   0   0   0   0   0   0   0   0   0   0
    foo         2010   0   0   0   0   0   0   0   0   0   1   1   1
    foo         2011   1   1   0   1   0   0   0   0   0   0   0   0
    task a      2010   0   0   0   1   1   1   1   1   1   1   0   0
    

    I'm not sure eactly how you plan to use it. Are there lines that overlap for the name of the task? You always want the same number of columns? What version of Oracle are you using?
    I think you want something like this:

    WITH     got_month_cnt          AS
    (
         SELECT  task_nm
         ,     TRUNC (open_dt, 'MONTH')     AS first_month
         ,     1 + MONTHS_BETWEEN ( TRUNC (closed_dt, 'MONTH')
                             , TRUNC (open_dt,   'MONTH')
                           )          AS month_cnt
         FROM    table_x
    --     WHERE     ...     -- If you want any filtering, put it here
    )
    ,     cntr     AS
    (
         SELECT     LEVEL     AS n
         FROM     (
                   SELECT  MAX (month_cnt)     AS max_month_cnt
                   FROM     got_month_cnt
              )
         CONNECT BY     LEVEL     <= max_month_cnt
    )
    ,     all_months     AS
    (
         SELECT     m.task_nm
         ,     ADD_MONTHS ( m.first_month
                      , c.n - 1
                      )          AS open_month
         FROM     got_month_cnt     m
         JOIN     cntr          c  ON     c.n     <= m.month_cnt
    )
    SELECT       task_nm
    ,       EXTRACT (YEAR FROM open_month)                         AS year
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Jan' THEN 1 ELSE 0 END)     AS jan
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Feb' THEN 1 ELSE 0 END)     AS feb
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Mar' THEN 1 ELSE 0 END)     AS mar
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Apr' THEN 1 ELSE 0 END)     AS apr
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'May' THEN 1 ELSE 0 END)     AS may
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Jun' THEN 1 ELSE 0 END)     AS jun
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Jul' THEN 1 ELSE 0 END)     AS jul
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Aug' THEN 1 ELSE 0 END)     AS aug
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Sep' THEN 1 ELSE 0 END)     AS sep
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Oct' THEN 1 ELSE 0 END)     AS ovt
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Nov' THEN 1 ELSE 0 END)     AS nov
    ,       MAX (CASE WHEN TO_CHAR (open_month, 'Mon') = 'Dec' THEN 1 ELSE 0 END)     AS dec
    FROM       all_months
    GROUP BY  task_nm
    ,       EXTRACT (YEAR FROM open_month)
    ORDER BY  task_nm
    ,            year
    ;
    

    This will work in Orfacle 9 (and), but from Oracle 11, you could simplify it a little bit by using SELECT... Function PIVOT.

    Published by: Frank Kulash, March 10, 2011 16:51

  • Need help with CASE When statement in a Where Clause

    So I have a SQL (simplified for this forum)

    Select t1.*
    from table1, table2 t2 t1
    where t1.field1 = t2.field1
    and when t1.field2 is null then trunc (sysdate) < = trunc (t1.date1 + 17)
    of another trunc (sydate) > = end of trunc (t2.date2 + t2.date3)

    I end up getting an error ORA-00905: lack of keyword

    I'm sure that I just got something here involved.

    You can not make the comparison within the statement underlying case like this. Assuming you have appropriate data types, something like this should work

    Select t1.*
    from table1 t1, table2 t2
    where t1.field1 = t2.field1
      and ((t1.field2 is null and
            trunc(sysdate) <= trunc(t1.date1 + 17)) or
           trunc(sydate) > = trunc(t2.date2 + t2.date3))
    

    John

  • Need help with an if statement

    I have three variables: subA, subB and subC. Initially, I got the calculation such as (subC = subA/subB) then I have implemented a model so subC presented as a percentage. This leads to the possibility of subC is > 100%, which the customer has not loved. They asked if subC > 100% N/A appear. So I wrote the following script, which I'm sure is fake because I don't have a lot of experience script and I had a syntax error. Thanks in advance for your help.

    Form1. #subform [9]. subC::calculate - (FormCalc, client)

    If (subC > 1) then

    subC = "N/a".

    ElseIf (subC < = 1) then

    (subA/subB = subC)

    endif

    Good to hear. If you mark as answer, it will save others to look at the wire.

  • Help with multiple 'if' statement

    If 'age' is greater than 17 and 'relationship' is also one of the other options as son or daughter, I would a subform to appear.  Java or formcalc will be fine.  Thanks for any help.

    Hello

    FormCalc script might look like:

    if (Age gt 17) then
         if (relationship eq "son" or relationship eq "daughter") then
              subform.presence = "visible"
         else
              subform.presence = "invisible"
         endif
    endif
    
  • Need help with the conditional statement.

    I have a conditional configuration to check the 6 values, and I don't know how to put in place.  I know that the way I have it Setup is wrong, but I'm stuck here after several attempts of other variants.

    LINE OF CODE IN QUESTION:

    If (eventObject.info.name! = "marker2" |) "marker4" | "marker5" | "marker6" | "marker7" | "marker8")

    FULL CODE BLOCK:

    var listenerObject:Object = new Object();
    listenerObject.cuePoint = {function(eventObject:Object):Void}
    If (eventObject.info.name! = "marker2" |) "marker4" | "marker5" | "marker6" | "marker7" | {"marker8")}
    var listenerObject:Object = new Object();
    listenerObject.stateChange = {function(eventObject:Object):Void}
    trace (_root.theVideo.State);
    If {(_root.theVideo.paused)
    mclContinue._visible = true;
    var myTween:TweenLite = new TweenLite (mclContinue,.35, {_alpha:100, ease:Back.easeOut, onComplete:contBtn});})
    } else {}
    var myTween:TweenLite = new TweenLite (mclContinue,.35, {_alpha:0, ease:Back.easeOut, onComplete:contBtn});})
    }
    }
    _root.theVideo.addEventListener ("stateChange", listenerObject)
    }
    }
    _root.theVideo.addEventListener ("cuePoint", listenerObject)


    Thank you.

    You are welcome.

Maybe you are looking for