0

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:

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

Here is the type of XML data being give for trip and city

<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

4

1 回答 1

0

我最终注意到,通过使用 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>
于 2012-12-05T07:32:07.053 回答