one-to-many selfjoin, delete records with the same rank or a substitution

Sorry for my poor choice of the title of the discussion, feel free to suggest me a more relevant


I rewrote for clarity and as a result of the FAQ post.


Version of DB


I use Oracle10g Enterprise 10.2.0.1.0 64-bit


Tables involved

CREATE TABLE wrhwr (
wr_id INTEGER PRIMARY KEY,

eq_id VARCHAR2(50) NULL,
date_completed DATE NULL,
status VARCHAR2(20) NOT NULL,
pmp_id VARCHAR2(20) NOT NULL,
description VARCHAR2(20) NULL);


Examples of data


INSERT into wrhwr  VALUES (1,'MI-EXT-0001',date'2013-07-16','Com','VER-EXC','Revisione')
INSERT into wrhwr VALUES  (2,'MI-EXT-0001',date'2013-07-01','Com','VER-EXC','Verifica')
INSERT into wrhwr  VALUES (3,'MI-EXT-0001',date'2013-06-15','Com','VER-EXC','Revisione')
INSERT into wrhwr  VALUES (4,'MI-EXT-0001',date'2013-06-25','Com','VER-EXC','Verifica')
INSERT into wrhwr  VALUES (5,'MI-EXT-0001',date'2013-04-14','Com','VER-EXC','Revisione')
INSERT into wrhwr  VALUES (6,'MI-EXT-0001',date'2013-04-30','Com','VER-EXC','Verifica')
INSERT into wrhwr  VALUES (7,'MI-EXT-0001',date'2013-03-14','Com','VER-EXC','Collaudo')


Query used

SELECT *
  FROM (SELECT eq_id,
               date_completed,
               RANK ()
               OVER (PARTITION BY eq_id
                     ORDER BY date_completed DESC NULLS LAST)
                  rn
          FROM wrhwr
         WHERE     status != 'S'
               AND pmp_id LIKE 'VER-EX%'
               AND description LIKE '%Verifica%') table1,
       (SELECT eq_id,
               date_completed,       
               RANK ()
               OVER (PARTITION BY eq_id
                     ORDER BY date_completed DESC NULLS LAST)
                  rn
          FROM wrhwr
         WHERE     status != 'S'
               AND pmp_id LIKE 'VER-EX%'
               AND description LIKE '%Revisione%') table2,
       (SELECT eq_id,
               date_completed,            
               RANK ()
               OVER (PARTITION BY eq_id
                     ORDER BY date_completed DESC NULLS LAST)
                  rn
          FROM wrhwr
         WHERE     status != 'S'
               AND pmp_id LIKE 'VER-EX%'
               AND description LIKE '%Collaudo%') table3
 WHERE     table1.eq_id = table3.eq_id
       AND table2.eq_id = table3.eq_id
       AND table1.eq_id = table2.eq_id

The above query is intended to selfjoin wrhwr table 3 times in order to have for each line:

  • eq_id;
  • date of the completion of a verification type work request for this eq_id (aka table1);
  • date completion of a line (aka table2) type wr for this eq_id;
  • date of completion of a type wr Collaudo (aka table3) for this eq_id;

A separate eq_id:

  • can have different completion of many requests for work (wrhwr records) with dates or date of completion (date_completed NULL column).
  • in a date range can have all types of wrhwr ('verification', 'Line', 'Problem'), or some of them (e.g. audit, line but not Collaudo, Collaudo but not verification and line, etc.);
  • must not repeat the substrings in the description;
  • (eq_id, date_completed) are not unique but (eq_id, date_completed, description, pmp_id) must be unique;

Expected results

Using data from the example above, I expect this output:

eq_id, table1.date_completed, table2.date_completed, table3.date_completed

MI-ext-001,2013-07-01,2013-07-16,2013-03-14 <- to this eq_id table3 doesn't have 3 lines but only 1. I would like to repeat the value most in the rankings in table 3 for each line of output

MI-ext-001,2013-07-01,2013-06-15,2013-03-14 <-I don't want this line of table1 and table2 with both 3 lines match must be in terms of grade (1st, 1st) (2nd, 2nd) (3rd, 3rd)

