Retrieve Distinct Values using XQuery

The following query is returning me duplicate rows. How can we retrieve the distinct values? Can we use Distinct somewhere in this query? Please help me.
SELECT XMLQuery('<Update>
{ for $demo in (ora:view("TableA")),
$demo_audit in ora:view("TableA_AUDIT")
let $demo_id := $demo/ROW/ID/text(),
$demo_audit_trans_date := $demo_audit/ROW/DATE/text(),
$demo_audit_id := $demo_audit/ROW/ID/text(),
$demo_audit_type := $demo_audit/ROW/TYPE/text()
where $demo_id = $demo_audit_id and
$demo_audit_type = "U"
return
<result>
<type>U</type>
<id>{$demo_id}</id>
</result>}</Data>' RETURNING CONTENT)
FROM dual;

Geoff,
I tried distinct-values in both let and return; however the result isn't distinct. Is the usage correct?
SELECT XMLQuery('<Update>
{for   $a in ora:view("EMP")
       let   $a_empno         := distinct-values($a/ROW/EMPNO/text()),
             $a_ename         := $a/ROW/ENAME/text(),
             $a_job           := $a/ROW/JOB/text(),
             $a_mgr           := $a/ROW/MGR/text(),
             $a_deptno        := distinct-values($a/ROW/DEPTNO/text())
       return
       <op>
             <empno>{distinct-values($a_empno)}</empno>
<name>{$a_ename}</name>
<deptno>{distinct-values($a_deptno)}</deptno>
</op>}
</Update>'
RETURNING CONTENT)
FROM dual;
The output generated is given below:
<Update>
<op>
<empno>1</empno>
<name>Henry</name>
<deptno>10</deptno>
</op>
<op>
<empno>1</empno>
<name>Henry1</name>
<deptno>10</deptno>
</op>
</Update>

Similar Messages

  • XQuery: Retrieve Distinct Values

    The following query is returning me duplicate rows. How can we retrieve the distinct values? Can we use Distinct somewhere in this query? Please help me.
    SELECT XMLQuery('<Update>
    { for $demo in (ora:view("TableA")),
    $demo_audit in ora:view("TableA_AUDIT")
    let $demo_id := $demo/ROW/ID/text(),
    $demo_audit_trans_date := $demo_audit/ROW/DATE/text(),
    $demo_audit_id := $demo_audit/ROW/ID/text(),
    $demo_audit_type := $demo_audit/ROW/TYPE/text()
    where $demo_id = $demo_audit_id and
    $demo_audit_type = "U"
    return
    <result>
    <type>U</type>
    <id>{$demo_id}</id>
    </result>}</Data>' RETURNING CONTENT)
    FROM dual;

    Playing the devil here...
    SELECT distinct XMLQuery...

  • Distinct-values() in XQuery

    According Q4, 1.1.9.4 in XML Query User Case and Oracle magazine (May2003), the function should be all right. Why the following XQuery can not be excuted in command line?
    Error message:
    XQE: unknown function 'distinct-values'
    Thanks a lot!
    <dataroot>
    let $a := document("Orders.xml")/dataroot/Orders
    for $o in distinct-values($a/CusomerID),
    $c in document("Customers.xml")/dataroot/Customers
    where $c/CustomerID = $o/CustomerID
    return
    <Customer>
    { $c/CustomerID }
    { $c/CustomerName }
    </Customer>
    </dataroot>

    Hi,
    Please let me know where It went wrong.The function's return value is declared as a string but you're actually returning sequences of string literals.
    One way to achieve that is using string-join function :
    (here for testing purpose, I used an external $doc variable)
    declare namespace xf = "http://tempuri.org/abcorg/Transformations/Distinct_Fruits/";
    declare default element namespace "http://www.abc.org.com/FormDetails";
    declare function xf:Distinct_Fruits( $fruits as element(Fruits)* )
    as xs:string
    string-join(
       for $d in distinct-values($fruits/fruit/@name)
       return concat($d, " = ", $fruits/fruit[@name=$d][1]/@color)
    , "|"
    xf:Distinct_Fruits($doc/Main/Fruits)Note that I assumed that a fruit with a given name will always have the same color.
    If that's not the case and you might have something like :
    <Main>
    <abcd first="1" second="2"/>
    <Fruits>
      <fruit name="banana" color="green"/>
      <fruit name="apple" color="red"/>
      <fruit name="grape" color="green"/>
      <fruit name="apple" color="red"/>
      <!-- here is a red banana! -->
      <fruit name="banana" color="red"/>
      <fruit name="apple" color="red"/>
      <fruit name="grape" color="green"/>
      <fruit name="orange" color="orange"/>
      <fruit name ="lemon" color="yellow"/>
    </Fruits>
    </Main>then the solution is to use deep-equal function to test if two nodes are identical (as per their values, attributes and children) :
    declare function xf:Distinct_Fruits( $fruits as element(Fruits) )
    as xs:string
    string-join(
       for $i in $fruits/fruit
       let $f := $i[not(some $x in $i/preceding-sibling::fruit satisfies fn:deep-equal($x,.))]
       where ($f)
       return concat($f/@name, " = ", $f/@color)
    , "|"
    };which gives :
    banana = green|apple = red|grape = green|banana = red|orange = orange|lemon = yellowEdited by: odie_63 on 12 juil. 2011 14:43 - changed first example to reflect sample modification

  • How to group similar values using XQuery

    I have a table Employee with following columns
    EmpNo number,
    Title varchar2(100),
    First_Name varchar2(100),
    Last_Name varchar2(100),
    1, Miss, A, B (Sample record in Employee Table)
    There is an audit table Employee_A with columns
    ID number (PK),
    EmpNo number,
    Field_Name varchar2(100),
    Old_Value varchar2(100),
    New_Value varchar2(100)
    An update statement causes the Title of EmpNo=1 to change from Miss to Mrs and Last_Name from B to C. Hence the audit table has two records
    1,1,Title, Miss,Mrs
    2,1,Last_Name, B, C
    I need to generate an xml which is as follows:
    <Tag>
    <Operation>Update</Operation>
    <Key>1</Key>
    <Data>
    <Field>
    <Column>Title</Column>
    <NewValue>Mrs</NewValue>
    </Field>
    <Field>
    <Column>Last_Name</Column>
    <NewValue>C</NewValue>
    </Field>
    <Data>
    </Tag>
    How can we do this using XQuery?
    If not, can we do this using xmlelement, xmlagg?

    I tried the following:
    SELECT XMLQuery('<Dummy>
    {for $c in ora:view("Employee"),
               $ca in ora:view("Employee_A")
           let $emp := $c/ROW/EMPNO/text(),
               $emp_a := $ca/ROW/EMPNO/text(),
               $field := $ca/ROW/FIELD_NAME/text(),
               $new := $ca/ROW/NEW_VALUE/text()
               where $emp = $emp_a
           return
           <Tag>
             <operation>Update</operation>
             <key_id>$emp_a</key_id>
             <Data>
             <Field>
               <Column>{$field}</ent_id>
    <NewValue>{$new}</NewValue>
    </Field>
    </Data>
    </Tag>
    }</Dummy' RETURNING CONTENT)
    FROM dual;
    The output that I'm getting is
    <Dummy
    <Tag>
    <Operation>Update</Operation>
    <Key>1</Key>
    <Data>
    <Field>
    <Column>Title</Column>
    <NewValue>Mrs</NewValue>
    </Field>
    <Data>
    </Tag>
    <Tag>
    <Operation>Update</Operation>
    <Key>1</Key>
    <Data>
    <Field>
    <Column>Last_Name</Column>
    <NewValue>C</NewValue>
    </Field>
    <Data>
    </Tag>
    </Dummy>
    Two <Tag></Tag> gets created - one for each update entry. Please help

  • Retrieving the value using formatted search

    Hi everyone. I have a question regarding on how I can get the value of the user-defined field in the title of a marketing document. I would like to get the value using formatted search and sql query.
    Here's the format I am currently using.
    $[$ <Item>.<Pane>.<Variable>]
    Is my format correct? Thanks.
    Regards,
    Omann

    Hi Omann,
    You can refer to fields in an entry screen using the syntax
    $[Table name.Field name]
    The table name is the name of the table belonging to the entry screen, for example, OINV for the A/R invoice entry screen.
    You can also use the fieldu2019s item number and fieldu2019s column number to refer to a field on the entry screen. By doing this, the query applies to all document entry screens. The syntax is then
    $[$Fieldu2019s item number.Fieldu2019s column number.NUMBER/CURRENCY/DATE/0]
    The system is able to uniquely identify each field of a document using the fieldu2019s index number and fieldu2019s column number. If you have activated the debug information under View  Debug Information, the system displays the fieldu2019s item number and the fieldu2019s column number in the status bar.
    You use the NUMBER parameter if the field concerned contains an amount and a currency key, and you want to extract the amount only. 
    You use the CURRENCY parameter if the field concerned contains an amount and a currency key, and you want to extract only the currency key.
    You use the DATE parameter if the field concerned is a date field and you want to use it for calculations.
    Regards, Yatsea

  • Select nested WMS layers with specific SRS value using XQuery

    A WMS getcapabilities document contains a nested list of Layers. I need to get a list of layers for which is true: - It is queryable and - It has an SRS value of EPSG:28992 or any of it's parent layers (or parents parents etc) has an SRS value of EPSG:28992.
    I came up with this query which I'm not very happy with because it only allows for three nested Layers.
    select t.*
            from xmltable(xmlnamespaces('http://www.w3.org/1999/xlink' as "xlink")
                          ,'for $d in /WMT_MS_Capabilities/Capability/Layer[SRS="EPSG:28992"]
                            where $d/@queryable="1"  return $d'
                          passing p_xml columns name varchar2(100) path 'Name'
                          ,title varchar2(100) path 'Title'
                          ,url varchar2(4000) path 'Style[1]/LegendURL/OnlineResource/@xlink:href'
                          ,style xmltype path 'Style') as t
          union all
                select t.*
                     from xmltable(xmlnamespaces('http://www.w3.org/1999/xlink' as "xlink")
                                ,'for $d in /WMT_MS_Capabilities/Capability/Layer/Layer
                                  where $d/@queryable="1"
                                    and (/WMT_MS_Capabilities/Capability/Layer/SRS="EPSG:28992"
                                         or /WMT_MS_Capabilities/Capability/Layer/Layer/SRS="EPSG:28992") return $d'
                                passing p_xml columns name varchar2(100) path 'Name'
                                ,title varchar2(100) path 'Title'
                                ,url varchar2(4000) path 'Style[1]/LegendURL/OnlineResource/@xlink:href'
                                ,style xmltype path 'Style') as t
          union all
                   select t.*
                      from xmltable(xmlnamespaces('http://www.w3.org/1999/xlink' as "xlink")
                                ,'for $d in /WMT_MS_Capabilities/Capability/Layer/Layer/Layer
                                  where $d/@queryable="1"
                                    and (/WMT_MS_Capabilities/Capability/Layer/SRS="EPSG:28992"
                                         or /WMT_MS_Capabilities/Capability/Layer/Layer/SRS="EPSG:28992"
                                         or /WMT_MS_Capabilities/Capability/Layer/Layer/Layer/SRS="EPSG:28992") return $d'
                                passing p_xml columns name varchar2(100) path 'Name'
                                ,title varchar2(100) path 'Title'
                                ,url varchar2(4000) path 'Style[1]/LegendURL/OnlineResource/@xlink:href'
                                ,style xmltype path 'Style') as t;I'm wondering if there is a better approach using Oracle 10.2.05.

    A couple of options (both quite slow unfortunately) :
    XQuery recursive function :
    select x.*
    from tmp_xml t
       , xmltable(
           xmlnamespaces('http://www.w3.org/1999/xlink' as "xlink")
         , 'declare function local:getLayer($e as element(Layer)*) as element(Layer)*
              for $i in $e
              where $i/SRS = "EPSG:4269"
              return $e[@queryable="1"] | local:getLayer($i/Layer)
            local:getLayer( /WMT_MS_Capabilities/Capability/Layer[SRS="EPSG:4269"] )'
           passing t.object_value
           columns name  varchar2(100)  path 'Name'
                 , title varchar2(100)  path 'Title'
                 , url   varchar2(4000) path 'Style[1]/LegendURL/OnlineResource/@xlink:href'
                 , style xmltype        path 'Style'
         ) x
    Ancestor axis :
    select x.*
    from tmp_xml t
       , xmltable(
           xmlnamespaces('http://www.w3.org/1999/xlink' as "xlink")
         , 'for $i in /WMT_MS_Capabilities/Capability/descendant::Layer
            where $i/@queryable = "1"
            and exists($i/ancestor-or-self::Layer[SRS="EPSG:4269"])
            return $i'
           passing t.object_value
           columns name  varchar2(100)  path 'Name'
                 , title varchar2(100)  path 'Title'
                 , url   varchar2(4000) path 'Style[1]/LegendURL/OnlineResource/@xlink:href'
                 , style xmltype        path 'Style'
         ) x
    ;Tested on a modified version of this document (stored in an XMLType table) :
    http://wms.ess-ws.nrcan.gc.ca/wms/toporama_en?VERSION=1.1.1&request=GetCapabilities&service=wms

  • Retrieving time value using getTimestamp

    Hi, I like to retrieve date fields from Oracle9i database using getTimestamp. This works fine if the Date field has a date in it, or is null. However , if I try to retrieve a date field which only has a time component , say it is being used to store someones start time for their job (:-((((, then it fails with a format exception.
    I can obviously use some other get , but why shouldn't I be able to retrieve a pure time value from a date using getTimestamp (0900 AM).
    Is there any other way which would let me retrieve this using getTimestamp. I would like to keep all my date/times get and sets from the database as Timestamps.
    Thanks in anticipation , Colin C
    Exception was .... java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff; nested exception is:
         java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff

    Hi Colin,
    I just answered this very same question you asked in Oracle's Technet forum. I will explicitly state what I implicitly hinted at in that response.
    If you supply details of your platform/environment, plus the full error message and stack trace you are getting, as well as the part of your code that you believe to be causing the problem, I may be able to help you resolve it.
    Good Luck,
    Avi.

  • How to retrieve DB values using select-options

    Hi all,
            I have problem to run this code for getting select option values in runtime.  Is there any function, methods or structure to know this?  I didnot get DB values when I click select-option button.
            I would like to display DB field values when i click select-option pushbutton.  select-option values while programs working in runtime.
    Example Code:
    REPORT  ZBAPUSHBUTTON.
    data: lryrctno type ZLRYRCT-ZEBELN,
          LDRIVER_FN type ZLRYRCT-DRIVER_FN,
          LZDELDT TYPE ZLRYRCT-ZDELDT,
          LZLRYNO TYPE ZLRYRCT-ZLRYNO,
          LZDRIVERID type zlryrct-ZDRIVERID.
    DATA: LRYRCT TYPE ZLRYRCT.
    SELECTION-SCREEN PUSHBUTTON 10(10) LB1 USER-COMMAND PB1.
    SELECTION-SCREEN PUSHBUTTON 25(10) LB2 USER-COMMAND PB2.
    SELECTION-SCREEN BEGIN OF SCREEN 100 TITLE T1.
    PARAMETERS: PLRYCTNO like lryrctno,
              PDR_FN LIKE LDRIVER_FN,
              PZDELDT LIKE LZDELDT,
              PZLRYNO LIKE LZLRYNO.
    SELECTION-SCREEN END OF SCREEN 100.
    SELECTION-SCREEN BEGIN OF SCREEN 200 TITLE T2.
    select-options: slryctno for lryrctno,
                    sdr_fn for ldriver_fn,
                    szdeldt for lzdeldt,
                    szlryno for lzlryno,
                    pzdrid for lzdriverid no intervals.
    SELECTION-SCREEN END OF SCREEN 200.
    INITIALIZATION.
    T1 = 'SELECT CHECK BOX AND RADIO BUTTON'.
    T2 = 'SELECT PARAMETERS'.
    LB1 = 'PARAMETER'.
    LB2 = 'SELECT-OPTION'.
    AT SELECTION-SCREEN.
    CASE SY-UCOMM.
    WHEN 'PB1'.
    CALL SELECTION-SCREEN 100.
    select single * from zlryrct into lryrct where zebeln = plryctno.
        if sy-subrc = 0.
          message 'primary key already exists. enter different lorry rect. no.' type 'W'.
        endif.
        LRYRCT-ZEBELN    = PLRYCTNO.
        LRYRCT-DRIVER_FN = PDR_FN .
        LRYRCT-ZDELDT    = PZDELDT.
        LRYRCT-ZLRYNO    = PZLRYNO.
        INSERT INTO ZLRYRCT VALUES LRYRCT.
        if not sy-subrc = 0.
          MESSAGE 'Insert not possible' TYPE 'W' .
        endif.
    WHEN 'PB2'.
    CALL SELECTION-SCREEN 200.
        select * from zlryrct into lryrct where
                 zebeln in slryctno and
                 driver_fn in sdr_fn and
                 zdeldt in szdeldt and
                 zlryno in szlryno.
          write: / lryrct-zebeln, LRYRCT-DRIVER_FN, LRYRCT-ZDELDT, LRYRCT-ZDELDT, LRYRCT-ZLRYNO .
        endselect.
    ENDCASE.

    What you are looking for is called "search help" (SE11 transaction), you may reuse an existing one (it must be attached to data element, or structure component as foreign key, etc.) Please look at SAP documentation and forums. You may also program it yourself by using AT SELECTION-SCREEN ON VALUE-REQUEST FOR ...

  • BUG: retrieving NCHAR value using ViewObject returns "???" with OCI driver

    Hi,
    after upgrading JDeveloper to 10.1.3.1 we cannot use OCI JDBC drivers, because they don't work correctly - characters, that do not exist in database character set (EE8ISO8859P2 in our case) are converted to "?". Thin driver works well. We need to use OCI, because we had some issues with RAC with thin driver.
    Test case is easy: create table with NCHAR column, populate with unicode data, create ViewObject to select from this table and it will work only with THIN and not with OCI driver. This worked well with 10.1.3.0. When I tried to directly connect to DB and select that table using java.sql.Statement, it worked with both drivers, that's why I suspect the mechanics inside ADF BC.
    I logged a SR for this in Metalink, but try this way as well, for someone else could hit the same problem.
    Thanks for any help,
    Viliam

    Hi,
    after upgrading JDeveloper to 10.1.3.1 we cannot use OCI JDBC drivers, because they don't work correctly - characters, that do not exist in database character set (EE8ISO8859P2 in our case) are converted to "?". Thin driver works well. We need to use OCI, because we had some issues with RAC with thin driver.
    Test case is easy: create table with NCHAR column, populate with unicode data, create ViewObject to select from this table and it will work only with THIN and not with OCI driver. This worked well with 10.1.3.0. When I tried to directly connect to DB and select that table using java.sql.Statement, it worked with both drivers, that's why I suspect the mechanics inside ADF BC.
    I logged a SR for this in Metalink, but try this way as well, for someone else could hit the same problem.
    Thanks for any help,
    Viliam

  • Distinct operation using GROUP BY

    I was doing some research on retrieving Duplicate records in a table.
    create table x
    (empid number,
    empname varchar2(50)
    insert into x values (1,'John');
    insert into x values (2,'Reynolds');
    insert into x values (3,'Harrison');
    insert into x values (1,'Kate');
    insert into x values (2,'Hans');
    SQL> select * from x;
         EMPID EMPNAME
             1 John
             2 Reynolds
             3 Harrison
             1 Kate
             2 Hans
    SQL> select empid from x group by empid;          ------ Query 1
         EMPID
             1
             2
             3        
    SQL> select min(empid) from x group by empid;      ------ Query 2
    MIN(EMPID)
             1
             2
             3
    SQL> select * from x where empid not in
      2  (select min(empid) from x group by empid);   -------- Query 3   
    no rows selectedI understand how Query 1 and Query 2 retrieves distinct values. Query 3 can't fetch duplicate values because it is equivalent to
    SQL> select * from x where empid not in
      2  (select distinct(empid) from x);
    no rows selectedAm i right?
    From various OTN threads, i gathered that a query using ROWID should be used for retrieving duplicate rows.
    Something like
    select * from x where rowid not in
    (select min(rowid) from x group by empid);   ------------- Query 4How come this query (Query 4) works and Query 3 doesn't?

    Hi,
    Y.Ramlet wrote:
    I was doing some research on retrieving Duplicate records in a table.
    create table x
    (empid number,
    empname varchar2(50)
    insert into x values (1,'John');
    insert into x values (2,'Reynolds');
    insert into x values (3,'Harrison');
    insert into x values (1,'Kate');
    insert into x values (2,'Hans');
    SQL> select * from x;
    EMPID EMPNAME
    1 John
    2 Reynolds
    3 Harrison
    1 Kate
    2 Hans
    SQL> select empid from x group by empid;          ------ Query 1
    EMPID
    1
    2
    3        
    SQL> select min(empid) from x group by empid;      ------ Query 2
    MIN(EMPID)
    1
    2
    3
    SQL> select * from x where empid not in
    2  (select min(empid) from x group by empid);   -------- Query 3   
    no rows selectedI understand how Query 1 and Query 2 retrieves distinct values. Query 3 can't fetch duplicate values because it is equivalent to
    SQL> select * from x where empid not in
    2  (select distinct(empid) from x);
    no rows selectedAm i right?Yes, you're right. Query 3 and Query 4 get the same results (or lack of results) by different ways.
    From various OTN threads, i gathered that a query using ROWID should be used for retrieving duplicate rows.
    Something like
    select * from x where rowid not in
    (select min(rowid) from x group by empid);   ------------- Query 4How come this query (Query 4) works and Query 3 doesn't?They both work; they get different results because they are doing different things.
    ROWID is unique; empid is not. When you "GROUP BY empid", any group with more than one member will have ROWIDs that are not equal to MIN (ROWID) for that group.

  • How to retrieve multilpe values stored in driectory entry

    Hi all,
    I have managed to add an entry which containing multiple values for an attribute to the OiD. I am also able to retrieve those values using Sun's JNDI API's. The problem is when i am retrieving duplicate values in the entry. The Attributes.get() returns only the distinct values.
    The Attribute definition is
    attributetypes: ( 2.1.5.5.2.8.3.3.5.3 NAME 'employeeSpecialAllowance' DESC 'Employee Special Allowance' ORDERING numericStringMatch SYNTAX '1.3.6.1.4.1.1466.115.121.1.36' USAGE userApplications )
    The duplicate values what i mean here is, suppose if the attribute has values like
    employeeSpecialAllowance: 1000
    employeeSpecialAllowance: 2000
    employeeSpecialAllowance: 1000
    employeeSpecialAllowance: 3000
    The Attributes.getAll() retrieves only 1000,2000 and 3000. The second instance of 1000 is not retrieved. The attributes were created specifying its ordered type set to true like this
    attr = new BasicAttribute("employeeSpecialAllowance",10000,true);
    Can anybody provide me an insight into this?
    Thanks in advance,
    Cheeka

    The Attributes.get method in JNDI does not support this. That is why you are only able to fetch non-unique values. It is not a problem with OiD.

  • Distinct values as input to another query

    Can any one help me to write a program to fetch distinct values in query and then insert that distinct values as input to another query

    Fetch distinct values:
    use distinct keyword
    for insert : use the following.
    insert into <table_name> select distinct <column_names> from <table_name>;

  • Xquery to return distinct values in xml

    Hi,
    I have a requirement, where in i have to get distinct values from an xml file.
    EX: Input
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Main xmlns="http://www.abc.org.com/FormDetails">
    <abcd first="1" second="2"/>
    <Fruits>
    <fruit name="banana" color="green">
    <fruit name="apple" color="red">
    <fruit name="grape" color="green">
    <fruit name="apple" color="red">
    <fruit name="banana" color="green">
    <fruit name="apple" color="red">
    <fruit name="grape" color="green">
    <fruit name="orange" color="orange">
    <fruit name ="lemon" color="yellow">
    </Fruits>
    <Fruits>
    <fruit name="banana" color="green">
    <fruit name="apple" color="red">
    <fruit name="grape" color="green">
    <fruit name="apple" color="red">
    <fruit name="banana" color="green">
    <fruit name="apple" color="red">
    <fruit name="grape" color="green">
    <fruit name="orange" color="orange">
    <fruit name ="lemon" color="yellow">
    </Fruits>
    <Fruits>
    <fruit name="banana" color="green">
    <fruit name="apple" color="red">
    <fruit name="grape" color="green">
    <fruit name="apple" color="red">
    <fruit name="banana" color="green">
    <fruit name="apple" color="red">
    <fruit name="grape" color="green">
    <fruit name="orange" color="orange">
    <fruit name ="lemon" color="yellow">
    </Fruits>
    </Main>
    Required output: String
    banana = green|apple = red| grape=green|orange=orange|lemon=yellow
    I have used this xquery:
    declare namespace xf = "http://tempuri.org/abcorg/Transformations/Distinct_Fruits/";
    declare namespace ns = "http://www.abc.org.com/FormDetails";
    declare function xf:Distinct_Fruits($fruits as element(ns:Main))
    as xs:string{
    for $d in distinct-values($fruits//ns:fruit/@name)
    return "{$d} ="
    for $e in distinct-values($fruits//ns:fruit/@color)
    return "{$e}|"
    declare variable $fruitsas element(ns:Main) external;
    xf:Distinct_Member($fruits)
    I am getting error like:
    Error executing the XQuery transformation: line 11, column 1: {err}FORG0005: expected exactly one item, got 2+ items
    Please let me know where It went wrong.
    Thanks..
    Edited by: user12679330 on Jul 12, 2011 4:47 AM

    Hi,
    Please let me know where It went wrong.The function's return value is declared as a string but you're actually returning sequences of string literals.
    One way to achieve that is using string-join function :
    (here for testing purpose, I used an external $doc variable)
    declare namespace xf = "http://tempuri.org/abcorg/Transformations/Distinct_Fruits/";
    declare default element namespace "http://www.abc.org.com/FormDetails";
    declare function xf:Distinct_Fruits( $fruits as element(Fruits)* )
    as xs:string
    string-join(
       for $d in distinct-values($fruits/fruit/@name)
       return concat($d, " = ", $fruits/fruit[@name=$d][1]/@color)
    , "|"
    xf:Distinct_Fruits($doc/Main/Fruits)Note that I assumed that a fruit with a given name will always have the same color.
    If that's not the case and you might have something like :
    <Main>
    <abcd first="1" second="2"/>
    <Fruits>
      <fruit name="banana" color="green"/>
      <fruit name="apple" color="red"/>
      <fruit name="grape" color="green"/>
      <fruit name="apple" color="red"/>
      <!-- here is a red banana! -->
      <fruit name="banana" color="red"/>
      <fruit name="apple" color="red"/>
      <fruit name="grape" color="green"/>
      <fruit name="orange" color="orange"/>
      <fruit name ="lemon" color="yellow"/>
    </Fruits>
    </Main>then the solution is to use deep-equal function to test if two nodes are identical (as per their values, attributes and children) :
    declare function xf:Distinct_Fruits( $fruits as element(Fruits) )
    as xs:string
    string-join(
       for $i in $fruits/fruit
       let $f := $i[not(some $x in $i/preceding-sibling::fruit satisfies fn:deep-equal($x,.))]
       where ($f)
       return concat($f/@name, " = ", $f/@color)
    , "|"
    };which gives :
    banana = green|apple = red|grape = green|banana = red|orange = orange|lemon = yellowEdited by: odie_63 on 12 juil. 2011 14:43 - changed first example to reflect sample modification

  • "How to get distinct values of sharepoint column using SSRS"

    Hi,
        I have integrated sharepoint list data to SQL Server reporting services. I am using the below to query sharepoint list data using sql reporting services.
    <Query>
       <SoapAction>http://schemas.microsoft.com/sharepoint/soap/GetListItems</SoapAction>
       <Method Namespace="http://schemas.microsoft.com/sharepoint/soap/" Name="GetListItems">
          <Parameters>
             <Parameter Name="listName">
                <DefaultValue>{GUID of list}</DefaultValue>
             </Parameter>
             <Parameter Name="viewName">
                <DefaultValue>{GUID of listview}</DefaultValue>
             </Parameter>
             <Parameter Name="rowLimit">
                <DefaultValue>9999</DefaultValue>
             </Parameter>           
          </Parameters>
       </Method>  
    <ElementPath IgnoreNamespaces="True">*</ElementPath>
    </Query>
    By using this query, I am getting a dataset which includes all the columns of sharepoint list. Among these columns, I wanted to display only 2 columns (i.e Region and Sales type) using chart. I have created a Region parameter but when I click preview, the drop down box is giving me all the repeatative values of region like RG1,RG1,RG1,RG2,RG2,RG2,RG2,RG3.......... I wanted to display only distinct values of Region parameter so that whenever end user select region from the parameter drop down, it will display the respective value of Sales type column.
    Also when I select only RG1 parameter, it is giving me a chart including the sales type of all the Regions. (it should display me only the sales type of RG1) How can I link these 2 columns so that they will display the values respectively.
              I would really appreciate if anyone can help me out with this.
    Thanks,
    Sam.

    Hi Sam,
    By code, the CAML language doesn’t have any reserved word (or tag) to set this particular filter to remove duplicate results.
    In this case, we could use the custom code to get distinct records.
    Here are the detailed steps:
    1.         Create a hidden parameter that gets all the records in one field.
    Note: Please create another dataset that is same of the main dataset. This dataset is used for the parameter.
    2.         Create a function that used to remove the duplicate records.
    Here is the code:
    Public Shared Function RemoveDups(ByVal items As String) As String
    Dim noDups As New System.Collections.ArrayList()
    Dim SpStr
    SpStr = Split(items ,",")
    For i As Integer=0 To Ubound(Spstr)
    If Not noDups.Contains(SpStr(i).Trim()) Then
    noDups.Add(SpStr(i).Trim())
    End If
    Next
    Dim uniqueItems As String() = New String(noDups.Count-1){}
    noDups.CopyTo(uniqueItems)
    Return String.Join(",", uniqueItems)
    End Function
    3.         Create another parameter that will be used for filtering the maindata.
    Please set the available value to be =Split(Code.RemoveDups(JOIN(Parameters!ISSUE_STATUS_TEMP.Value, ",")), ",")
    And the default value to be the value you what such as the first value:
    =Split(Code.RemoveDups(JOIN(Parameters!ISSUE_STATUS_TEMP.Value, ",")), ",").(0)
    4.         Go to the main dataset. Open the property window of this dataset.
    5.         In the “Filters” tab, set the filter to be:
    Expression: <The field to be filter>
    Operator: =
    Value: =Parameters!Region.Value
    The parameter “Region” should be the parameter we created in the step3.
    Now, we should get distinct values of SharePoint columns.
    If there is anything unclear, please feel free to ask.
    Thanks,
    Jin
    Jin Chen - MSFT

  • Retrieve alert values for use as parameter in corrective action sql script

    I am trying to write a corrective action sql script to kill a session that is blocking other sessions. I have the "blocking session count" metric set and the alert is firing correctly.
    Is there any way to retrieve the sid and serial number from the alert generated and use it in a corrective action sql script?
    Here is the alert generated:
    Target Name=myproddb.world
    Target Type=Database Instance
    Host=myprodserver
    Metric=Blocking Session Count
    Blocking Session ID=SID: 522 Serial#: 5228
    Timestamp=Mar 4, 2008 5:57:12 PM EST
    Severity=Warning
    Message=Session 522 is blocking 1 other sessions
    Notification Rule Name=Testing Corrective actions
    Notification Rule Owner=sysman
    Clearly the sid, and serial # is contained within the alert Message field
    what I want to write for the sql script is :
    alter system kill session '%sid%,%serial_no%' immediate;
    and have GC pass in the sid and serial_no to the script.
    The "Target Properties" listed on the right of the Edit Corrective Action screen lists minimal details pertaining to the alert and certainly not the session sid, serial no.
    Generically, is there any way to retrieve the values from an alert and use them in a corrective action script or job?
    I've looked into getting the values from the mgmt$alert_history table, but I'm hoping that GC can pass the values to the sql script.
    thanks in advance for your help.

    Hi
    You can implementing a procedure like this.
    1. When a block session count alarms occurs, there is a column in the v$lock that you can examine.
    #!/bin/ksh
    #kill_block_session.sh
    #first export your variables
    export ORACLE_HOME=/oracle/product/10.2.0.3
    export ORACLE_SID=SIDNAME
    $ORACLE_HOME/bin/sqlplus "/ as sysdba" << EOF
    execute immediate killed_blocks;
    EOF
    # end
    The killed_blocks is a procedure:
    create procedure
    declare
    v_sid varchar2(15);
    v_serial varchar2(15);
    -- now a sql query that retrieve the sid and serial
    -- you can obtain these values from v$session and v$lock
    select vs.sid,vs.serial into v_sid,v_serial
    from v$session vs,v$lock vl
    where vs.sid=vl.sid
    and vl.block >0
    -- After this, you execute a dbms_put line with these
    -- values
    But you understant that this response action is very dangerous, because its possible that you kill sessions that the blocking are transitient.
    You must examine your enviroment and your application and establish the metric like UDM and not for only session blocking count.
    You must to see:
    - The type of block
    - The ctime time in the v$lock for to understatn the amount of time to determine that the block is need killed.
    - In my opinion you need a special UDM and deactivate the blocking sesion count
    If you want help to create this UDM send me a mail to [email protected]
    Regards
    Robert

Maybe you are looking for