Need help in query..for a select / update on multiple rows

DROP TABLE TEST;
CREATE TABLE TEST (ACCOUNT_NUMBER VARCHAR2(30), TIER_NUMBER NUMBER, FROM_DATE DATE, TO_DATE DATE)
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 1, '28-FEB-11','28-FEB-11');
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 2, '28-FEB-11','28-FEB-11');
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 3, '28-FEB-11','01-MAR-11');
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 1, '01-MAR-11','01-MAR-11');
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 2, '01-MAR-11','01-MAR-11');
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 1, '02-MAR-11',NULL);
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 2, '02-MAR-11',NULL);
INSERT INTO TEST(ACCOUNT_NUMBER, TIER_NUMBER, FROM_DATE, TO_DATE) VALUES ('11111', 3, '02-MAR-11',NULL);
in this table, for each FROM_DATE, the TO_DATE value should be same for all tiers.
In the above data, for FROM_DATE = 28-Feb-11, there are 3 rows with TIER_NUMBER = 1, 2, 3.
TIER_NUMBER = 1,2 --> TO_DATE = 28-FEB-11
But for TIER_NUMBER = 3 --> TO_DATE = 01-MAR-11 (this occurred due to some incorrect logic).
Any help on how to identify such records with wrong data?
And how to update the TIER_NUMBER=3 row with TO_DATE = 28-FEB-11 (whatever we have in TO_DATE for TIER_NUMBER = 1 as that is the minimum tier)?
The table would contain millions of accounts and atleast 6 rows for each account.
Thanks.

Hi,
I think this should solve your problem
Declare
todate date;
differenc number;
      CURSOR myCursor IS
       select to_date, FIRST_VALUE(TO_DATE) over
(partition by account_number, from_date order by tier_number) - to_date as differ from test
        FOR UPDATE of to_date;
   BEGIN
     FOR idx IN myCursor LOOP
       todate :=idx.to_date;
        differenc := idx.differ;
        UPDATE test
        SET to_date = todate + differenc
      WHERE CURRENT OF myCursor;
     END LOOP;
     COMMIT;
   END;
   /It both finds and updates rows that doesn't have correct to_date value

Similar Messages

  • Need help with query for converting columns to rows

    Hello,
    I know this is a very common question asked in the forum. I have searched regading this, i did find some threads, but i was not able to achieve what i require from the answers posted. So anybody please help me.
    I have a table which is having multiple columns as follows:
    Insert into table_1 (X,Y,Z,A,B,C,D,E,F,G,H,I) values (0,0,2,0,0,1,3,0,0,0,0,0);I want to convert the result into a two column, multiple rows i.e., I want the result as follows:
    Col1 Col2
    X      0
    Y     0
    Z      2
    A     0
    B     0
    C     1
    D     3
    E     0
    F     0
    G     0
    H     0
    I      0Please anybody help me in writing the query for this..

    Is this what you are expecting:
    SQL> WITH T AS
      2  (
      3  SELECT 0 X, 0 Y, 2 Z, 0 A, 0 B, 1 C, 3 D, 0 E, 0 F, 0 G, 0 H, 0 I FROM DUAL
      4  )
      5  SELECT  'X' col1, X col2 FROM T
      6  UNION ALL
      7  SELECT  'Y' col1, Y col2 FROM T
      8  UNION ALL
      9  SELECT  'Z' col1, Z col2 FROM T
    10  UNION ALL
    11  SELECT  'A' col1, A col2 FROM T
    12  UNION ALL
    13  SELECT  'B' col1, B col2 FROM T
    14  UNION ALL
    15  SELECT  'C' col1, C col2 FROM T
    16  UNION ALL
    17  SELECT  'D' col1, D col2 FROM T
    18  UNION ALL
    19  SELECT  'E' col1, E col2 FROM T
    20  UNION ALL
    21  SELECT  'F' col1, F col2 FROM T
    22  UNION ALL
    23  SELECT  'G' col1, G col2 FROM T
    24  UNION ALL
    25  SELECT  'H' col1, H col2 FROM T
    26  UNION ALL
    27  SELECT  'I' col1, I col2 FROM T
    28  /
    C       COL2                                                                   
    X          0                                                                   
    Y          0                                                                   
    Z          2                                                                   
    A          0                                                                   
    B          0                                                                   
    C          1                                                                   
    D          3                                                                   
    E          0                                                                   
    F          0                                                                   
    G          0                                                                   
    H          0                                                                   
    C       COL2                                                                   
    I          0                                                                   
    12 rows selected.

  • Need help in optimisation for a select query on a large table

    Hi Gurus
    Please help in optimising the code. It takes 1 hr for 3-4000 records. Its very slow.
    My Select is reading from a table which contains 10 Million records.
    I am writing the select on large table and Retrieving the values from large tables by comparing my table which has 3-4 k records.
    I am pasting the code. please help
    Data: wa_i_tab1 type tys_tg_1 .
    DATA: i_tab TYPE STANDARD TABLE OF tys_tg_1.
    Data : wa_result_pkg type tys_tg_1,
    wa_result_pkg1 type tys_tg_1.
    SELECT /BIC/ZSETLRUN AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1 from
    /BIC/PZREB_SDAT *******************THIS TABLE CONTAINS 10 MILLION RECORDS
    into CORRESPONDING FIELDS OF table i_tab
    FOR ALL ENTRIES IN RESULT_PACKAGE***************CONTAINS 3000-4000 RECORDS
    where
    /bic/ZREB_SDAT = RESULT_PACKAGE-/BIC/ZREB_SDAT
    AND
    AGREEMENT = RESULT_PACKAGE-AGREEMENT
    AND /BIC/ZLITEM1 = RESULT_PACKAGE-/BIC/ZLITEM1.
    sort RESULT_PACKAGE by AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1.
    sort i_tab by AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1.
    loop at RESULT_PACKAGE into wa_result_pkg.
    read TABLE i_tab INTO wa_i_tab1 with key
    /BIC/ZREB_SDAT =
    wa_result_pkg-/BIC/ZREB_SDAT
    AGREEMENT = wa_result_pkg-AGREEMENT
    /BIC/ZLITEM1 = wa_result_pkg-/BIC/ZLITEM1.
    IF SY-SUBRC = 0.
    move wa_i_tab1-/BIC/ZSETLRUN to
    wa_result_pkg-/BIC/ZSETLRUN.
    wa_result_pkg1-/BIC/ZSETLRUN = wa_result_pkg-/BIC/ZSETLRUN.
    modify RESULT_PACKAGE from wa_result_pkg1
    TRANSPORTING /BIC/ZSETLRUN.
    ENDIF.
    CLEAR: wa_i_tab1,wa_result_pkg1,wa_result_pkg.
    endloop.

    Hi,
    1) RESULT_PACKAGE internal table contains any duplicate records or not bassed on the where condotion like below
    2) Remove the into CORRESPONDING FIELDS OF table instead of that into table use.
    refer the below code is
    RESULT_PACKAGE1[] = RESULT_PACKAGE[].
    sort RESULT_PACKAGE1 by /BIC/ZREB_SDAT AGREEMENT /BIC/ZLITEM1.
    delete adjustant duplicate form RESULT_PACKAGE1 comparing /BIC/ZREB_SDAT AGREEMENT /BIC/ZLITEM1.
    SELECT /BIC/ZSETLRUN AGREEMENT /BIC/ZREB_SDAT /BIC/ZLITEM1
    from /BIC/PZREB_SDAT
    into table i_tab
    FOR ALL ENTRIES IN RESULT_PACKAGE1
    where
    /bic/ZREB_SDAT = RESULT_PACKAGE1-/BIC/ZREB_SDAT
    AND
    AGREEMENT = RESULT_PACKAGE1-AGREEMENT
    AND /BIC/ZLITEM1 = RESULT_PACKAGE1-/BIC/ZLITEM1.
    and one more thing your getting 10 million records so use package size in you select query.
    Refer the following link also For All Entry for 1 Million Records
    Regards,
    Dhina..
    Edited by: Dhina DMD on Sep 15, 2011 7:17 AM

  • Need help with query for OITW

    I want to query a list that gives me all OnHand quantities for the Warehouses in one line per item:
    Item, Qty Whs1, Qty Whs2 ...
    Item1, Qty1_1, Qty1_2 ...
    Item2, Qty2_1, Qty2_2 ...
    Could somebody put me in the right direction?
    Thanks
    Franz

    Try this:
    SELECT P.Itemcode, T.ItemName,
    (Select U_NF_LagPlatz1 from OITW where WhsCode = 'NFE-1' and itemcode = T.Itemcode),
    (Select U_NF_LagPlatz2 from OITW where WhsCode = 'NFE-1' and itemcode = T.Itemcode),
        [NFE-1] AS Q1, [2] AS Q2, [3] AS Q3, [4] AS Q4,
        [5] AS Q5, [6] AS Q6, [7] AS Q7, [8] AS Q8,
        [9] AS Q9, [10] AS Q10
    FROM dbo.OITM T
    INNER JOIN
    (select Itemcode,OnHand, Whscode from oitw) s
    PIVOT
        sum(onhand)
        FOR whscode IN ([NFE-1],[2],[3],[4],[5],[6],[7],[8],[9],[10])
    ) p ON P.ItemCode = T.ItemCode
    ORDER BY P.[Itemcode]
    Regards,
    Bala
    Edited by: Balakumar Viswa on Jul 26, 2010 3:06 PM

  • Need help in query for datediff

    Hi,
    I need datediff in hh:mi:ss format.
    How to get it.
    SELECT
    TO_DATE('2/16/2007 1:05:46 PM', 'MM/DD/YYYY HH12:MI:SS AM')-
    TO_DATE('2/13/2007 1:23:07 PM', 'MM/DD/YYYY HH12:MI:SS AM')
    FROM DUAL
    Thanks
    Amit

    Something like:
    SQL> l
      1  SELECT (EXTRACT(DAY FROM NUMTODSINTERVAL(last_dt - first_dt ,'day')) *24) +
      2         EXTRACT(HOUR FROM NUMTODSINTERVAL(last_dt - first_dt ,'day')) hours,
      3         EXTRACT(MINUTE FROM NUMTODSINTERVAL(last_dt - first_dt ,'day')) minutes,
      4         EXTRACT(SECOND FROM NUMTODSINTERVAL(last_dt - first_dt ,'day')) seconds
      5  FROM (SELECT TO_DATE('2/13/2007 1:23:07 PM', 'MM/DD/YYYY HH12:MI:SS AM') first_dt,
      6               TO_DATE('2/16/2007 1:05:46 PM', 'MM/DD/YYYY HH12:MI:SS AM') last_dt
      7*       FROM dual)
    SQL> /
         HOURS    MINUTES    SECONDS
            71         42         39You should be able to figure out how to concatenate this together to get whatver display format you need.
    John

  • Need help with query that uses 'SELECT INTO'

    Hi guys,
    I am trying to duplicate the values of a table to another by using the script below:
    ACCEPT TBSNAME           CHAR PROMPT 'Tablespace name>'
    PROMPT Connect As System
    Connect system
    CREATE TABLE FREESPACE
         TABLESPACE_NAME          VARCHAR(20)          NOT NULL,
              CONSTRAINT FREESPACE_PKEY PRIMARY KEY(TABLESPACE_NAME),
         TOTAL_FREE_STORAGE     NUMBER(10)          NOT NULL
    ) TABLESPACE USERS;
    SELECT     TABLESPACE_NAME,
              SUM(BLOCKS) TOTBLOCKS
    INTO      FREESPACE
    FROM      SYS.DBA_FREE_SPACE
    WHERE      TABLESPACE_NAME = UPPER('&TBSNAME')
    GROUP BY TABLESPACE_NAME;
    However, when I execute this script, I was prompted with the 'missing keyword' error which happens at the third line of the select statment. Any idea what's missing here?
    Thanks in advance.

    If you mean "fill up the table FREESPACE" by using "select...into..." then it is totally wrong. You cannot do it in plsql also.
    Use this:
    insert into freespace(tablespace_name,total_free_storage)
    SELECT TABLESPACE_NAME,SUM(BLOCKS) TOTBLOCKS
    FROM SYS.DBA_FREE_SPACE
    WHERE TABLESPACE_NAME = UPPER('&TBSNAME')
    GROUP BY TABLESPACE_NAME;
    Message was edited by:
    Yas

  • Need Help to query Lync Database for User Information

    Need Help to Query the lync database to retrieve below user information.
    1. SIP Address of the registered user
            2. Phone Number configured to the particular account.
            3. IP Address
           4. Last Logged in time.
    I am trying to pull the above information from rtc database for all the registered users. Please let me know if this is possible and it would be great if you can throw some light on what tables to look for the data. Thank You.

    Hi,
    For SIP address and Phone number you can check RTC database.
    IP Address:
    You can refer to the link below to query IP address: 
    http://h30499.www3.hp.com/t5/Business-Service-Management-BAC/Monitoring-Lync-with-the-User-Registrations-Viewer-Free-NMC-tool/ba-p/5961497#.UtOU43mIrwo
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    Please make sure that you completely understand the risk before retrieving any suggestions from the above link.
    Last Logged in time:
    You can refer to the link below:
    http://blogs.technet.com/b/dodeitte/archive/2011/05/11/how-to-get-the-last-time-a-user-registered-with-a-front-end.aspx
    Best Regards,
    Eason Huang
    Eason Huang
    TechNet Community Support

  • I need help! i am trying to update iphoto on my macbook pro that i had bought from someone else. although i am signed into my account when trying to update it is asking me for the previous owners account details and it wont let me change it to my details

    i need help! i am trying to update iphoto on my macbook pro that i had bought from someone else. although i am signed into my account when trying to update it is asking me for the previous owners account details and it wont let me change it to my details how can i solve this problem please.
    thank you
    Jan Robinson

    That's because the Mac, OS X and the iLife apps that came preinstalled on the Mac all belong to the previous owner. The previous owner needs to call Apple care and have their Apple ID disassociated from the Mac. Then the hard drive needs to be totally erased and OS X and the iLife apps reinstalled using OS X Recovery.

  • Can my iWeb '08 site be fixed,or do I need to just wait for an iWeb update?

    Ever since my "upgrade" to iWeb '08, the text in my webpages does not appear. I haven't changed any fonts or colors. I have had to put my website back to it's iWeb '06 version.
    Cyclosaurus mentioned that my "text box elements has attribute style.visibility set to hidden" and that I might be able to correct it if I were javascript saavy, I should be able to turn visibility attribute to visible with a few lines of javascript.
    How do I do this? Does anyone know any quick javascript that I could cut-and-paste, maybe within an iWeb HTML Widget, that could counteract the "attribute style.visibility set to hidden"? Perhaps I can do this until Apple fixes this Bug/Glitch in iWeb '08.
    Can my iWeb '08 site be fixed,or do I need to just wait for an iWeb update? Please help.
    Thanks.
    Wayne
    P.S. Also see the remarks under the discussions # 5663242 & # 1205196

    Starting with the release of Firefox 7, the previous versions of Firefox (4.0.x, 5.0.x, 6.0.x) are no longer maintained with Security and Stability updates. Firefox 3.6 is the last previous Firefox version that is still being maintained.
    '''''Warning:'' Firefox 3.6 will only be maintained with security and stability updates for a short period of time.'''
    If you want to Downgrade to Firefox 3.6, download Firefox 3.6 from:
    * http://www.mozilla.com/en-US/firefox/all-older.html
    -> [[Uninstalling Firefox]] - Uninstall your current Firefox
    * DO NOT SELECT TO REMOVE USER DATA AND SETTINGS
    * Restart your system
    -> [[Installing Firefox]]
    * Use the Downloaded & Saved Firefox 3.6 installer
    Check and tell if its working.

  • In a query, for the selection field 'KNB1-ZWELS' a match-code isn't availab

    Hi All,
    Creating a query, for the selection field 'KNB1-ZWELS' a match-code isn't available.
    Could anyone tell me the reason?
    Thanks
    Gandalf

    Hi,
    Match Code Object is not available for the field ZWELS. If required, You need to contact a Developer for creating one .

  • HT4623 i need help...did a FACEBOOK UPDATE on a iphone 3 and now facebook will not open. i realize the version os 4.3.1, but in settings their is no updates sections

    i need help i believe i did an update for facebook on a iphone 4.3.1, and now facebook will not open?

    You have updated the app to a version that is not compatible with your iphone.
    The iphone 3g can only go to 4.2.1
    You should check compatibiity before updating your apps.
    Sorry

  • Need a drop down for a selection screen

    HI all,
    I need a drop down for a selection screen.
    say for eg: Field is MARA-MTART   (Material Type). The selection is optional and the dropdown should be using check table: T134
    How to get a dropdown? I think a function module is used , but I dont remember the name.
    Kindly reply.
    Thanks in advance.
    Points will be rewarded

    Hi,
    chk the below code...
    TYPE-POOLS: vrm.
    PARAMETER: pr_spart TYPE spart AS LISTBOX VISIBLE LENGTH 7.
    *AT SELECTION-SCREEN ON VALUE-REQUEST FOR pr_spart.
    INITIALIZATION.
    *----Code for Drop down list....
      DATA: lit_spart_list  TYPE vrm_values, "Table for list of Divisions
              lwa_spart_list TYPE vrm_value,
              lw_name  TYPE vrm_id.            "Name of parameter with list-box.
    *---Assign selected values to table that would be passed to FM VRM_SET_VALUES
      CLEAR : lwa_spart_list,
              lw_name.
    *---Pass required values for list-box display.
      lwa_spart_list-key = 'H'.
      lwa_spart_list-text = 'HL'.
      APPEND lwa_spart_list TO lit_spart_list.
      lwa_spart_list-key = 'S'.
      lwa_spart_list-text = 'SL'.
      APPEND lwa_spart_list TO lit_spart_list.
      lwa_spart_list-key = 'P'.
      lwa_spart_list-text = 'PP'.
      APPEND lwa_spart_list TO lit_spart_list.
      lwa_spart_list-key = 'F'.
      lwa_spart_list-text = 'FR'.
      APPEND lwa_spart_list TO lit_spart_list.
    *Name of parameter to which list is to be assigned
      lw_name = 'PR_SPART'.
      CALL FUNCTION 'VRM_SET_VALUES'
        EXPORTING
          id              = lw_name
          values          = lit_spart_list
        EXCEPTIONS
          id_illegal_name = 1
          OTHERS          = 2.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
    Reagrds,
    nagaraj

  • I need help in making the programe which updates our code into client machi

    I need help in making the programme which updates our code into client machine from the server.
    As yahoo messanger does if there is any updated version of yahoo messanger.
    It asks when we login into it do you want to update new version of yahoo messanger
    It automatically updates it
    Need Help??

    Sounds to me like you need Java Web Start.

  • HT1338 Need help locating where and how to update Mac OS-X 10.6.5 to the latest Mountain Lion Software...thanx John

    I need help locating where and how to update Mac OS-X to Mountain Lion.....Thanx....Jay

    First update your 10.6 version to 10.6.8 from the software update under the Apple Menu.
    This will add direct access to the Mac App store via a new application.
    Now launch the App Store from your Applications folder - Its the new icon a letter A formed from a ruler pencil and pen on a blue circle !
    Once launched you need to add your iTunes account details or create an account add payment details etc...
    Sign in purchase download and follow install processes to upgrade to 10.8 Mountain Lion.
    OH and to be safe BEFORE you install backup your current system to an external drive !

  • I need help choosing RAM for MSI PM8PM-V 7222 VER. 2.0 MB

    hey everyone as you know my MB is in the subject and i need help buying ram for it. on the MSI website it says it can surpport max. 2GB so 1GB in each slot? it only has 2 slots also what DDR and MHZ clock, DRAM fequency etc does the ram have to be and both single sided or double sided or 1 single and 1 double?
    thanks

    Quote
    also what DDR and MHZ clock, DRAM fequency etc does the ram have to be
    http://global.msi.eu/index.php?func=proddesc&maincat_no=1&prod_no=1039
    Quote
    • Supports DDR2 533/400 memory interface.
    Check the memory support list for best compatibility:
    http://global.msi.eu/uploads/test_report/TR10_1039.pdf

Maybe you are looking for

  • Tax Report and Tax Reconcilation Report

    Can anyone tell me what is the difference between Tax Report and Tax Reconcilation Report? What is Tax Reconcilation Report supposed to do?

  • Errno 88 - can't start up

    GLah! i can't manage to start up my computer. The other day, i woke up my computer from sleep. Mail wasn't responding properly, so i decided to quit and start it again. I quit, but when i clicked on the icon on the dock, it would only bounce once and

  • Why wifi is called half duplex

    In a tutorial video i have found that wiifi is called half duplex. why it is so called? Posted by WebUser Poristhan Prince from Cisco Support Community App

  • Problem with joining 3 tables

    Hi, The following is my join code and internal table declaration: BEGINOF/ENDOF INTERNAL TABLE DATA: BEGIN OF I_MARC_VALUES OCCURS 0,         WERKS LIKE MARC-WERKS,         DISPO LIKE MARC-DISPO,         DISMM LIKE MARC-DISMM,         ZZLIFNR LIKE MA

  • Macbook Pro Wireless Antenna Gain

    Does anyone know what the gain (in dB) of the factory wi-fi airport antenna in a macbook pro?  I think it may be a 3dB gain antenna, but im not sure.