Update using lead/rank

Goodmorning,

I have a little trouble trying to update using lead/rank.

Summary of the issue: My table contains duplicate data (only difference being 2 fields reason viewer_org & interest) and rather than the operation/processing multuple lines I want to assign the org of duplicate records Viewer and then I can hold 1 row, which has a list of org that can consult them.

I took a few of the fields of interest here: -.
create table copy_test 
(
 OWNER_ORG varchar(10),
 GEN_REC NUMBER(10),
 VIEWER_ORG varchar(10),
 INTEREST_REASON varchar(10),
 col_1 varchar(10),
 col_2 varchar(10),
 col_3 varchar(10),
 col_4 varchar(10)
);
Samples: -.
INSERT INTO COPY_TEST (OWNER_ORG ,GEN_REC ,VIEWER_ORG ,INTEREST_REASON ,COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('5AA' ,12345 ,'5AA' ,'6543' ,'' ,'' ,'' ,''  );
INSERT INTO COPY_TEST (OWNER_ORG ,GEN_REC ,VIEWER_ORG ,INTEREST_REASON ,COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('5AA' ,12345 ,'5BB' ,'5430' ,'' ,'' ,'' ,''  );
INSERT INTO COPY_TEST (OWNER_ORG ,GEN_REC ,VIEWER_ORG ,INTEREST_REASON ,COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('5BB' ,32165 ,'5CC' ,'430' ,'' ,'' ,'' ,''  );
INSERT INTO COPY_TEST (OWNER_ORG ,GEN_REC ,VIEWER_ORG ,INTEREST_REASON ,COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('5BB' ,32165 ,'5AA' ,'5430' ,'' ,'' ,'' ,''  );
INSERT INTO COPY_TEST (OWNER_ORG ,GEN_REC ,VIEWER_ORG ,INTEREST_REASON ,COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('5BB' ,32165 ,'5BB' ,'6543' ,'' ,'' ,'' ,''  );
INSERT INTO COPY_TEST (OWNER_ORG ,GEN_REC ,VIEWER_ORG ,INTEREST_REASON ,COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('YAA' ,98765 ,'5AA' ,'0' ,'' ,'' ,'' ,''  );
INSERT INTO COPY_TEST (OWNER_ORG ,GEN_REC ,VIEWER_ORG ,INTEREST_REASON ,COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('YAA' ,98765 ,'5BB' ,'543' ,'' ,'' ,'' ,''  );
Data looks like this: -.
 select * from copy_test;

OWNER_ORG     GEN_REC VIEWER_ORG INTEREST_R COL_1      COL_2      COL_3      COL_4
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
5AA             12345 5AA        6543
5AA             12345 5BB        5430
5BB             32165 5CC        430
5BB             32165 5AA        5430
5BB             32165 5BB        6543
YAA             98765 5AA        0
YAA             98765 5BB        543
Essential, we have 3 examples above (claim on gen_rec). The 1st example 5AA owner is a record that the Organization 5AA and 5BB are allowed to see. That's why he existing twice, viewer_org 5AA on 1 row and 5BB on the other. Then I need to assign these two organizations against one of the lines. I stated thise who strives to identify (a little): -.
SET LINESIZE 250;

select GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG,VIEWER_ORG CL_1,
LEAD (VIEWER_ORG,1,0) OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) as CL_2,
LEAD (VIEWER_ORG,2,0) OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) as CL_3,
LEAD (VIEWER_ORG,3,0) OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) as CL_4,
RANK() OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) rank
from COPY_TEST 
order by GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG;
Gives these results:--
   GEN_REC VIEWER_ORG INTEREST_R OWNER_ORG  CL_1       CL_2       CL_3       CL_4             RANK
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
     12345 5AA        6543       5AA        5AA        5BB        0          0                   1
     12345 5BB        5430       5AA        5BB        0          0          0                   2
     32165 5AA        5430       5BB        5AA        5BB        5CC        0                   1
     32165 5BB        6543       5BB        5BB        5CC        0          0                   2
     32165 5CC        430        5BB        5CC        0          0          0                   3
     98765 5AA        0          YAA        5AA        5BB        0          0                   1
     98765 5BB        543        YAA        5BB        0          0          0                   2
This is the result, I need: -.
GEN_REC     VIEWER_ORG     INTEREST_R     OWNER_ORG     CL_1     CL_2     CL_3     CL_4
12345     5AA     6543     5AA     5AA     5BB          
12345     5BB     5430     5AA                    
32165     5AA     5430     5BB                    
32165     5BB     6543     5BB     5BB     5AA     5CC     
32165     5CC     430     5BB                    
98765     5AA     0     YAA                    
98765     5BB     543     YAA     5AA     5BB          
I need the information in the viewer_org field to be placed in depeneding CL_1 CL_2, CL_3 or CL_04 the number of duplicate rows it y a. (is never more than 4) the hierarchy of what line I want to update would be pitched what was the Interest_reason above, even if this isn't a body of numbers It may be that '0' is important.


Any ideas guys?

One way would be to use Polish CASE in SELECT

with t_data as
(
SELECT
     GEN_REC ,
     VIEWER_ORG ,
     INTEREST_REASON,
     MAX(INTEREST_REASON) OVER (PARTITION BY GEN_REC) as max_int_reason,
     OWNER_ORG,
     VIEWER_ORG CL_1,
     LEAD (VIEWER_ORG,1,0) OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) AS CL_2,
     LEAD (VIEWER_ORG,2,0) OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) AS CL_3,
     LEAD (VIEWER_ORG,3,0) OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) AS CL_4,
     RANK() OVER (PARTITION BY GEN_REC ORDER BY GEN_REC ,VIEWER_ORG ,INTEREST_REASON,OWNER_ORG) rank
FROM
     COPY_TEST
ORDER BY
     GEN_REC ,
     VIEWER_ORG ,
     INTEREST_REASON,
     OWNER_ORG
)
SELECT
     t1.GEN_REC,
     VIEWER_ORG ,
     INTEREST_REASON,
     CASE
          WHEN INTEREST_REASON = max_int_reason
          THEN
          (SELECT
               decode(cl_1,'0',null,cl_1)
          FROM
               t_data t2
          WHERE
               rank          = 1
          AND t1.gen_rec = t2.gen_rec)
          ELSE
          null
     END cl_1,
     CASE
          WHEN INTEREST_REASON = max_int_reason
          THEN
          (SELECT
               decode(cl_2,'0',null,cl_2)
          FROM
               t_data t2
          WHERE
               rank          = 1
          AND t1.gen_rec = t2.gen_rec)
          ELSE
          null
     END cl_2,
     CASE
          WHEN INTEREST_REASON = max_int_reason
          THEN
          (SELECT
               decode(cl_3,'0',null,cl_3)
          FROM
               t_data t2
          WHERE
               rank          = 1
          AND t1.gen_rec = t2.gen_rec)
          ELSE
          null
     END cl_3,
     CASE
          WHEN INTEREST_REASON = max_int_reason
          THEN
          (SELECT
               decode(cl_4,'0',null,cl_4)
          FROM
               t_data t2
          WHERE
               rank          = 1
          AND t1.gen_rec = t2.gen_rec)
          ELSE
          null
     END cl_4