MI-ext-001,2013-06-25,2013-06-15,2013-03-14 <-2nd table1 joined the 2nd row from table2

MI-ext-001,2013-04-30,2013-04-14, 2013-03-14 <-1 table1, table2 rank rank 1, 1st rank table3

In the syntax of vector style, tuple expected output should be:

IX = ranking of the i - th of tableX

(i1, i2, i3) IF EXISTS a rank i - th line in each table

ON THE OTHER

(i1, b, b)

where b is the first available lower ranking of the table2, or NULL if there isn't any line of lower rank.

Clues?

With the query, I am unable to delete the lines "spurius.

I think a solution based on analytical functions such as LAG() and LEAD(), using ROLLUP() or CUBE(), using nested queries, but I would find a solution elegant, simple, fast, and easy to maintain.


Thank you

Hello

Sorry, it's still not quite clear what you are asking.

This becomes the desired resutls of the sample data you posted:

WITH got_r_type AS

(

SELECT eq_id, date_completed

CASE

Description WHEN LIKE '% Collaudo %' THEN 'C '.

Description WHEN AS 'Line %' THEN 'R '.

Description WHEN AS 'Verification %' THEN 'V '.

END AS r_type

OF wrhwr

Situation WHERE! = s "

AND pmp_id LIKE '% WORM - EX'

)

got_r_num AS

(

SELECT eq_id, date_completed, r_type

, ROW_NUMBER () OVER (PARTITION BY eq_id, r_type)

ORDER BY date_completed DESC NULLS LAST

) AS r_num

OF got_r_type

WHERE r_type IS NOT NULL

)

SELECT eq_id

LAST_VALUE (MIN (CASE WHEN r_type THEN date_completed END = ' V')

IGNORES NULL VALUES

) OVER (PARTITION BY eq_id

ORDER BY r_num

) AS audit

LAST_VALUE (MIN (CASE WHEN r_type = 'R' THEN date_completed END)

IGNORES NULL VALUES

) OVER (PARTITION BY eq_id

ORDER BY r_num

) AS line

LAST_VALUE (MIN (CASE WHEN r_type = 'C' THEN date_completed END)

IGNORES NULL VALUES

) OVER (PARTITION BY eq_id

ORDER BY r_num

) AS collauda

OF got_r_num

GROUP BY eq_id

r_num

ORDER BY eq_id

r_num

;

I guess the description can have (at most) only substrings target, in other words, you can't have a line like this:

