Doubt with query that returns unnecessary reocrds

Hello
I use under query to retrieve security descending titles in a database:

Select master sre1.security_id,
sre2. Clone SECURITY_ID, sa1. Addr STREET_ADDR1
Of
SAM_REAL_ESTATE sre1, SAM_REAL_ESTATE sre2, sam_address sa1, sa2 sam_address
where
sre1. ADDRESS_ID = sa1. ADDRESS_ID and sre2. ADDRESS_ID = sa2. ADDRESS_ID
and sre1.security_id < sre2. SECURITY_ID
and trim (nvl (upper (sa1. STREET_ADDR1), 'Null')) = trim (nvl (upper (sa2. STREET_ADDR1), 'Null'))
and in sre1.security_id (175459876,175460893,175460770,20530359,21943331,20306795)
and in sre2.security_id (175459876,175460893,175460770,20530359,21943331,20306795)
order by 1, 2;


which returns results as below:

MASTER CLONE ADDR
20306795 20530359 Waverley
20306795 21943331 Waverley
* 20530359 21943331 Waverley *.
175459876 175460770 temple
175459876 175460893 temple
* 175460770 temple 175460893 *.

Now, I do not want the selected records in bold as the safety of clone is coming security master while only the less registration by different address should enter into main list and the rest of his security should come clone

Please suggest

Thank you
Deepak

NOT TESTED

select min(master) master,clone,addr
  from (select sre1.security_id master,
               sre2.SECURITY_ID clone,
               sa1.STREET_ADDR1 addr
          from SAM_REAL_ESTATE sre1,
               SAM_REAL_ESTATE sre2,
               sam_address sa1,
               sam_address sa2
         where sre1.ADDRESS_ID = sa1.ADDRESS_ID
           and sre2.ADDRESS_ID = sa2.ADDRESS_ID
           and sre1.security_id < sre2.SECURITY_ID
           and trim(nvl(upper(sa1.STREET_ADDR1),'Null')) = trim(nvl(upper(sa2.STREET_ADDR1),'Null'))
           and sre1.security_id in(175459876,175460893,175460770,20530359,21943331,20306795)
           and sre2.security_id in(175459876,175460893,175460770,20530359,21943331,20306795)
         order by sre2.SECURITY_ID,upper(sa1.STREET_ADDR1),sre1.security_id
       )
 group by clone,addr
 order by 1,2

Concerning

Etbin

It is not a Top - N query

Edited by: Etbin the 13.8.2009 12:00

Tags: Database

