简体   繁体   English

如何在 MarkLogic xquery 中获取具有特定值的特定记录的计数

[英]How to get count of a certain record with a specific value in MarkLogic xquery

I am trying to get count of a record for a given condition.我正在尝试计算给定条件下的记录数。

Count the number of Ancillary Price in all documents in a collection where the Ancillary Price is greater than 1000.计算辅助价格大于 1000 的集合中所有文档中辅助价格的数量。

I am trying the below XQuery for that, it gives me this below response:我正在为此尝试下面的 XQuery,它给了我以下响应:

Returned sequence of 532 items in 9007.0781 ms.在 9007.0781 毫秒内返回 532 个项目的序列。 (-361.0691 ms. compared to previous run) (-361.0691 毫秒,与之前的运行相比)

And along with it prints value of 1 in each row for all 532 rows.并随之在所有 532 行的每行中打印值 1。

However my expected result is to get the count as result: 532 in my result page.但是,我的预期结果是在我的结果页面中获得计数作为结果:532。

Can you please help to identify the XQuery issue here?您能帮忙找出这里的 XQuery 问题吗?

xquery version "1.0-ml";
for $x in collection("GTM2_Shipment")
where ($x/*:Shipment/*:Ancillary/*:QuotePrice/text() > 1000) 
return (count($x))

The structure is slightly incorrect.结构稍有不妥。

Ref: https://www.w3schools.com/xml/xquery_flwor.asp参考: https ://www.w3schools.com/xml/xquery_flwor.asp

Try:尝试:

xquery version "1.0-ml";
fn:count(
   for $x in collection("GTM2_Shipment")
   where ($x/*:Shipment/*:Ancillary/*:QuotePrice/text() > 1000) 
   return $x
)

Or simply:或者简单地说:

xquery version "1.0-ml";
fn:count(collection("GTM2_Shipment")[./*:Shipment/*:Ancillary/*:QuotePrice/text() > 1000])

However, eventually these will not scale.但是,最终这些将无法扩展。 Model your data where each shipment is in it's own document and then you can use an estimate such as:在每批货物都在其自己的文档中为您的数据建模,然后您可以使用估算值,例如:

xquery version "1.0-ml";
xdmp:estimate(cts:search(fn:count(collection("GTM2_Shipment"), {a range query to your quotePrice }))

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

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