How to select only the last child nodes in a piece of XML via XMLTABLE?

Hello

I use Oracle 10.2.0.4.

I have the following XML:
with sd as (select xmltype('<Fruits>
                              <Fruit>
                                <FruitType>Apple</FruitType>
                                <FruitSubtype>Granny Smith</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Pear</FruitType>
                                <FruitSubtype>Anjou</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Pear</FruitType>
                                <FruitSubtype>Comice</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Plum</FruitType>
                                <FruitSubtype>Victoria</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Apple</FruitType>
                                <FruitSubtype>Bramley</FruitSubtype>
                              </Fruit>
                            </Fruits>') fruit_xml from dual)
select *
from   sd;
and I want to choose the last child nodes where the FruitType is Apple or pear.

So far, I've got:
with sd as (select xmltype('<Fruits>
                              <Fruit>
                                <FruitType>Apple</FruitType>
                                <FruitSubtype>Granny Smith</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Pear</FruitType>
                                <FruitSubtype>Anjou</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Pear</FruitType>
                                <FruitSubtype>Comice</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Plum</FruitType>
                                <FruitSubtype>Victoria</FruitSubtype>
                              </Fruit>
                              <Fruit>
                                <FruitType>Apple</FruitType>
                                <FruitSubtype>Bramley</FruitSubtype>
                              </Fruit>
                            </Fruits>') fruit_xml from dual)
select x.*
from   sd,
       xmltable('//Fruits/Fruit[FruitType=''Apple'' or FruitType=''Pear'']'
               passing sd.fruit_xml
               columns fruit_type VARCHAR2(25) path '//FruitType',
                       fruit_subtype VARCHAR2(25) path '//FruitSubtype') x;

FRUIT_TYPE                FRUIT_SUBTYPE
------------------------- -------------------------
Apple                     Granny Smith
Pear                      Anjou
Pear                      Comice
Apple                     Bramley
but I just want to finish with the last child node by FruitType, for example:
FRUIT_TYPE                FRUIT_SUBTYPE
------------------------- -------------------------
Pear                      Comice
Apple                     Bramley
Is it possible to do through XMLTABLE, or I have to number each of the lines that results from (what I know how to do) and then create a group to prove XMLTABLE? The latter seems awkward to me, so I'd avoid it if possible, but if this is the way to go, I can do it.

Thank you.

Is it possible to do through XMLTABLE, or I have to number each of the lines that results from (what I know how to do) and then create a group to prove XMLTABLE?

Indeed, it is a possible way:

select x.fruit_type,
       min(fruit_subtype) keep(dense_rank last order by rn) as fruit_subtype
from   sd,
       xmltable('/Fruits/Fruit'
               passing sd.fruit_xml
               columns fruit_type VARCHAR2(25) path 'FruitType',
                       fruit_subtype VARCHAR2(25) path 'FruitSubtype',
                       rn for ordinality) x
where fruit_type in ('Apple','Pear')
group by fruit_type
;

Other options, should push the logic of consolidation in the XQuery:

select x.*
from sd,
     xmltable('/Fruits/Fruit[FruitType="Apple"][last()] |
               /Fruits/Fruit[FruitType="Pear"][last()]'
              passing sd.fruit_xml
              columns fruit_type VARCHAR2(25) path 'FruitType',
                      fruit_subtype VARCHAR2(25) path 'FruitSubtype'
     ) x
;

or,

select x.*
from   sd,
       xmltable('for $i in distinct-values(/Fruits/Fruit/FruitType)
                 return /Fruits/Fruit[FruitType=$i][last()]'
               passing sd.fruit_xml
               columns fruit_type VARCHAR2(25) path 'FruitType',
                       fruit_subtype VARCHAR2(25) path 'FruitSubtype'
                       ) x
where x.fruit_type in ('Apple','Pear')
;

or,

select x.*
from   sd,
       xmltable('/Fruits/Fruit[not(following-sibling::Fruit/FruitType=./FruitType)]'
               passing sd.fruit_xml
               columns fruit_type VARCHAR2(25) path 'FruitType',
                       fruit_subtype VARCHAR2(25) path 'FruitSubtype'
                       ) x
where x.fruit_type in ('Apple','Pear')
;

Edited by: odie_63 APR 17. 2012 17:56 - added latest example

Tags: Database

Similar Questions

  • How to select only the URLS in the same page

    I'm used to be able to select only the URL with right click of the entire page. After reinstalling Firefox, I can't use this feature more. Please let me know what kind of add-on, I need to have. Thank you

    I looked in the Wayback Machine for an older version of this page. He said:

    Multi links allows you to open, copy or favorite of several links at the same time rather than having to do them all individually.

    To open, links copy or bookmark, you simply right-click and do to drag a rectangle around the links to the desire to act. When you release the right mouse button, you will open/copy/bookmark these links.

    Note: The Wayback Machine does not cache downloads real extension due to the limitations of robots.txt on the site of modules.

    One of these current extensions could be partial replacement:

    I have not tried any of them myself.

  • How to select only the objects in a marquee?

    I am a new user of Illustrator CS5, switching from Freehand. I'm trying to find out if there is a way to select only the objects in a marquee? In AutoCAD, you can make a selection on the left window to the right and only those objects entirely in the window are selected. If you the window right on the left all the objects that are "crossed" or affected by the window are selected. Is there a similar technique in Illustrator?

    Currently, I have to select the objects, and then go back and hold the SHIFT key to deselect the object I don't want, or lock layers to prevent additional items get selected.

    NO.

  • How to select only the part by using the sql query

    Hello

    I have the task to retrieve only the integral of the input text by using the sql query.

    The entry is as follows

    Entry for the price setting

    $12.5 (FYI without space)

    $ 12.5 (FYI single space)

    $ 12.5 (double space FYI)

    $12.5 (FYI multiple space)

    $12.5 (FYI multiple space)

    Output expected of 12.5

    The price is the type varchar2 column in the store_price table.

    Please let me know how to achieve this.

    Thanks in advance.

    If this is always the case that you get a $ followed by a number of places, you can use something like:

    Select to_number (ltrim ('$ 12.5',' $')) DOUBLE

    or

    SELECT ltrim ('$ 12.5',' $') OF double

    but take care of your nls_numeric_character settings if they are defined so that, for example, a comma is the decimal separator, you will have a problem.

    HTH

  • How to select only the best score?

    I have a table with notes, like this:
    Name    | Section |Score
    ----------------------
    John    | A      | 1
    John    | B      | 3
    John    | C      | 2
    Sue     | A      | 3
    Sue     | B      | 2
    Sue     | C      | 3
    Now, I want to choose the name and the average score that has the highest average score. So, in this case, the average score of John is 2 and average score of Sue is 2.6, so I want the result to be:
    Name    | Score
    -----------------------
    Sue     | 2.6
    How do I get there?

    Published by: 785131 on August 10, 2011 14:49
    PS. I don't know why, but suddenly my handle is recessed to a number, while I put a proper name for it...

    Hello

    Here's a way to do it:

    with t as (
    select 'John' as name, 'A' as section, 1 as score from dual union all
    select 'John' as name, 'B' as section, 3 as score from dual union all
    select 'John' as name, 'C' as section, 2 as score from dual union all
    select 'Sue' as name, 'A' as section, 3 as score from dual union all
    select 'Sue' as name, 'B' as section, 2 as score from dual union all
    select 'Sue' as name, 'X' as section, 3 as score from dual
    )
    select distinct
    first_value(name) over (order by avg(score) desc) max_name,
    max(avg(score)) over () max_avg
    from t
    group by name; 
    
    MAX_NAME      MAX_AVG
    ---------- ----------
    Sue        2.66666667
    

    Kind regards
    Sylvie

  • It is not clear to me that my desktop images get synchronized to the mobile in Lightroom or how to select only the pictures I want to synchronize

    some clarification on this would be appreciated

    I see no advantage to have all my images Desktop Sync and that's what happened so that I stopped the synch

    Hi natureron,

    You have the choice of synchronization of specific collections you want.

    In the Panel collections, there is a sync button on the left of the collections that you can choose and set the collections that you want to synchronize.

    And the same button can be used to stop the synchronization for a specific collection.

    See this tutorial for more information on synchronization:

    How to start using Lightroom on mobile | Adobe Photoshop Lightroom CC tutorials

    How do I synchronize Lightroom desktop and mobile application of Lightroom

    Kind regards

    Claes

  • How to set up the multimedia keys (only the last 3 buttons)

    Hello

    How to set up the multimedia keys (only the last 3 buttons)? The first button works, you open the browser and the other open WMP.
    But the last 3 buttons (play/pause, Stop, forw/rewd) aren´t work with WMP. How can I fix?

    Thank you

    Hello

    Can you please tell us what model of laptop Satellite A do you have?

  • How can I find the last selection OR has highlighted it selection?

    When I select some elements in indesign I have the option of 'command + click' to highlight one selections. How can I find this item highlighted in the script?

    OR

    How I'll get the last item selected in indesign.


    I'm writing a script that relies on certain properties of the element highlight or the last selected item.


    Thank you


    Screen Shot 2014-08-01 at 12.17.03 PM.png

    Thank you myDavey. It's not exactly what I was looking for, but he had good information on something else, I'm trying.

    I found the solution to my problem. This highlighted selection apparently called the object 'key '.

    And it can be used in the script as app.selectionKeyObject. This solves what I was looking for.

    And you can find the last selection by selecting the last item in the selection table.

  • How can I stop the last post showing when I want to send this person a new Imessage.  I have an Iphone 5s

    How can I stop the last post showing when I want to send this person a new Imessage.  I don't want an old conversation appears constantly when I am a Messenger contact.  I have an Iphone 5s

    I don't know, but let me see if I understand what you're asking. You do not want to see the thread you have with a particular contact when you go to send them another message? The only way to do this is to remove the previous message thread. If you send a message to someone that you already have a thread of conversation with, it will continue to post in this thread. There is no such thing as having multiple threads with the same person. All conversations with the same person would remain in the same thread, unless you remove the previous discussions. If you delete your previous conversation, when you go to start a new conversation, he won't have any of the other answers.

  • How can I keep the last 5 test results as the test continues?

    Hello

    I have a test requirement where I need to run a series of engine failure or a large number of tracks, each for a short period of time. I measure intensity charged for each race. I need to keep only the last 5 records of each engine. How to roll the table to just keep the last 5 surveys?

    Where is my aspirin...

    Hi slipstick,.

    Here are the main steps:

  • I can see only the last fax received in my PC and not all of them using an Officejet Pro 8600 Plus

    I create a file in my office where received faxes can be saved. Indeed, I can see the fax received, but if I get one more, it replaces the previous one. So, I see only the last received fax. How do I see all of them and does not replace another?

    The problem was that I didn't set the time on the machine of the printer.   I just put the date and time and the problem solved. I get faxes pdf correctly.  Be it, thanks for the help!

  • How to call only the operations of several connectors of ICF through simple connector server

    Hi Experts,

    I developed two connectors of the ICF (ICF1 and ICF2) and placed the beams of connector on the same server connector.

    Please guide me how to get only the authorities of each of the connector to call operations of each separately.

    I use following code-

    List of < ConnectorInfo > this.getConnectorInfoManager = cInfos () .getConnectorInfos ();

    System.out.println (cInfos.Size ());

    for {(ConnectorInfo cInfo:cInfos)

    APIConfiguration apiConfiguration = cInfo.createDefaultAPIConfiguration ();

    setPoolConfigurations (apiConfiguration);

    Discoveryendpointspecifie configProps = apiConfiguration.getConfigurationProperties ();

    this.setUpConfigurationProperties (configProps);

    ConnectorFacadeFactory facadeFactory = ConnectorFacadeFactory.getInstance ();

    ConnectorFacade connectorFacade = (apiConfiguration) facadeFactory.newInstance;

    connectorFacade.test ();

    }

    Methods of all connectors are called here to test and how do I selectively invoke test() selective connectors?

    Hello

    The connector of the ICF is called by these configurations in the 'Lookup.CONNECTOR_NAME. Research of configuration. The search name is configured in the COMPUTER resource

    We have the following values configured in the search based on who the connector class fires is

    Name of the connector

    org.identityconnectors.CONNECTOR_NAME. Connector

    Main connector class identity. It is the class that implements the SPI of the ICF framework operations.

    Name of the bundle

    org.identityconnectors.CONNECTOR_NAME

    Name of the identity connector bundle

    In Version

    11.1.1.5.x

    Version connector identity

  • replace only the last 3 characters within a string

    Hi all
    I have the string "abcdef_01". How can I replace the last 3 characters with "000"? in this string, for example "_01" should be replaced by '000'. Any way to do it, without the concatenation of the original string with the last 3 characters replaced by "000"?
    Thank you

    Edited by: Roger22 the 18.10.2011 09:29

    Yes, you can...

    SELECT rpad(substr('abcdef_01',1,length('abcdef_01')-3),length('abcdef_01'),chr(dbms_random.value(32,64))
                                                                             || chr(dbms_random.value(32,64))
                                                                             || chr(dbms_random.value(32,64)))
    FROM dual;
    

    RPAD/lpad may use a third parameter that is longer than 1 character and - if the duration of param 1 + length of parameter 3 is longer that parameter 2 - truncates the result to fit the length of parameter 2.

    HTH

  • Select only the objects in a rectangle selection

    I am a new user of Illustrator CS5, switching from Freehand. I'm trying to find out if there is a way to select only the objects in a marquee? In AutoCAD, you can make a selection on the left window to the right and only those objects entirely in the window are selected. If you the window right on the left all the objects that are "crossed" or affected by the window are selected. Is there a similar technique in Illustrator?

    Currently, I have to select the objects, and then go back and hold the SHIFT key to deselect the object I don't want, or place objects on different layers and then lock the layers to prevent additional items get selected.

    In AutoCAD, my main program, you simply draw a window selection, left or right, depending on what your next step would be. No special tools or rocking or thinking, that's just the selection primary process and very intuitive.

    I posted a discussion on the Illustrator forum several months ago, which still arouses comments so this seems to be a tool that would be appreciated by many users. Someone posted a script but it has not worked for me yet.

    Thank you for everything that you can do to solve this problem.

    This is a copy of my last post of the original thread. I found some discussions confused because I have no experience in writing or by using scripts. So I copied all the recommendations in a document to read without the comments and digressions. After several hours and a lot of searching on Google I found what solution adapted to my goal, which was a selection process fast and simple with fewer steps to use.

    So, I think I have this job. The following is what I did:

    1 copy the script by Carlos Canto post #11 here:

    http://forums.Adobe.com/thread/856221?start=0&TSTART=0

    2. open the ExtendScript Toolkit Utilities > Adobe utilities - CS5.5 > ExtendScript Toolkit CS5.5 > ExtendScript Toolkit CS5.5.App

    3. when the program opens, paste the script in the large Panel on the left.

    4. then go to file > save as.

    5. that the dialog box click on the arrow next to the save box slot to expand the section options.

    6. next, go to Applications > Adobe Illustrator CS5.1 > Presets > en_US > Scripts.

    7. then, in the Save as box enter a name to identify the script (I named mine Window1).

    8. click on save and close the Toolbox.

    Launch Illustrator and open a drawing.

    1 using the rectangle tool, I drew a rectangle around the objects.

    2. make sure that the new rectangle is selected.

    3. I went to file > Scripts > Window1.

    4A got an error message - error 8705: target layer cannot be changed 25-> iart.selected + true line:

    Oops, you must have ALL unlocked layers.

    5. you click OK.

    6 unlocked all the layers.

    7. my new activated rectangle.

    8. went to file > Scripts > Window1.

    9 here! Items in the rectangle are selected and the rectangle is removed.

    So yes, this script works as requested. Thank you, Carlos.

  • a table of images only the last image shows the index

    Hello world

    I try to index a table of images and all, but the last item gives me the error: "not an image."

    Here is a picture of the vi. The first loop load images and stores them in an indexed auto tunnel. Each diagnosis that I fell on this loop showed that all is well. The second loop must display the image stored in the tunnel sequence indexed auto, but it does not work. Instead, I got the error message that the 0 to n - 1 elements are not images, and only the last image is displayed.

    What I am doing wrong?

    Thanks for your help.

    and vi:

    Hello

    This VI has worked well for me with a contribution of 5 bmp files. You can check your input files?

Maybe you are looking for

  • Extension of the memory for Satellite L500 - 1-7

    Hello! I'm looking for more memory for my Satellite L500 - 1 7 portable. I am looking for 2 or 4 x 4 GBbut I can't believe that the price is about 249.00 for a 4 GB memory DDR2 PC2(800 MHz) (in the toshiba official store). I was looking on amazon and

  • Audio driver for Windows 8.1

    Hi I have recently updated to windows 8 to 8.1. Where can I get the audio driver for the modelType: laptopOS: 8.1 Pro 64 bit Windows Country: INDIA Model: PC portable HP 2000-2121TU (Driver Audio P.S:Realtek.)

  • Can I set the sensitivity of my touchpad?

    I have a HP Pavilion laptop US g7-1260. How to set the sensitivity of my touchpad?

  • When I try to start the time service Windows, I get a message:

    When I try to start the time service Windows, I get a message: ------------------------------------------------------------------------------------------------------------ Windows could not start the time Service Windows on the Local computer. Error

  • Problem AP1522 with WLC 4402

    Hello I have a problem installing a 1522 AP to the WLC 4402, it is not recognized, what can be? Check all connections and all is well thank me for help