Similar Questions

  • Need help to write a MySQL query that returns only the peer matching records

    Because I don't know how to explain it easily, I use the table below as an example.

    I want to create a MySQL query that returns only the records that match counterparts where 'col1' = 'ABC '.

    Notice the ' ABC / GHI' record does not have a Counter-match ' GHI / ABC' record. This record must not be returned because there is no Counter-Party correspondent. With this table, the ' ABC / GHI' record should be the one returned in the query.

    How can I create a query that will do it?


    ID | col1 | col2
    --------------------
    1. ABC | DEF
    2. DEF | ABC
    3. ABC | IGS
    4. DEF | IGS
    5. IGS | DEF


    * Please let me know if you have no idea of what I'm trying to explain.

    I wanted to just the results where col1 = ABC, but I already got the answer I needed on another forum. Thank you anyway.

    SELECT a.col1,
    a.col2
    FROM table_name AS a
    LEFT OUTER
    Table_name JOIN b
    ON b.col1 = a.col2
    AND a.col1 = b.col2
    WHERE b.col1 IS NOT NULL AND a.col1 = 'ABC '.

  • Query that returns number 1 - Add additional columns based on percentages

    I have a query that returns a large number.  The return value will always be a single field (a column, a line, a single value).

    I need to have also three other columns in the return of this request: one that returns a number that is 50% of the original number, one that returns a number that is 75% of the original number, and one that is double the original number.  How is that possible?  If anyone can point me to examples, it would be a great help.  I got the results mixed by searching, so I'm not sure good practices.

    Thank you for the input in advance.

    Hello

    Whenever you have a question, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the accurate results you want from this data, so that people who want to help you can recreate the problem and test their ideas.

    Explain, using specific examples, how you get these results from these data.

    If you show what you want to do using commonly available tables, like those of the scott schema, then you don't need to display the sample data; just results and explanations.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: Re: 2. How can I ask a question on the forums?

    I'm not sure that understand the question.

    If x is a number, then

    .50 * x is 50% of this number,

    .75 * x is 75% of that number, and

    2 * x is to double this figure.

    You can get them all in the same query, if you want to.  For example:

    SELECT ename

    sal

    .50 * sal AS p_50

    .75 * sal AS p_75

    2 * sal AS dbl

    FROM scott.emp;

    Output:

    ENAME SAL P_50 P_75 DBL

    ---------- ---------- ---------- ---------- ----------

    SMITH, 800, 400, 600, 1600

    1600 800 1200 3200 ALLEN

    1250 625 WARD 937,5 2500

    JONES 2975 1487.5 2231,25 5950

    1250 625 MARTIN 937,5 2500

    2850 1425 BLAKE 2137,5 5700

    2450 1225 CLARK 1837,5 4900

    3000 1500 2250 6000 SCOTT

    KING 5000 2500 3750 10000

    1500 750 1125 3000 TURNER

    1100 550 825 2200 ADAMS

    JAMES 950 475 712,5 1900

    FORD 3000 1500 2250 6000

    1300 650 975 2600 MILLER

    Is that what you ask?

  • I need a query that returns the average amount of characters from text colum in MS SQL.

    I need a query that returns the average amount of characters from text colum in MS SQL.

    Could someone show me how?

    Sorting, I need of the

    DATALENGTH

    function

  • SQL QUERY THAT RETURNS THE PL/SQL

    Hello

    I'm putting in 4.2 below apex

    DECLARE

    date of frdate: =: P22_FROMDATE;

    date date: =: P22_TODATE;

    l_date1 varchar2 (11): = to_char(frdate,'dd-mon-yyyy');

    l_date2 varchar2 (11): = to_char(todate,'dd-mon-yyyy');

    v_sql varchar2 (32000);

    v_Name varchar2 (4000): = months_name (frdate, todate);

    char (1) of the v1: = q "[']";

    number of lbr1 (6): =: P22_FROMBRANCH;

    number of LBR2 (6): =: P22_TOBRANCH;

    number of M1 (6): = 11;

    Start

    v_sql: ='SELECT * FROM (SELECT LBRCODE, PRDACCTID, MN, AVGX FROM (SELECT LBRCODE, PRDACCTID, MN, SUM (BALL) BALL, COUNT (MN) DAYS, ROUND (SUM (BAL) count (MN), 2) AVGX ';))

    v_sql: = v_sql | "FROM (SELECT LBRCODE, PRDACCTID, TO_CHAR (BALDATE,'|)) CHR (39) | ' MM' | CHR (39) |') ' | ' ||'|| CHR (39) | » _'|| CHR (39) |' | ' || «TO_CHAR (BALDATE,'|)» CHR (39) | ' AAAA '. CHR (39) |') MN, BALL ';

    V_SQL: = V_SQL | "(SELECT Q2. LBRCODE, Q2. PRDACCTID, T1. BALDATE, T_OST_NEW (Q2. LBRCODE, Q2. PRDACCTID, T1. BALDATE) BALL ';

    V_SQL: = V_SQL | "FROM (select TO_DATE('||) Chr (39) | l_date1 | Chr (39) | «, » || CHR (39) | ' DD-MON-YYYY ' | CHR (39) |') baldate FROM DUAL Union all the ';

    V_SQL: = V_SQL |' select (TO_DATE('||) Chr (39) | l_date1 | Chr (39) | «, » || CHR (39) | ' DD-MON-YYYY ' | CHR (39) |') + level) double baldate ';

    V_SQL: = V_SQL |' connect by level < = (TO_DATE('||) Chr (39) | l_date2 | Chr (39) | «, » || CHR (39) | ' DD-MON-YYYY ' | CHR (39) |') -TO_DATE('||) Chr (39) | l_date1 | Chr (39) | «, » || CHR (39) | ' Dd-mon-yyyy ' | CHR (39) |')) ) Q1,';

    V_SQL: = V_SQL | "(SELECT LBRCODE,'|) ' (rpad (prdcd, 8,'|)) V1 ||'' || v1 |') || LTRIM (rpad('|| v1 ||) » x'|| V1 | «, 25, » || v1 | » 0' || v1||'),'|| v1 | » x'|| v1 |')) prdacctid OF D009021 WHERE the LBRCODE between ' | : P22_FROMBRANCH |' and ' | : P22_TOBRANCH | "AND moduleinfo =' | TO_NUMBER(:P22_SELECTLIST);

    V_SQL: = V_SQL |') ((Q2)) GROUP BY LBRCODE, PRDACCTID, MN))';

    V_SQL: = V_SQL | "PIVOT (MAX (AVGX) (MN) IN (';))

    V_SQL: = V_SQL | V_NAME;

    V_SQL: = V_SQL |')) ORDER OF LBRCODE, PRDACCTID ';

    RETURN V_SQL;

    END;

    I created all the elements of the required page

    I get the error message like missing expression

    to test, I created a function to return the query, it works very well know to return a query that gives me desired report

    am I missing something?

    Help, please

    HEMU wrote:

    Hello Sir

    Sorry to bother you again

    I tried to use «function that returns colon-delimited topics»

    and I got following error

    unable to determine query headings: ORA-06550: line 1, column 138: PLS-00103: Encountered the symbol ";" when expecting one of the following:  . ( ) , * % & = - + < / > at in is mod remainder not rem =>  <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultiset The symbol ")" was substituted for ";" to continue.
    
    failed to parse SQL query:
    

    can take you a look at page901 once again please and a function named months_namex

    is it feasible that I'm looking for?

    The immediate cause of this error is simply incorrect report headings function call syntax:

    return months_namex(to_date(:p901_fromdate,'dd-mon-yyyy'),to_date(:p901_todate,'dd-mon-yyyy')

    This should be:

    return months_namex(to_date(:p901_fromdate,'dd-mon-yyyy'),to_date(:p901_todate,'dd-mon-yyyy'));

    However there is no months_namex function defined in the workspace.

  • Help turn the query that returns single return system

    Hello, I have two tables, readings, and values. Values have a foreign key to the readings.
    create table readings 
    (
    id number primary key,
    point number,
    datereading date
    );
    
    create table reading_values
    (
    id number primary key,
    counter_nr number,
    point number,
    reading_id number,
    datereading date,
    CONSTRAINT fk_reading
    FOREIGN KEY (reading_id)
    REFERENCES readings (id)
    );
    The point and datereadings columns have the same value of related rows.
    insert into readings values (1, 1234, to_date('20100622 12:12', 'yyyymmdd HH24:MI'));
    insert into reading_values values (1, 0, 1234, 1, to_date('20100622 12:12', 'yyyymmdd HH24:MI')); --row1
    insert into reading_values values (2, 1, 1234, 1, to_date('20100622 12:12', 'yyyymmdd HH24:MI')); --row2
    
    insert into readings values (2, 1234, to_date('20100623 12:12', 'yyyymmdd HH24:MI'));
    insert into reading_values values (3, 0, 1234, 2, to_date('20100623 12:12', 'yyyymmdd HH24:MI')); --row3
    insert into reading_values values (4, 1, 1234, 2, to_date('20100623 12:12', 'yyyymmdd HH24:MI')); --row4
    
    insert into readings values (3, 1111, to_date('20100621 12:12', 'yyyymmdd HH24:MI'));
    insert into reading_values values (5, 0, 1111, 3, to_date('20100623 12:12', 'yyyymmdd HH24:MI')); --row5
    Now I have the procedure which the entry: p_point and p_date.
    select * from reading_values rv, 
    (select * from (select id, datereading
    from readings where datereading < p_date and point = p_point
     order by datereading desc) where rownum <= 1) t where rv.reading_id = t.id
    so I want the previous reading_values (in time based on p_date) for a certain point p_date. I hope this makes sense?
    Example:
    select * from reading_values rv, 
    (select * from (select id, datereading
    from readings where datereading < to_date('20100624 12:12', 'yyyymmdd HH24:MI') and point = 1234
     order by datereading desc) where rownum <= 1) t where rv.reading_id = t.id
    
    returns row 3 and 4
    Now, what I want to do, is replace p_point and p_date with two collections. For example: p_points = [1234, 1111] p_dates = [20100622 15:12, 20100624 12:12]
    Here, I want to have the previous reading_values by point = 1234 and datereading < 20100622 order 15:12 by datereading (where rownum < = 1).

    and also reading_values by point = 1111 and datereading < 20100624 order 12:12 by datereading (where rownum < = 1).

    This should return line1, line2 and row5...

    So far I came up with this. But I can't be sure that it is correct. And the performance seems to be horrible (cost 88, 13 recursive calls, 155 becomes uniform) for a query with 2 entry points and 2 dates compared to the original to just a single point query and the date of entry (cost 13, 1 recursive call and gets 14 coherent).
    Select rv.* from (SELECT r.id
          FROM    Readings r, 
          (select rownum rn1, t1.id from table1 t1 order by id) t1, -- input parameter1 (point collection)
          (select rownum rn2, t2.date_now from table1 t2 order by id) t2  -- input parameter 2 (date collection)
          WHERE  r.point = t1.id
              AND     r.datereading = (SELECT  max(r2.datereading)
              FROM   Readings r2
                WHERE   r2.point = t1.id
                AND     r2.datereading < t2.date_now AND rn1 = rn2)
        ) r, Reading_Values rv where r.id= rv.reading_id;
    code for creating table 1 that I used instead of collections of entry:
    create table table1
    (
     id number not null primary key,
     date_now date not null
    );
    insert into table1 values (1234, to_date('20100622 15:12', 'yyyymmdd HH24:MI'));
    insert into table1 values (1111, to_date('20100624 12:12', 'yyyymmdd HH24:MI'));
    Hope someone can help me on my way :) Please ask if something is unclear or strange! Thank you!

    Published by: KarlTrumstedt on 23-jun-2010 06:20

    Try this

    SELECT RV.ID, RV.COUNTER_NR, RV.POINT, RV.READING_ID, RV.DATEREADING
    FROM (
        SELECT (    SELECT LAST_VALUE(RE.ID) OVER
                         (PARTITION BY RE.POINT ORDER BY RE.DATEREADING)
                    FROM READINGS RE
                    WHERE RE.DATEREADING < T1.DATE_NOW AND RE.POINT = T1.ID
                ) ID,
                T1.ID POINT,
                T1.DATE_NOW
        FROM TABLE1 T1
    ) X
    JOIN READING_VALUES RV
    ON (X.ID = RV.READING_ID AND X.POINT = RV.POINT)
    ;
    
  • Query that returns only certain fields in Java

    Currently I use a filter that identifies certain keys, then get all the keys and get a unique value of each object. The rest of the object is important enough. I've seen references to the passage on the ability to do it, using CohQL, but I can't run Java.

    Basically, I want to do: "select the"Cache"box where key ="asdf '" I just want back the specific 'field' rather than the entire object. How can I do this?

    Thank you.

    Hi user13279246,

    You can do this by using the ReducerAggregator. Your code would look like:

    Filter filter = QueryHelper.createFilter(...);
    ValueExtractor extractor = new ReflectionExtractor("isReadOnly");
    Map mapResult = cache.aggregate(filter, new ReducerAggregator(extractor));
    

    The mapResult will be indexed by the actual cache keys, but the values will be extracted Boolean property.

    If you need to return a number of "columns", you would use the MultiExtractor instead. In this case, each value in the returned map would be a list of corresponding extracted properties.

    Kind regards
    Gene

    Published by: ggleyzer on February 15, 2011 16:41

  • Query that returns several generic criteria using as and in

    Hi there, try to do something like this:

    Select * from tablename
    Where columnname as 5% ' or as 8% ' or like 12% '

    without the help OR... so in this big:

    Select * from tablename
    Where columnname like IN (5% ", 8%, 12%" ")

    In MS Sql, I can do something like this:

    SELECT * from tablename t
    Where exists (select val FROM dbo.fn_String_To_Table (25, 05', '', 0))
    ATA columname WHERE AS val + '%')

    Where dbo.fn_String_To_Table converts the array of strings in a table.

    Any suggestions?

    Thank you very much.

    Samir says:

    where the comma-delimited string variable?

    Use your imagination :):

    select  *
      from  tablename t
      where regexp_like(t.column_name,'^(' || replace(starts_with_scv,',','|') || ')')
    / 
    

    SY.

  • Looping on a method that returns a query

    Hello

    I have a method that returns a query, I convey in this method, an identification number, the query uses in where clause. Now I have another query that returns a list of identification numbers that I have to loop through the appellant the first method and passing in the ID number, for example

    methodA

    Browse the methodA results
    Call methodB passing methodA.ID
    results of methodB
    close the loop

    now, the problem I have is that methodB returns a query and I want to be able to return the results of methodB as a unique query structure. I don't know how many times I will do a loop through the results of methodA, maybe twice, he could by ten times. Thus, the results of calling methodB could be very large.
    How can I add methodB results in a single query structure?

    I finally had to create a monster query with joins in about three tables, but it works.

  • XMLForest with multiple subqueries return not some tags

    Hello

    PL/SQL in Oracle 11.2.0.1.0 (stored procedure).

    I am using this slider (I only show a part of the cursor - hope that is not a problem):

    XMLForest)
    XMLForest)
    (SELECT XMLConcat)
    XMLForest (brd.service_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:Insemination",
    XMLElement ("ns3:Bull",
    XMLElement("ns3:RegistrationAnimalNumber", brd.sire_reg_id)))
    OF cmi_breedings brd
    WHERE brd.anm_id = anm.id
    AND brd.sire_reg_id IS NOT NULL
    AND brd.service_date = (SELECT MAX (brd2.service_date)
    OF cmi_breedings brd2
    WHERE brd2.anm_id = brd.anm_id)) AS "AnimalEvent."
    (SELECT XMLConcat)
    XMLForest (lct.start_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:Parturition",
    XMLElement("ns3:LactationNumber", lct.lact_no)))
    OF cmi_lactations lct
    WHERE lct.anm_id = anm.id
    AND lct.start_date = (SELECT MAX (lct2.start_date)
    OF cmi_lactations lct2
    WHERE lct2.anm_id = lct.anm_id)) AS "AnimalEvent."
    (SELECT XMLConcat)
    XMLForest (lct.end_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:DryOff")))
    OF cmi_lactations lct
    WHERE lct.anm_id = anm.id
    AND lct.end_date = (SELECT MAX (lct2.end_date)
    OF cmi_lactations lct2
    WHERE lct2.anm_id = lct.anm_id)) AS "AnimalEvent."
    (SELECT XMLConcat)
    XMLForest (prg.prgncy_chk_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:PregnancyCheck",
    XMLElement ("ns3:PregnancyCheckResult", DECODE (prg.prgncy_ind, '1', 'F', 'E')))
    OF cmi_pregnancies prg
    WHERE prg.anm_id = anm.id
    AND prg.prgncy_chk_date = (SELECT MAX (prg2.prgncy_chk_date)
    OF prg2 cmi_pregnancies
    WHERE prg2.anm_id = prg.anm_id)) AS 'AnimalEvent '.
    ) AS "AnimalEvents".
    )

    which means (as expected and works perfectly):

    < AnimalEvents >
    < AnimalEvent >
    < ns3:BeginDate > 1997 - 09 - 08 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Insemination >
    < ns3:Bull >
    < ns3:RegistrationAnimalNumber > 5401809 < / ns3:RegistrationAnimalNumber >
    < / ns3:Bull >
    < / ns3:Insemination >
    < / AnimalEventType >
    < / AnimalEvent >
    < AnimalEvent >
    < ns3:BeginDate > 1997 - 05 - 10 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Parturition >
    < ns3:LactationNumber > 8 < / ns3:LactationNumber >
    < / ns3:Parturition >
    < / AnimalEventType >
    < / AnimalEvent >
    < AnimalEvent >
    < ns3:BeginDate > 1998 - 02 - 02 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:DryOff / >
    < / AnimalEventType >
    < / AnimalEvent >
    < / AnimalEvents >

    However, if I wanted to return several rows for an animal insemination event by changing the query like this:


    XMLForest)
    XMLForest)
    (SELECT XMLConcat)
    XMLForest (brd.service_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:Insemination",
    XMLElement ("ns3:Bull",
    XMLElement("ns3:RegistrationAnimalNumber", brd.sire_reg_id)))
    OF cmi_breedings brd
    WHERE brd.anm_id = anm.id
    AND brd.sire_reg_id IS NOT NULL
    AND brd.service_date > (SELECT MIN (brd2.service_date) <-what IS THE LINE has BEEN CHANGED)
    OF cmi_breedings brd2
    WHERE brd2.anm_id = brd.anm_id)) AS "AnimalEvent."
    (SELECT XMLConcat)
    XMLForest (lct.start_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:Parturition",
    XMLElement("ns3:LactationNumber", lct.lact_no)))
    OF cmi_lactations lct
    WHERE lct.anm_id = anm.id
    AND lct.start_date = (SELECT MAX (lct2.start_date)
    OF cmi_lactations lct2
    WHERE lct2.anm_id = lct.anm_id)) AS "AnimalEvent."
    (SELECT XMLConcat)
    XMLForest (lct.end_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:DryOff")))
    OF cmi_lactations lct
    WHERE lct.anm_id = anm.id
    AND lct.end_date = (SELECT MAX (lct2.end_date)
    OF cmi_lactations lct2
    WHERE lct2.anm_id = lct.anm_id)) AS "AnimalEvent."
    (SELECT XMLConcat)
    XMLForest (prg.prgncy_chk_date AS "ns3:BeginDate",
    'INF' AS 'ns3:ActionCode'),
    XMLElement ("AnimalEventType",
    XMLElement ("ns3:PregnancyCheck",
    XMLElement ("ns3:PregnancyCheckResult", DECODE (prg.prgncy_ind, '1', 'F', 'E')))
    OF cmi_pregnancies prg
    WHERE prg.anm_id = anm.id
    AND prg.prgncy_chk_date = (SELECT MAX (prg2.prgncy_chk_date)
    OF prg2 cmi_pregnancies
    WHERE prg2.anm_id = prg.anm_id)) AS 'AnimalEvent '.
    ) AS "AnimalEvents".
    )

    I get an error message on several rows returned (and that's what I want to be returned in some cases, more than one record).

    For example, I would look like this (notice that I have an extra "insemination" in the example below):

    < AnimalEvents >
    < AnimalEvent >
    < ns3:BeginDate > 1997 - 09 - 08 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Insemination >
    < ns3:Bull >
    < ns3:RegistrationAnimalNumber > 5401809 < / ns3:RegistrationAnimalNumber >
    < / ns3:Bull >
    < / ns3:Insemination >
    < / AnimalEventType >
    < / AnimalEvent >
    < AnimalEvent >
    < ns3:BeginDate > 1998 - 09 - 08 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Insemination >
    < ns3:Bull >
    < ns3:RegistrationAnimalNumber > 2221809 < / ns3:RegistrationAnimalNumber >
    < / ns3:Bull >
    < / ns3:Insemination >
    < / AnimalEventType >
    < / AnimalEvent >
    < AnimalEvent >
    < ns3:BeginDate > 1997 - 05 - 10 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Parturition >
    < ns3:LactationNumber > 8 < / ns3:LactationNumber >
    < / ns3:Parturition >
    < / AnimalEventType >
    < / AnimalEvent >
    < AnimalEvent >
    < ns3:BeginDate > 1998 - 02 - 02 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:DryOff / >
    < / AnimalEventType >
    < / AnimalEvent >
    < / AnimalEvents >

    I tried to use the XMLAgg() in the part of the query that returns the inseminations, but it comes down to something like this (I've lost my < Animalevent > for the second record of the insemination, I want it to look to the example above):

    < AnimalEvents >
    < AnimalEvent >
    < ns3:BeginDate > 1997 - 09 - 08 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Insemination >
    < ns3:Bull >
    < ns3:RegistrationAnimalNumber > 5401809 < / ns3:RegistrationAnimalNumber >
    < / ns3:Bull >
    < / ns3:Insemination >
    < / AnimalEventType >
    < ns3:BeginDate > 1998 - 09 - 08 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Insemination >
    < ns3:Bull >
    < ns3:RegistrationAnimalNumber > 2221809 < / ns3:RegistrationAnimalNumber >
    < / ns3:Bull >
    < / ns3:Insemination >
    < / AnimalEventType >
    < / AnimalEvent >
    < AnimalEvent >
    < ns3:BeginDate > 1997 - 05 - 10 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:Parturition >
    < ns3:LactationNumber > 8 < / ns3:LactationNumber >
    < / ns3:Parturition >
    < / AnimalEventType >
    < / AnimalEvent >
    < AnimalEvent >
    < ns3:BeginDate > 1998 - 02 - 02 < / ns3:BeginDate >
    < ns3:ActionCode > INF < / ns3:ActionCode >
    < AnimalEventType >
    < ns3:DryOff / >
    < / AnimalEventType >
    < / AnimalEvent >
    < / AnimalEvents >

    I tried different combinations of XMLAgg() for the multiple rows returned, and he allowed too lines returned the error to go away, but at the expense of the tag being AnimalEvent to be reproduced for the same animals event or be there only once when there are several animal events.  I know I'm close to the solution and I bet it's very simple, but I don't get it.

    The only "rules" that I need are:

    1. the AnimalEvents tag must appear only once and only as it is at least an AnimalEvent.

    2. no blank/empty tag should appear.  For example, if there is no AnimalEvent tag while there should be no tag AnimalEvents.

    3. even if I'm trying to select the records of several "insemination" only so far for this particular test, all 4 potential AnimalEvents ("Insémination", "Parturition", 'DryOff' and 'PregnancyCheck') will be rescheduled to select multiple records later.

    4. each animal individual event ("Insemination", "Parturition", 'DryOff', 'PregnancyCheck') should be locked up by the AnimalEvent tag.

    I don't know if I'm missing any other details?

    Help, please!  Thank you.

    I guess you wrote that because I used it somewhere, that seems like a bad idea

    I understand why you used, and why most of the people use it in this kind of situation, but as see you it clearly now, it is not suitable more to what you aim at. Where my suggestion while trying not to use in the first place.

    In addition, XMLForest is just a shortcut, in fact, a combination of a CASE statement and XMLElement that Oracle develops during analysis.

    In some cases, where the subquery caching is not possible for example, that made a request using XMLForest performs badly if the operands are complex because they are used twice during the assessment.

    Here's a simple example:

    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    
    SQL> set serveroutput on
    SQL>
    SQL> create or replace function get_data (p_input in number) return number is
      2  begin
      3    dbms_output.put_line('You called me!');
      4    return p_input;
      5  end;
      6  /
    
    Function created.
    
    SQL> select xmlserialize(content xmlforest(get_data(1) as "test"))
      2  from dual;
    
    XMLSERIALIZE(CONTENTXMLFOREST(GET_DATA(1)AS"TEST"))
    --------------------------------------------------------------------------------
    1
    
    You called me!
    You called me!
    

    The function is called twice, because Oracle rewrites the XMLForest call to:

    BOX WHEN get_data (1) IS NOT NULL THEN XMLElement ('test', get_data (1)) END

    Of course, the function can be deterministic, but this isn't the point here.

    In your case, since you are with subqueries, those are probably cached and executed only once, but I think it is useful to know the internal mechanisms, however.

    Back to your question, how about something like this:

    XMLForest(
      -- concat the four possible events
      XMLConcat(
        -- Insemination
        (
          SELECT CASE WHEN count(*) != 0
                 THEN XMLAgg(
                        XMLElement("AnimalEvent",
                           XMLForest(brd.service_date AS "ns3:BeginDate",
                                     'INF'            AS "ns3:ActionCode"),
                           XMLElement("AnimalEventType",
                              XMLElement("ns3:Insemination",
                                 XMLElement("ns3:Bull",
                                    XMLElement("ns3:RegistrationAnimalNumber", brd.sire_reg_id))))
                        )
                      )
                 END
          FROM   cmi_breedings brd
          WHERE  brd.anm_id       = anm.id
          AND    brd.sire_reg_id  IS NOT NULL
          AND    brd.service_date > (SELECT MIN(brd2.service_date)
                                     FROM   cmi_breedings brd2
                                     WHERE  brd2.anm_id = brd.anm_id)
        ),
        -- Parturition
        (
          SELECT CASE WHEN count(*) != 0
                 THEN XMLAgg(
                        XMLElement("AnimalEvent",
                          XMLForest(lct.start_date AS "ns3:BeginDate",
                                    'INF'          AS "ns3:ActionCode"),
                          XMLElement("AnimalEventType",
                             XMLElement("ns3:Parturition",
                                XMLElement("ns3:LactationNumber", lct.lact_no)))
                        )
                      )
                 END
          FROM   cmi_lactations lct
          WHERE  lct.anm_id     = anm.id
          AND    lct.start_date > (SELECT MIN(lct2.start_date)
                                   FROM   cmi_lactations lct2
                                   WHERE  lct2.anm_id = lct.anm_id)
        ),
        -- DryOff
        (
          SELECT CASE WHEN count(*) != 0
                 THEN XMLAgg(
                        XMLElement("AnimalEvent",
                          XMLForest(lct.end_date AS "ns3:BeginDate",
                                    'INF'        AS "ns3:ActionCode"),
                          XMLElement("AnimalEventType",
                             XMLElement("ns3:DryOff"))
                        )
                      )
                 END
          FROM   cmi_lactations lct
          WHERE  lct.anm_id   = anm.id
          AND    lct.end_date > (SELECT MIN(lct2.end_date)
                                 FROM   cmi_lactations lct2
                                 WHERE  lct2.anm_id = lct.anm_id)
        ),
        -- PregnancyCheck
        (
          SELECT CASE WHEN count(*) != 0
                 THEN XMLAgg(
                        XMLElement("AnimalEvent",
                          XMLForest(prg.prgncy_chk_date AS "ns3:BeginDate",
                                    'INF'               AS "ns3:ActionCode"),
                          XMLElement("AnimalEventType",
                             XMLElement("ns3:PregnancyCheck",
                                XMLElement("ns3:PregnancyCheckResult", DECODE(prg.prgncy_ind, '1', 'F', 'E'))))
                        )
                      )
                 END
          FROM   cmi_pregnancies prg
          WHERE  prg.anm_id          = anm.id
          AND    prg.prgncy_chk_date > (SELECT MIN(prg2.prgncy_chk_date)
                                        FROM   cmi_pregnancies prg2
                                        WHERE  prg2.anm_id = prg.anm_id)
        )
      ) AS "AnimalEvents"
    )
    

    Here, the AnimalEvent tag is included directly in the subquery, before the aggregation, removing the need for deeper XMLConcat in the process.

    If the subquery returns no rows, no tag is generated through the CASE statement.

  • Selection of a function that returns a sys refcursor or alternative

    I have a query that returns a result set of three columns, namely the SSN, the PAID_YEAR and the PAID_TOTAL. This query, I can:

    1. Create a view and then question him.
    2. Create a function result set and return

    If I go the first way, that a simple request like this takes more than 20 seconds:

    SELECT PAID_YEAR,PAID_TOTAL FROM VIEW_1 WHERE SSN=12345678912882;

    I know it's because when I query a view engine first brings all the rows in the view and then filters the lines for the criteria provided.

    If I go the second way I can send only a parameter and make the engine look for only the rows that match the condition and the Recordset. But I don't know how then SELECT this result set returned. I took a look at the tables in pipeline, but do not understand how to their advantage. So my final question is if it is somehow possible to select from the result set returned by a function like this:

    SELECT * FROM FUNCTION_1(12132323232).

    If Yes, then how, if not, what would be another way?

    I know it's because when I query a view engine first brings all the rows in the view and then filters the lines for the criteria provided.

    No - you don't "know that" because it is not true. Just check out the plan of the command explain to yourself. Oracle can always use appropriate indexes so that only the needed rows are returned.

    So my final question is if it is somehow possible to select from the result set returned by a function like this:

    SELECT * FROM FUNCTION_1(12132323232).
    

    No - you can't do it like that. You must use the TABLE function to process the result of the function defined in a table:

    'SELECT * FROM TABLE(FUNCTION_1(12132323232)).

    -type to match record emp
    create or replace type emp_scalar_type as an object
    (EMPNO NUMBER 4,
    ENAME VARCHAR2 (10),
    USE VARCHAR2 (9).
    MGR NUMBER 4,
    HIREDATE DATE,
    NUMBER OF SAL (7, 2)
    NUMBER OF COMM (7, 2)
    DEPTNO NUMBER (2)
    )
    /

    -records of the emp table
    create or replace type emp_table_type to table of emp_scalar_type
    /

    -function of pipelined

    create or replace function get_emp (p_deptno number)
    return emp_table_type
    PIPELINED
    as
    TYPE EmpCurTyp IS REF CURSOR RETURN emp % ROWTYPE;
    emp_cv EmpCurTyp;
    l_rec emp % rowtype;
    Start
    Open emp_cv SELECT * from emp where deptno = p_deptno;
    loop
    extract the emp_cv in l_rec;
    When the output (emp_cv % notfound);
    line of conduct (emp_scalar_type (l_rec.empno, LOWER (l_rec.ename),
    l_rec.job, l_rec.mgr, l_rec.hiredate, l_rec.sal, l_rec.comm, l_rec.deptno));
    end loop;
    return;
    end;
    /
     
    Select * from table (get_emp (20))

  • Query to return then 7 dates

    Hello

    is it possible to return the next 7 dates just by using a query, for example, I need a query that returns:

    Select (I don't know who put here) double

    Date
    2012-10-05
    2012-10-06
    2012-10-07
    2012-10-08
    2012-10-09
    2012-10-10
    2012 10-11

    If possible, I would like to know if there is a way to pass a date and according to her, the query returns the next 7 dates based on the date... for example:

    Select (I don't know who put here) of the double if date > '' 2012-10-15

    Date
    2012 10-16
    2012 10-17
    2012 10-18
    2012 10-19
    2012 10-20
    2012 10-21
    2012 10-22

    I really appreciate any help
    Thank you
    SQL> select trunc(sysdate) + level - 1 from dual
      2  connect by level <= 7;
    
    TRUNC(SYS
    ---------
    05-OCT-12
    06-OCT-12
    07-OCT-12
    08-OCT-12
    09-OCT-12
    10-OCT-12
    11-OCT-12
    
    7 rows selected.
    
    SQL> select date '2012-10-15' + level - 1 from dual
      2  connect by level <= 7;
    
    DATE'2012
    ---------
    15-OCT-12
    16-OCT-12
    17-OCT-12
    18-OCT-12
    19-OCT-12
    20-OCT-12
    21-OCT-12
    
    7 rows selected.
    
  • We can find the size of the query that is extracted in the selection operation?

    Hello

    We organize a query select and found that the query is retriving records (rows) 953368. Now is it possible to calculate what this query in terms of size. ??

    Could you pls suggest me.

    Thank you

    Srini...

    >
    We organize a query select and found that the query is retriving records (rows) 953368
    >
    Yes, but how many different tables are involved? The number of columns? What are the data types of the columns?
    If I run a query that returns a single line, it could have 900 columns defined as CHAR (4000), or a column defined as char (1).

    Length of line is therefore somewhere between rows 1 million times 1 byte and 3.6 MB.

    I'm guessing 1 million bytes minimum and 3.6 GB on the high side.

  • Query Union returns the correct data x 2

    I have a union query that returns 2 identical lists of data. The data are correct, but I can't work on why it is duplicated. Can anyone help? Thank you.

    John
    ****************************
    set linesize 400
    set pagesize 9999
    NewPage Set 1
    wrap off Set
    break off Set
    Set of feedback on

    set the file name = CATS_TURBO_all_LiveT

    coil & filename

    Noprint today new_value DATEVAR COLUMN
    COLUMN case_id FORM 999999 TITLE 'fault '.
    COLUMN product_name FORMAT a10 HEADING 'node '.
    COLUMN priority_type_name format a4 POSITION "PRI."
    Report_date FORMAT of the COLUMN entitled date 'Date of Recvd'
    Restore_date FORMAT of the COLUMN date TOPIC "restored".
    Close_date FORMAT of the COLUMN date POSITION "closed".
    COLUMN assigned_tech_id FORMAT a10 TITLE "GENIUS".
    COLUMN status_type_name FORMAT a3 DIRECTION "FS".
    Header of COLUMN name format a4 'Type '.
    Header of COLUMN Champ_1 format a20 'Exchange '.
    COLUMN sla_name format a12 direction "platform".
    Format COLUMN header generic a4 "Gen."
    COLUMN question_text format a90 titled "Error Description".
    COLUMN note_text a90 format called 'progress '.

    TTITLE left 'report ' -.
    Center ' TURBO all flaws (& filename... SQL) "-"
    Jump STRAIGHT DATEVAR-
    Left ' wait > > ' jump 1

    Select distinct
    a.case_id "Fault."
    c.product_name
    , decode (f.priority_type_name, 'Low', '4', 'Medium', '3', 'High', '2', 'Urgent', '1') Pri
    , to_char (a.report_date, ' DD/MM/YYYY HH24') "received".
    , to_char (' a.restore_date, ' DD/MM/YYYY HH24 ")"restored ".
    , to_char (a.close_date, ' DD/MM/YYYY HH24') 'closed '.
    , decode (d.name, 'Software', from ", 'Hardware', 'H', 'Firmware', 'F', 'Unknown', 'U', 'General', 'Gen', 'Doc', 'Doc', 'n' and"No. Fault"") 'Type '.
    , decode (b.status_type_name, ' closed - resolved ', '02', 'Pending', '40', 'On Hold - waiting for approval', '41 ', 'pending', '41', 'Wait-Service restored', ' 41', 'lack of training', '44', 'Telesys survey fault', '44') FS
    g.field_1
    e.sla_name
    , decode (a.assigned_tech_id, '10', 'JSMITH', '09', 'MBROWN', '07', 'SCHATTER', '08', 'RSMEED' ', '12', ' SCHOYM') 'B '.
    , to_char (sysdate, 'DD-MON-YYYY HH24') today
    , to_char (translate (dbms_lob.substr(a.question_text,100), CHR (13) |)) CHR (10), CHR)
    question_text 32)))
    , null "Note".
    of oracledb.incident one
    oracledb.status_type b
    c oracledb.item_type
    oracledb.problem_type d
    e oracledb.sla
    oracledb.priority_type f
    oracledb.item g
    oracledb.incident_note h
    where (a.status_type_id = b.status_type_id)
    and (a.product_id = c.product_id)
    and (a.problem_type_id = d.problem_type_id)
    and (a.sla_id = e.sla_id)
    and (a.priority_type_id = f.priority_type_id)
    and (a.product_id = g.product_id)
    and e.sla_name = "TURBO PÉI"
    and g.field_1 not in the (' SysCntl-1', ' SysCntl-2', "TURBO PÉI" "")
    and a.case_id = h.case_id (+)
    and not exists (select case_id from k oracledb.incident_note where k.case_id = a.case_id)
    and a.deleted <>' 1'
    Union
    Select distinct
    a.case_id "Fault."
    c.product_name
    , decode (f.priority_type_name, 'Low', '4', 'Medium', '3', 'High', '2', 'Urgent', '1') Pri
    , to_char (a.report_date, ' DD/MM/YYYY HH24') "received".
    , to_char (' a.restore_date, ' DD/MM/YYYY HH24 ")"restored ".
    , to_char (a.close_date, ' DD/MM/YYYY HH24') 'closed '.
    , decode (d.name, 'Software', from ", 'Hardware', 'H', 'Firmware', 'F', 'Unknown', 'U', 'General', 'Gen', 'Doc', 'Doc', 'n' and"No. Fault"") 'Type '.
    , decode (b.status_type_name, ' closed - resolved ', '02', 'Pending', '40', 'On Hold - waiting for approval', '41 ', 'pending', '41', 'Wait-Service restored', ' 41', 'lack of training', '44', 'Telesys survey fault', '44') FS
    g.field_1
    e.sla_name
    , decode (a.assigned_tech_id, '10', 'JSMITH', '09', 'MBROWN', '07', 'SCHATTER', '08', 'RSMEED' ', '12', ' SCHOYM') 'B '.
    , to_char (sysdate, 'DD-MON-YYYY HH24') today
    , to_char (translate (dbms_lob.substr(a.question_text,100), CHR (13) |)) Chr (10), Chr (32))) question_text
    , to_char (translate (dbms_lob.substr(h.note_text,100), CHR (13) |)) Chr (10), Chr (32))) note_text
    of oracledb.incident one
    oracledb.status_type b
    c oracledb.item_type
    oracledb.problem_type d
    e oracledb.sla
    oracledb.priority_type f
    oracledb.item g
    oracledb.incident_note h
    where (a.status_type_id = b.status_type_id)
    and (a.product_id = c.product_id)
    and (a.problem_type_id = d.problem_type_id)
    and (a.sla_id = e.sla_id)
    and (a.priority_type_id = f.priority_type_id)
    and (a.product_id = g.product_id)
    and e.sla_name = "TURBO PÉI"
    and g.field_1 not in the (' SysCntl-1', ' SysCntl-2', "TURBO PÉI" "")
    and a.case_id = h.case_id
    and h.note_id = (select max (i.note_id)
    oracledb.incident_note I have
    where a.case_id = i.case_id)
    and a.deleted <>' 1'
    order by 1;
    /
    SPOOL OFF
    Clear jump
    output

    Published by: user13388553 on 11/24/2010 14:59

    If your code is that you run in the production data splits because you're runnign queries twice.

    from oracledb.incident_note i
    where a.case_id = i.case_id)
    and a.deleted '1'
    order by 1;
    /
    SPOOL OFF
    

    The semicolon after that command by 1 causes the statement to be executed, the slash on the next line causes the reexecute being. Delete one of them.

    John

  • How to write a query to return rows with the varchar column that contains even a single occurrence of the characters, such as Ÿ and

    How to write a query to return rows with the varchar column that contains even a single occurrence of the characters, such as Ÿ and

    I have a table whose columns with values such as

    MINNEAŸPOLIS and ¿VV ¿A

    Only the characters that are allowed in this column are alphabets, numbers, spaces, points and supports.

    Please help to write a SQL SELECT with Regexp_like query or any other option.

    Thanks to you all! Under query worked for me. Thank you Frank to explain the concept of hooks inside regexp_like.

    SELECT * FROM testspecial, WHERE REGEXP_LIKE (sampletext, "[^] ^ A - Z ^ a - z ^ 0-9 ^ [^.]") ^ {^} ^]') ;

