English 中文(简体)
十三、通过质量实现
原标题:Distinct Result via xQuery

I m trying to get reviewers who review one or more books published after 2010.

for $r in doc("review.xml")//Reviews//Review,
    $b in doc("book.xml")//Books//Book
where $b/Title = $r/BookTitle
    and $b/Year > 2010
return {$r/Reviewer}

The following are both XML file.

审查:

<Reviews>
    <Review>
        <ReviewID>R1</ReviewID>
        <BookTitle>B1</BookTitle>
        <Reviewer>AAA</Reviewer>
    </Review>
    <Review>
        <ReviewID>R2</ReviewID>
        <BookTitle>B1</BookTitle>
        <Reviewer>BBB</Reviewer>
    </Review>
    <Review>
        <ReviewID>R3</ReviewID>
        <BookTitle>B2</BookTitle>
        <Reviewer>AAA</Reviewer>
    </Review>
    <Review>
        <ReviewID>R4</ReviewID>
        <BookTitle>B3</BookTitle>
        <Reviewer>AAA</Reviewer>
    </Review>
<Reviews>

书籍:

<Books>
    <Book>
        <Title>B1</Title>
        <Year>2005</Year>
    </Book>
    <Book>
        <Title>B2</Title>
        <Year>2011</Year>
    </Book>
    <Book>
        <Title>B3</Title>
        <Year>2012</Year>
    </Book>
</Books>

我用我的“质量法”获得两部AA。 我很想知道,我是否能够取得独特的成果,这意味着只有一个AAA。 我尝试了不同的价值,但不知道如何使用。 感谢您的答复!

iii 1.0

<root>
{
    for $x in distinct-values
    (
        for $r in doc("review.xml")//Reviews//Review,
            $b in doc("book.xml")//Books//Book
        where $b/Title = $r/BookTitle
            and $b/Year > 2010
        return {$r/Reviewer}
    )
    return <reviewer>{$x}</reviewer>
}
</root>
最佳回答

为了保存节点,你可以使用“组别”条款,并选择组序的第一个项目:

for $r in doc("review.xml")//Review,
    $b in doc("book.xml")//Book
let $n := $r/Reviewer
where $b/Title = $r/BookTitle
  and $b/Year > 2010
group by $n
return $r[1]/Reviewer
问题回答

The following query will give you all distint reviewer names (note that the values are atomized, which means the element nodes are removed):

distinct-values(
  for $r in doc("review.xml")//Reviews//Review,
      $b in doc("book.xml")//Books//Book
  where $b/Title = $r/BookTitle
    and $b/Year > 2010
  return $r/Reviewer
)




相关问题
XQuery, fn:id and BD eXist

Does "fn:id" function return IDREFS when it is used in a FLOWER xquery with eXists database? I can t get any example... :(

SQL Server xQuery return NULL instead of empty

So in this example I m trying to return a NULL instead of an empty nvarchar for element2 and element3. I can t seem to find an answer to this anywhere, or if it s even possible. I know I can check ....

Does QExo XQuery string-join work?

When I run string-join, I get a weird output like this: <clinic> <Name>Olive Street Pediatrics</Name> <Address>1500 Olive St, Los Angeles, CA 90015</Address> <...

XQuery: how to properly append , in for loop

So I have xml data like this: <PhoneNumber>213-512-7457</PhoneNumber> <PhoneNumber>213-512-7465</PhoneNumber> and with this XQuery: <PhoneNumberList> { for $...

Filter SQL queries on the XML column using XPath/XQuery

I m having a table with one XML column. I d like to filter out the rows where a specific attribute in the XML match a string, essentially doing a WHERE or HAVING. The table looks something like this ...

热门标签