简体   繁体   English

XQuery / XPath LIMIT总和

[英]XQuery/XPath LIMIT a sum

I am new to XQuery. 我是XQuery的新手。 I need to limit by 1 in XQuery but I'm having trouble. 我需要在XQuery中限制为1,但遇到了麻烦。

I am trying to find out the winner of a tournament by finding each players scores and summing up the scores to get the total scores. 我试图通过找到每个球员的得分并汇总得分以获得总得分来找出比赛的获胜者。 I then sort in descending order and try to limit the total score, however I am having problems with trying to limit in XQuery. 然后,我以降序排序并尝试限制总分,但是在尝试限制XQuery时遇到了问题。

Here I am wanting to get the top score but I have tried to use subsequence($sequence, $starting-item, $number-of-items) and [position()] but it seems to not be working. 在这里,我想获得最高分,但是我尝试使用subsequence($ sequence,$ starting-item,$ number-of-items)和[position()],但它似乎无法正常工作。

for $pair_ids in distinct-values(fn:doc("tourny.xml")/Competition[@date = "2012-12-12"]/Tennis/Partner/@pair_id)
let $total_scores := sum(fn:doc("tourny.xml")/Competition[@date = "2012-12-12"]/Tennis/Partner[@pair_id = $pair_ids]/@score)
order by $total_scores descending
return 
    $total_scores

the output is giving me: 输出给我:

$total_scores:
34
11
20

How can I limit the result so I only get 34 as the highest score? 如何限制结果,使我的最高分数只有34? Thanks 谢谢

You can use fn:max() function as follow: 您可以使用fn:max()函数,如下所示:

fn:max(
for $pair_ids in distinct-values(fn:doc("tourny.xml")/Competition[@date = "2012-12-12"]/Tennis/Partner/@pair_id)
let $total_scores := sum(fn:doc("tourny.xml")/Competition[@date = "2012-12-12"]/Tennis/Partner[@pair_id = $pair_ids]/@score)
order by $total_scores descending
return $total_scores
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM