[英]Count in XQuery giving strange results
I'm trying to count how many trips have been taken to California within two dates 我想计算两个日期内去加利福尼亚的旅行次数
I have the current XQuery that is as follows: 我有当前的XQuery如下:
for $x in doc("triptracker.xml")/data/cities/city,
$y in doc("triptracker.xml")/data/trips/trip
let $date as xs:date := xs:date($y/date)
where $x/cityName = $y/cityName and $x/state = "CA" and
$date gt xs:date("2003-01-01") and $date lt xs:date("2006-12-31")
return <Result>Trips to California: {count($y)}</Result>
Which gives me the result set 这给了我结果集
<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>
Instead of just giving me a count of 5 而不是只给我5分
Here is the type of XML data being give for trip and city 这是为旅行和城市提供的XML数据的类型
<trips>
<trip>
<ssn>123-20-4567</ssn>
<cityName>Tucson</cityName>
<date>2001-08-11</date>
</trip>
</trips>
<cities>
<city>
<cityName>Houston</cityName>
<state>TX</state>
<location>
<latitude>29.76045</latitude>
<longitude>-95.369784</longitude>
</location>
</city>
</cities>
Any help would be greatly appreciated 任何帮助将不胜感激
I ended up noticing that by using the fors, I could never get only 1 count so I revised it using let 我最后注意到,使用fors时,我永远无法获得仅1个计数,因此我使用let对其进行了修改
let $x := doc("triptracker.xml")/data/cities/city[state = "CA"],
$y := doc("triptracker.xml")/data/trips/trip[cityName=$x/cityName
and xs:date(date) gt xs:date("2003-01-01")
and xs:date(date) lt xs:date("2006-12-31")]
return <Result>Trips to California: {count($y)}</Result>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.