INSERT into (1,'MI-EXT-0001',date'2013-07-16','Com','VER-EXC','Revisione VALUES wrhwr and audit");

In addition, you said the combination (eq_id, date_comepleted) is not unique, that it is y no example of this in your sample data.  What results would you if, in addition to this line (who did the validation):

INSERT into wrhwr VALUES (7,'MI-EXT-0001',date'2013-03-14','Com','VER-EXC','Collaudo');

the following line is also?

INSERT into wrhwr VALUES (97,'MI-EXT-0001',date'2013-03-14','Com','VER-EXCFUBAR','Collaudo');

.

You could do a self-join instead of GROUP BY, but I suspect it will be much less effective.  You can use FULL OUTER JOIN, since you do not know what r_types was that r_nums.

Tags: Database

Similar Questions

  • is it possible to install CC on one of my other mac/PC with the same subscription?

    is it possible to install CC on one of my other mac/PC with the same subscription?

    Hello

    Thank you Lara, who was the answer that I wanted.

    Respect of

    Gustav

  • Can the records with the same pictures are in the Organizer add? I have to start? I tried to organize photos in folders in a time better. I have to start all over again?

    I made new files so I could have a better time line, but the Organizer will not let me load them. The new files contain the same images, but they are better organized.  To add pictures in the folders I have to delete those in the organizer and start again?  I feel like I got this backward little and should have organized their first. Help.

    virginiae56248392 wrote:

    I made new files so I could have a better time line, but the Organizer will not let me load them. The new files contain the same images, but they are better organized.  To add pictures in the folders I have to delete those in the organizer and start again?  I feel like I got this backward little and should have organized their first. Help.

    The key to understanding the difference between the Organization of your files and folders directly in your disk folders tree and by organizing with a catalogue, is that the catalogue:

    -contains no image files, that links to the actual location on the disk

    -stores the links in a database with organizers strict rules, which is to avoid duplicates.

    One of the highlights of the databases such as those in the elements or Lightroom is that they store duplicate data. Which avoids not only of space useless, but also ensures that you're updating a file and forget its duplicates. As a result, the Organizer will prevent you from 'import', which is to 'index' your files several times. It recognizes the files into two criteria: the size of the file in kilobytes and the "date" of the shooting.

    The other strength of the catalogue, it's that you can select the sort order of your files displayed: by date, name of the file, import annealing in Bell or same "custom" order in albums. This means you don't ever need to have your physical files and folders sorted by dates on your drive. Try to get a similar organisation to the date on your car in your catalog can be an unnecessary pain. Good if you choose a folder structure to date in office at the time of importation; There must be little order at this time anyway. Once your photos are cataloged, the rule is that if you want to change the structure of your file, you must do it from the Organizer itself (left panel of record). This means that you are working with already imported files (you can not import duplicates). You can organize in this folder on the same panel as if you were in Solution Explorer / finder. You can either use the 'move' menu or drag and drop of folders with their subfolders, you can move the files of images to other subfolders. Beware, there are traps in doing this, for example by trying to move a subfolder in master already a folder with the same name of subfolder...

    So, ask yourself if all this work is really worth. And be patient and prudent to do this task from the file Organizer Panel.

  • Mix records with the same name of the internal and external space (clip +)

    Hello

    I have a question regarding the reading of 'File': if having a folder on the inside and external space microsd, both are named "graphics". Is it possible to shuffel these 2 files with the same name? There are also a few other folders I want to be included, so I just couldn't use the "play all" option. I don't want to use replay by id3tag because mines are always screwed up and would be a lot of work to mark all the appropriate files in the.

    Thanks for help

    m.

    I've not played with playlists much.

    If ID3 tags are all screwed up, I would use the MP3tag utility to change the label of the kind in "Charts" on internal and external memory, then you access the kind-> just Charts and play at all.  It is probably not an option if your labels are blank and you don't want to ruin you, but it's really quick & easy to do with MP3tag.

    I think that even Windows Explorer (XP/Vista) can change the field type in the batch (select all 'charts' music files, click right properties, summary (Advanced) tab change the field of its kind in "Charts", OK).  I keep the ID3tags 'good' on my PC, but edit on my player according to the needs.

    Just another idea which is really easy if you're not afraid of screwing the ID3 tags.

  • When you use the batch, record with the same file name in the same folder.

    I therefore about 500 pictures that needs to be cropped. I created the cropaction and added a "backup" function and close in action, because I want the photos to be saved with the same file name in the same folder...

    Now I want that it take its course. But no, I have to press ok to confirm the backup on each photo.

    Someone knows how to fix this?

    BR

    Robert

    I solved it.

    I forgot to check the box "override save under ' :)

  • Photo Loader deletes files with the same file name

    A few days ago I imported a number of photos and videos from my iPhone to a specific folder on my MBA by using Photo Loader. Tonight, I imported again a number of photos and videos in this folder, only to find out they were not imported after all as the file names already exist. Apparently, iOS uses IMG_xxxx.xxx for all that is shot with the camera and always uses the next available number. So as I had already IMG_0618.JPG in this case, she has just imported this photo, but that the flag has been verified, he took the original iPhone itself.

    As they have not been removed by interaction on the iPhone, but by means of Photo Loader, they don't pop up under "recently retired" and the device with third-party scanning software also has been failed.

    I have recovered the photos via photostream but am always desperate to find one or two movies that I shot. Any thoughts?

    Never mind... For some strange reason, the photos have been transferred to the location of default images.

  • Multiple images with the same file name no doubt prevent export

    iMac running OS X El Capitan v10.11.6; Photos 1.5

    I'm trying to export approximately 331 images and I get an error report saying that only 169 of the exported images due to the inability to create files for 159 of the images.  Then the report gives me the names of files of the first 100 images for which files were not created.  After looking at many images that would not create a file, I realized that, in any case, there was at least one, if not several, other images with the SAME EXACT FILE name as the image in question.  I can't change the names of files by right click on "info".  I tried to export the images and change the names of files to export using the sequential option and the option of album name - always having exactly the same problem.  I even tried not to export an image at once and change the name of the file individually or no available. Is there a work around that? I desperately need to export those specific images. I am trying to create a photo album for shown to mothers who choose a family with which you want to place their child/ren for adoption. I'm at my wit's end.  The kicker is I'm moving towards trying to export all my pictures, and that's going to be a HUGE problem in course for me, so I really hope that there is a way around this question somewhat simply. I am a hobby photographer and use the computer for businesses and crafts.  I've seen a few posts that included answers referencing "AppleScript" - I have no idea how to do something like that.

    From here on I will DEFINITELY ensure that my Canon continually numbers the names of image files and automatically resets.  For the other photos other than Canon, is possible to rename images during the import of the lot?

    All advice and help will be GREATLY appreciated!

    Finder has the ability to rename files with various models of lot.  Simply select all the files to rename, right-click one of the selected files, choose 'Rename X points... '. ", and then set the options and click on the button"Rename ".

  • I have 2 bookmarks with the same name but different stuff in them. I want to remove one, but pass the contemts remaining bookmark

    I have 2 bookmarks with the same name but different stuff in them. I want to remove one, but pass the contemts remaining bookmark

    This is the first mention you made records. See this - https://support.mozilla.org/en-US/kb/Sorting%20bookmarks#w_rearranging-manually - and move individual bookmarks from one folder to the other folder. You may need to press the Alt key to display the Menu bar and the View menu item.

  • How to open multiple files with the same extension in one program from the Explorer

    I frequently receive several JPG (or whatever extension) files to my clients.  On Windows XP, I could just highlight the files I want to open, and then click Open.  They would be so open to 1 program cascading.  This made it easy when comparing many images.  If I do the same thing in Windows 7, it opens the default program several times and puts 1 image of each open program.  It is Paint Shop Pro in my case.  Thus, instead of so-called images 5 open cascading in Paint Shop Pro... the computer open Paint Shop Pro 5 times with 1 image of each open version of the program.  I tried this search like crazy, but cannot find the right thread for an answer.  Most of the threads are on the opening of the different file types.  My question is only about the opening of multiple files with the same exact file inside 1 free program extension.

    Is this the same version of Paint Shop?

    One thing you might try is to open Paint Shop, then select and drag all the files in this window of the paint shop.  Various programs to manage this type of action differently from the DDE, but it might give you the expected results.

  • What is wrong with this sequence, it does not work when, after 3 inserts, I add a new record with the trigger it gives an error.

    Mr President.

    What is wrong with this sequence, it does not work when, after 3 inserts, I add a new record with the trigger it gives an error.

    --SL_CUSTOMERS table data
    
    
    INSERT INTO SL_CUSTOMERS VALUES(1,'Kamrul Hasan',NULL,NULL,'Moghbazar', 'Dhaka','0456789123',NULL,NULL,NULL,'Y',NULL);
    INSERT INTO SL_CUSTOMERS VALUES(2,'Rabiul Alam',NULL,NULL,'Motijheel', 'Dhaka','0567891234',NULL,NULL,NULL,'Y',NULL);
    INSERT INTO SL_CUSTOMERS VALUES(3,'Shahed Hasan',NULL,NULL,'2-G/1,2-2,Mirpur', 'Dhaka','0678912345',NULL,NULL,NULL,'Y',NULL);
    
    
    
    

    CREATE SEQUENCE  "ALIZA"."SL_CUSTOMERS_SEQ"  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE NOORDER  NOCYCLE ;
    
    
    CREATE OR REPLACE TRIGGER SL_CUSTOMERS_TRG 
    BEFORE INSERT ON "ALIZA"."SL_CUSTOMERS"   
    FOR EACH ROW   
    BEGIN   
    IF :NEW.CUSTOMER_ID IS NULL OR :NEW.CUSTOMER_ID < 0 THEN  
      SELECT SL_CUSTOMERS_SEQ.nextval   
        INTO :NEW.CUSTOMER_ID  
        FROM DUAL;   
      END IF;   
    END;   
    /
    
    
    
    

    When I try to insert several records with the seq.nextval it gives error

    violation of primary key.

    INSERT INTO "ALIZA"."SL_CUSTOMERS" (CUSTOMER_NAME) VALUES ('sdfsd')
    ORA-00001: unique constraint (ALIZA.SL_CUSTOMERS_PK) violated
    ORA-06512: at line 1
    
    
    
    
    One error saving changes to table "ALIZA"."SL_CUSTOMERS":
    Row 4: ORA-00001: unique constraint (ALIZA.SL_CUSTOMERS_PK) violated
    ORA-06512: at line 1
    
    
    
    
    
    
    
    

    Concerning

    Mr President.

    I find the solution by creating a function before the triiger

    as below

    CREATE SEQUENCE  "ALIZA"."SL_CUSTOMERS_SEQ"  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE NOORDER  NOCYCLE ;
    
    CREATE OR REPLACE FUNCTION get_SL_CUSTOMERS_vId RETURN VARCHAR2 AS
    BEGIN
       RETURN SL_CUSTOMERS_SEQ.NEXTVAL;
    
    END;
    /
    
    CREATE OR REPLACE TRIGGER SL_CUSTOMERS_TRG
    BEFORE INSERT ON "ALIZA"."SL_CUSTOMERS"
    FOR EACH ROW
    DECLARE
    dummy VARCHAR2(200);
    BEGIN
      dummy := get_SL_CUSTOMERS_vId();
      :NEW.CUSTOMER_ID := dummy;
    END;
    /  
    

    It works very well

    Thank you all for the suggestions.

    Concerning

  • Ran Malwarebytes & two threats were each one with the same name: PUM. Disabled.SecurityCenter. fact that associated with MSSE PUM & the boxes unchecked in MS security Center.

    Original title: PUM. Disabled.SecurityCenter

    I recently removed "patch system" of my Dell computer XP. Malwarebyte s was the only software to do. MSSE caught, but couldn't fix it. I then updated & ran MSSE on my Toshiba - no threats not found. I then ran Malwarebytes & two threats were each one with the same name: PUM. Disabled.SecurityCenter both at the Date of the registry in HKLM\SOFTE\Microsfot\SecurityCenter\AntiVirusDisableNotify & HKLM\SOFTWARE\Microsfot\securityCenterFirewallDisableNotify.  These two have been listed bad: Good (0) (1).  In the Malwarebytes wesite, I read that this has something to do with MSSE. In a commentary, he says it occurs if, in the MS Security Center, the notification "Notify me if my computer might be at risk because of my virus protection software settings" is unchecked.  I then went ahead & check all 3, firewalls, viruses & automatic updates. My computer seems to work well, except that the fan runs all the time (there are a lot of processes running & the CPU usage is very--often 100%, but the computer works fast).

    My questions are: 1) the PUM associated MSSE & the boxes unchecked in MS security Center; (2) should I click on "ignore" in the Malwarebytes scan? 3) was right to check all the boxes in the center of security - "altert me if my computer may be at rist b/c of my xxx software settings?  Thanks in advance for your help.

    PC fan2

    Hello

    (1) is associated with MSSE PUM & the boxes unchecked in MS security Center;

    The following thread might answer this question: http://forums.malwarebytes.org/index.php?showtopic=69859

    (2) should I click on "ignore" in the Malwarebytes scan?

    Yes

    3) was right to check all the boxes in the center of security - "altert me if my computer may be at rist b/c of my xxx software settings?

    Only, run antivirus software at the same time. Firewall must be performed, evil-ware bytes can be run at your pleasure.

  • All with the same name and more many nested directories

    Ideas: when trying to install Window7 it cannot be installed because of a folder and its subfolder had the same name... called a duplicate. There is a process to find duplicates files which takes you back to Vista to correct the problem. However my name of the folder's subfolders 28 user/name/photos/images/pictures etc all with the same name. When you try to delete more are added. There were files, but I moved to them but there is now way to delete the files. Strange but true. Help to install Windows 7, they should be deleted.

    First try to run a disk check. Open the computer and right click on the drive,
    choose Properties, Tools tab, and then under check errors click check now and
    Follow the prompts.
     
    --
    ..
    --
    "cjones5" wrote in message news: 8cecad2e-6e5d-49ef-a6ec-e40324ea19f5...
    > Remember - this is a public forum so never post private information such
    > as numbers of mail or telephone!
    >
    > Ideas: when trying to install Window7 it cannot be installed due to a
    > folder and its subfolder had the same name... called a duplicate. There are
    > a process designed to find duplicate files which takes you back to Vista for
    > correct the problem. However my folder name is
    > subfolders user/name/photos/pictures/images etc. 28 all with the same
    > name. When you try to delete more are added. There were some files but I
    > displaced but there is now to remove the files. Strange but true.
    > Help to install Windows 7, they should be deleted.
    >
    > a.. You have problems with programs
    > b.. Error messages
    > c.. Recent changes to your computer
    > d.. What you have already tried to solve the problem
    >
     
     
  • How many user take RDP at the same time with different user login ID in Server R2 2012

    How many user take RDP at the same time with different user login ID in Server R2 2012?

    How many user take RDP at the same time with different user login ID in Server 2008 R2?

    How many user take RDP at the same time with different user login ID in Server 2012 starndard?

    How many user take RDP at the same time with different user login ID in Server 2008 standard?

    This issue is beyond the scope of this site (for consumers) and to make sure you get the best answer, we need to ask either on Technet (for IT Pro) or MSDN (for developers)

    If you give us a link to the new thread we can point to some resources it
  • Email with attached video will not leave the Outbox... Cannot delete Email with the attached video.

    Email with attached video will not leave the Outbox... Cannot delete Email with the attached video.

    Check to work offline , and then it should delete.

  • Microsoft Account closed, but cannot set up a new one with the same e-mail address

    On his brand new Dell laptop, my wife has Microsoft Windows 8, but decided to close it after the establishment because she didn't see the need for it. Now the laptop screen ask password, but it cannot reopen the account to reset the password in him telling that the account does not exist. If she tries to set up a new account with the same email address, he said that the account is not accessible.

    When you delete a Microsoft account, there is a period of 30 days to use the same address to another account.

Maybe you are looking for

  • iOS10 WiFi keep forgetting about the hidden network

    I am on iPhone 5 iOS version 10.0.1 on the T-Mobile network. In my workplace, we have hidden wifi network. It was working fine. But after my upgrade to iOS 10, the continuous phone to forget the network hidden whenever it goes to another network wifi

  • HP 7780 Pro will not install the new HP Pavilion

    I tried to implement all install a HP 7780 Pro on my new pavilion that extends from Vista Home Premium.  Support download page / has a file of 64-bit Vista on it, but I can't seem to download.  Talking with a technician online this morning, but that

  • Server does not start - works on vista

    My server does not start correctly. It crashed when you perform an automatic update of itunes / real player. When the insafe mode I try to remove the apple programs I get this message: «the instsaller could not access the service windows.» This can h

  • "KB2497640 cumulative security update - does not install.

    Hello 'update cumulative security for internet explorer 8 for windows vista' KB2497640 9.2 MB will not install on my friends computer can you please help?

  • Best way to work when using 2 computers (laptop and desktop)

    Hello Adobe users and experts.I recently invested in a laptop to use when im on a job or simply does coffee work and again on some pictures. When Im at home, I use my game rig, as I work more comfortable and can make things more tons.Currently when I