FROM
     t_data t1 ;

GEN_REC                VIEWER_ORG INTEREST_REASON CL_1       CL_2       CL_3       CL_4
---------------------- ---------- --------------- ---------- ---------- ---------- ----------
12345                  5AA        6543            5AA        5BB
12345                  5BB        5430
32165                  5AA        5430
32165                  5BB        6543            5AA        5BB        5CC
32165                  5CC        430
98765                  5AA        0
98765                  5BB        543             5AA        5BB                              

 7 rows selected 

Tags: Database

Similar Questions

  • I have a problem with my keyboard after the update using drivercanner

    Original title: my keyboard stopped working after the last updae

    OTEVA here

    Hi I have a problem with my keyboard after update using drivercanner I need help fixing this cause I tried looking for the installation of the upate that cause, but I don't have where to look.

    try a system restore to a date preceding the update.

  • Windows Mail is not refreshing since 09/02/2012. When I try to update using the F5 key, I get the error messages.

    Windows Mail is not refreshing since 09/02/2012.  When I try to update using the F5 key, I get the error messages.  The only way I can see me new emails is to sign on the web page of my Internet service provider, which I don't like.

    original title: windows mail is NOT refreshing

    It would be useful that Microsoft has put a notice to have several products access our email accounts so that us non-technical people could avoid these problems.

    Maybe it's something that Apple should warn the consumer. I have three e-mail accounts and I receive and send all the accounts on three different computers at the same time and have never had this problem. Your case is the first.

  • ILÇE a7 firmware update using mac 10.11.1

    Enyone installed upgrading firmaware ILÇE v. a7 1.2 to 2.0 using Mac OS 10.11.1 v.. El Capitan? So far I've seen only updates using Mac OS 10.10 Yosemite.

    Thanks for a quick response

    Jiriep

    Hi Jiriep,

    Welcome to the community of Sony!

    From now to the ILCE7 firmware update is available for Mac OS v 7 - 10.10. I recommend using a computer that meets these BONES to update your camera. You can download the firmware here.

    For more assistance, we recommend that you visit our Sony Global Web site for more information on contacting Sony's Support Center in your area at http://www.sony.net/SonyInfo/Support/.

    If my post answered your question, please mark it as "accept as a Solution. Thanks_Mitch

  • by selecting each of the nth order key without using a rank value

    Hello

    Suppose we have a table of Orders

    create table orders)

    order_no number primary key,

    customer_id number,

    item_id number);

    insert into orders)

    Select 101, 601, 90 of all the double union

    Select 103, 602, 100 of all the double union

    Select 603, 105, 110 Union double all the

    Select 107, 604, 120 Union double all the

    Select double union all 108, 605, 130

    Select double union all 109, 606, 140

    Select 110, 607, 150 Union double all the

    Select 111, 608, 160 Union double all the

    Select 112, 609, 170 Union double all the

    Select 114, 610, 180 doubles);

    How can we take every third order_no controlled key order_no without use of rank and connect function using sql.

    as

    105,

    109,

    112

    can someone please help?

    Thank you

    Max

    How can we choose each third order_no order_no keys without the use of grade -controlled

    Reason is?

    Anyway, using ROWNUM then:

    SQL > select order_no

    2 starting at)

    3. Select order_no, rownum rn

    4 of)

    5. Select order_no

    6 orders

    7 order of order_no

    8    )

    9)

    10 where mod (rn, 3) = 0;

    ORDER_NO

    ----------

    105

    109

    112

  • I have just reactivated Dreamweaver on a new computer.  How can I get the files FROM my website ON my computer so I can edit/update using Dreamweaver.

    I have just reactivated Dreamweaver on a new computer.  How can I get the files FROM my website ON my computer so I can edit/update using Dreamweaver?

    If your old machine still works, export your definition of original site to a portable player, copy in your new machine and then import the site definition to the new machine. The definition file will have the extension .ste

    Create an empty folder on your new machine.

    Define a 'new' web site on your new machine to point to the new local root folder.

    Log your server and download all of the subfolders and files in the folder root from the server to the new local root folder.

  • Where can I get the Adobe Camera RAW Nikon D7100 update using Bridge CS5?

    Where can I get the Adobe Camera RAW Nikon D7100 update using Bridge CS5?

    Currently using:

    Adobe Bridge CS5 v. 4.1.0.54

    Adobe Camera RAW c. 6.7.0.339

    Thank you!

    The D7100 was added to the list of devices supported in Camera Raw version 7.4, which is compatible with Photoshop CS6. There is no Camera Raw plugin for Photoshop CS5 that will support this camera. You will need to choose another software, or use the free DNG Converter to create negative digital copies of your NEF files. Then you can open these files DNG using Camera Raw, you have now.

  • Can I use LEAD or TROLLING with groups?

    Can I use LEAD or TROLLING with groups?
    I want to compare two records in each group.

    Is this possible?

    Thanks in advance

    Xavi says:
    Can I use LEAD or TROLLING with groups?
    I want to compare two records in each group.

    What do you mean by groups?

    Twinkle

  • Help: SQL for the massive update (service lead)

    Hello

    I use Oracle11.2. I want to update the column of hist_dttm of tb_hist with the tb_base crt_dttm column value in the following line, which is ordered by crt_dttm. Here are the details:

    creat table tb_base (tid varchar2 (32), crt_dttm timestamp (6));
    creat table tb_hist (tid varchar2 (32), crt_dttm timestamp (6), upd_dttm timestamp (6));

    insert into tb_base values ("AAA", to_timestamp (12 January 05 14:00:00.123456', ' dd-mm-rr hh24:mi:ss.ff'));))
    insert into tb_base values ("AAA", to_timestamp (May 12 05 14:00:00.123456', ' dd-mm-rr hh24:mi:ss.ff'));))
    insert into tb_base values ("AAA", to_timestamp (16 December 05 14:00:00.123456', ' dd-mm-rr hh24:mi:ss.ff'));))

    insert into tb_base values ("AAA", to_timestamp (12 January 05 14:00:00.123456', 'dd-mm-rr hh24:mi:ss.ff'), null);
    insert into tb_base values ("AAA", to_timestamp (May 12 05 14:00:00.123456', 'dd-mm-rr hh24:mi:ss.ff'), null);

    I use the following SQL to update the column upd_dttm of the tb_hist, I'm waiting for the upd_dttm needs to be updated to "12/05/05 14:00:00.123456 ' and 14:00:00.123456 16 December 05. However they is NULL. Any suggestion?



    Update tb_hist hist
    Set (upd_dttm) = (select lead (base. CRT_DTTM over (Partition by order of base.tid of base.crt_dttm)
    tb_base base
    where base.tid = hist.tid
    and base.crt_dttm = hist.crt_dttm)
    ;
     update tb_hist
      set crt_dttm= (select min (crt_dttm)
           from tb_base
         where tb_base.crt_dttm > tb_hist.crt_dttm)
    

    Published by: DPT Opitz Consulting com on 09.10.2012 19:24

    Published by: DPT Opitz Consulting com on 09.10.2012 19:25

  • update using case

    update TRG
    set
    C2=(using Case only 
    Where 
    ...
    
    
    
    1).FOR C2 when TRG.K=SRC.K  then SRC.B ELSE 0 
    
        a).TRG.K=SRC.K (Only one row for id = 300 then SRC.B)
        b).TRG.K=SRC.K & TRG.K!=SRC.K (two rows row for id = 100 then 0 for first row when TRG.K=SRC.K then 0 else SRC.B)
    
    
    Example:
    
    TRG 
    
    K       Id                      C2    
    
    201     100                3
    202     100               4
    
    201     300                 5   
    
    
    Expected:
    
    TRG
    
    SRC.B   K       Id                      C2    
    
    30     201     100                0
    30     202     100               30
    
    42     201     300                 42  

    It looks like, I was lucky to guess :), but this time I honestly don't know what you're after :(
    If there is more than 2 ranks cnt > 1 and and rn = 1 will assign 0 to line 1 and b for the rest of the lines
    Another assumption - you might be after
    ranks cnt > 1 and and rn <> would assign 0 to all lines but the last (numbered cnt) and b on the last line has already been mentioned
    There is another possible change: row_number() over (partition by order of identification by k desc) rn, i.e. reverse the order of numbering
    Please provide the desired result

    rn cnt c2 c2 c2
    
    1   4   0  0  ?
    2   4   b  0  ?
    3   4   b  0  ?
    4   4   b  b  ?
            |  |
            |  cnt > 1 and rn < cnt
            |
            cnt > 1 and rn = 1
    

    Concerning

    Etbin

  • Apple updates using bad apple id

    Trying to apply updates on my iPhone of Apple 6 he asked access code for my Apple ID girls not mine. I checked icloud and the framework general and everything seems ok and put my Apple ID but if I try to apply updates it asks password for different apple id. That is to say my girls. Any ideas?

    My hypothesis is that the applications must have been purchased with Apple ID your daughter purchases are always related to the ID that was used to make the purchase.

  • Problem with smart updates using the WiFi, modem OK

    I started having problems with my WiFi on my Toshiba Satellite laptop.

    I can not access any program Smart Update - Windows, Norton, Spydoctor, inheritance etc..

    These Smart Update programs work very well if I'm connected to Dial-up but not wireless.

    I checked my Norton Firewall settings - doesn't seem to be an indication of a problem here.
    I disabled my firewall program - no change.
    I tried the drivers updated - no change.
    I uninstalled the other software spy/advertising programs and checked my registry - no change.

    My Internet is fine - I can always check email and browse websites - just the Smart Update function seems to be affected.

    I am running Vista and have updated and installed all the files system through my modem connection. I use Norton 360.

    Any suggestions?

    Concerning

    Lisa

    Hello

    The wireless internet connection seems to be ok if you can access the Web sites.
    So I think that the problem must be installed software or application.

    You use Norton 360?
    Although I know this software could affect the internet connection.
    See this Symantec support page:
    http://www.Symantec.com/Norton/support/selectproduct_ts.jsp

    There are some frequently asked questions about various issues with Vista, and Norton 360.

  • Compaq Presario: Card mother SR5050NX update using SP37378.exe

    Help, I need an exe file to return the BIOS on my Compaq Presario, model SR5050NX, product number RX897AA-ABA, build software 72NAv3PrA2 to the specification of the manufacturer. The BIOs vendor is Phoeniz, release date: 20/04/2007, version of BIOs 5.17 string.

    I was updating the CPU so that I can install Windows 7 and I went to the website of Hp on the model SR5050NX and SP37378.exe.Seems downloaded and installed to give me version 5.23, however, when I rebooted the computer, I had the 'launch Startup Repair' which automatically said that he could not fix the CPU. I then use F11, Recovery Manager, who said "Detected Root Cause, current installation failure" great answer? Then, I used my Hardware Diagnostic Tools diagnostics bootable CD for Vista backup and reinstall all the software without result.

    Seems that SP37378.exe screwed up my computer, especially when the computer has worked fine before using the exe file.

    Logically, you would have downloaded the sp37378.exe on a CD and then run it from there. That would be the best avenue.   In addition, WIN7 is not supported with drivers HP on the SR5050nx.  This decision was made four years after the origin of the product Vista OS in 2007.

  • Cannot find Windows Platform Update in Windows Update, used to be here, but cannot be installed

    I'm having a problem with the download of the update of the Windows platform. About two months ago the update appeared in Windows Update and I started the installation. However, my computer froze before the update was finished and had to be forced to close. I tried to install the update several times thereafter, but he kept stopping at 1%. I though I'd have another go at him today, but the update happened to Win. Update. It is not in the hidden no more updates.

    I use Vista 32 and have installed SP2.

    Does anyone have a solution? And please, I'm without a computer whizz and not a mother tongue English, so as short as possible will be much appreciated.

    You SHOULD have installed the free trial Norton, (b) download / run the Norton removal and then c installed Norton Internet Security BEFORE the free trial period had expired, not after. [And no, Norton Support won't tell you this!]

    This being said, cross your fingers and try the following:

    [You may want to print these instructions for reference offline. If a step said to reboot, reboot.]

    1. download the Norton Removal Tool, save it to your desktop: ftp://ftp.symantec.com/public/english_us_canada/removal_tools/Norton_Removal_Tool.exe

    2. close all open applications (that is, anything with an icon on the taskbar).

    3. After completing some you have a copy of your handy product key, uninstall all Norton software (including LiveUpdate and everything Norton Add ons) via Control Panel | Programs | Uninstall a program (or Control Panel |) Programs and features, if you are using the classic view).

    4 IMPORTANT! -Online activate Windows Firewall immediately after the restart.

    NB: Now you're "working without a net": no to ANYTHING else online (e.g., navigation, reading e-mail, chat) until you have completed step #7 below!

    5. right click on the file that you saved in step #1 above, and then select run as administrator.  DO TAP not your keyboard until the race ends, then restart.

    6. do a clean install of Norton Internet Security 2011 (using your product ID, if need be) & reboot. CF. http://www.symantec.com/norton/support/kb/web_view.jsp?wv_type=public_web&docurl=20080514162318EN

    NOTE: Do NOT activate or install additional modules of Norton (for example, Norton Anti-Phishing) immediately.

    7. manually and repeatedly run LiveUpdate (Norton) until you get a prompt "no update more".

    8. open Internet Explorer (only) to http://support.microsoft.com/kb/923737 & run the difficulty.

    9. open Internet Explorer (only) to http://support.microsoft.com/kb/971058 & run that set it by DEFAULT and modes and AGGRESSIVE. [1]

    10. restart a last time & test.

    NOTE: The platform update (KB971644) is ONLY available through Windows Update.

    ~~~~~~~~~~~~~~~~~~~~~~~~
    [1] full Disclosure: the difficulty operating in AGGRESSIVE mode will remove your update history but not the list of installed updates.

    ~ Robear Dyer (PA Bear) ~ MS MVP (that is to say, mail, security, Windows & Update Services) since 2002 ~ WARNING: MS MVPs represent or work for Microsoft

  • Install multiple windows updates using wusa under Windows 7

    I am using a script to make several updates Windows quiet (*.msu) for Windows 7 by using the wusa.
    I have a script that does this for Vista, but the version of Windows 7 to wusa seems to need a different syntax.

    It installs the first update OK, but it then displays a window showing the correct syntax, seeming to indicate that he does not understand the syntax of the command.

    Problem solved!

    It now appears that the problem was in the preparation of the updated files (*.msu).

    The script necessary to have the name of the shortcut file.

    Thanks to all for their comments.

Maybe you are looking for