Maybe you are looking for

  • Satellite R600-U10 - how to install a fresh Windows?

    HARD drive died and lost the CD of restoration. I tried a fresh install of Windows XP from a standard CD and I get some funny messages during installation that the HD is not recognized and also the CD and battery, etc. When the computer restarts afte

  • Velocity of myRio accelerometer

    Hello! I need to take "on a car' of MyRio acceleremoter. Theoretically, it seems easy. I need to integrate the signal to acceleremote to get my speed. But I do not know what block of integration I use, because I have problems with the date type. Plea

  • How can I get Age of Empires 2: The Conquerors expansion to work on Windows 7?

    When I try to run the game, the screen goes black, then a window pops up saying that the program does not work. I can play Age of Empires 2, but not the extension, it is meaningless to me. Please help if you know how to solve this problem.

  • I installed kies, but could not get my phone must be recognized in Windows XP.

    X / P with a samsung ace I am running windows X / House P. I just got a samsung ace, I loaded kies but I can't get my phone to work with it, all I get is Ref to object 'fatal error' not set to an instance of an object. If anyone can help.

  • is open source j2me or what?

    Hey,. I don't understand, isn't supposed to be gnu Java? I can't find any source for what I like. Should we change our slogin to Goodbye cruel world. instead of Hello World? Well here is my problem like any other creative person out there... can not