Hi, everyone:
  Here is my query that finds the distinct value and counts them,  you can see that I used the same sub query twice, first for finding the distinct values, second for count the distinct values,  is there a way that I can cache the sub query result and reuse it somewhere else? I think it will run faster in that way.


import module namespace  fx = "http://www.functx.com" at "functx-1.0-doc-2007-01.xq";
declare namespace business="http://www.sipo.gov.cn/XMLSchema/business";
declare namespace base="http://www.sipo.gov.cn/XMLSchema/base";

(:Iterate through the distributed values:)
for $dis_val in 
(
  (
  (:open multi database, then query the doc status field:)
  for $result in doc("50PatentDividedCreatingClause.xml")/results/result
  return db:open($result/dbName)/business:PatentDocumentAndRelated/@status
  )
  =>distinct-values()
)
return
"value : " || $dis_val || ",  count: "|| 
count(
  for $status in 
  (
    for $result in doc("50PatentDividedCreatingClause.xml")/results/result
    return db:open($result/dbName)/business:PatentDocumentAndRelated/@status
  )  
where $status = $dis_val
return $status||"")