2012-12-05 74 views
0

我想看看有多少車次已被送往加州兩個日期計數XQuery中給出奇怪的結果

我有如下當前的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> 

這給了我結果集

<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> 

而不是隻給我5

這裏的計數是個XML數據的e型所給出的行程和城市

<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> 

任何幫助,將不勝感激

回答

0

我最終注意到,通過維權,我不可能只得到1個計數所以我修改它使用讓

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> 
+0

正確。 「for」表達式的return子句對於「in」表達式的每個值都會被評估一次(或者如果存在多個「for」子句,那麼對於每個組合都會進行一次),因此在return子句中沒有辦法進行聚合 - 不需要這樣做,因爲如果你要聚合一個序列,那麼你不需要迭代序列。 –