[英]Global variables in XQuery and not sure how to return the average and why I am getting this error
I have been working on my 2nd simple query for a few days. 我一直在进行第二个简单查询,已有几天了。 All I am supposed to do is declare a global variable ($crimes) that points to all crime elements in the XML file and use a FLWOR query to iterate through each unique value of the day element so that I can see which days have the most crimes (it should be Saturday, Monday, and then Sunday in order).
我要做的就是声明一个全局变量($ crimes),该变量指向XML文件中的所有犯罪元素,并使用FLWOR查询来遍历day元素的每个唯一值,这样我就可以看到哪几天中最多犯罪(依次为星期六,星期一和星期日)。 I also keep getting this error:
我也不断收到此错误:
Unexpected end of query;
查询意外结束; variable "$crimes".
变量“ $ crimes”。
Code: 码:
xquery version "1.0";
delare variable $crimes := doc('dc_crime.xml')//crime;
(:
Query to display total number of crimes by
day of the week
:)
<results>{
for $day in distinct-values(doc('dc_crime.xml')/$day
let $crimes := sum($crimes/($day/$crimes))
order by $day descending order
return
<crime day="{$day}" crimes="{$count}">{$crimes}</crime>
}</results>
Sample of XML: XML样本:
<crime id="13402531">
<dateTime>2013-08-31T20:27:00</dateTime>
<month>8-Aug</month>
<day>7-Sat</day>
<offense>THEFT/OTHER</offense>
<method>OTHERS</method>
<ward>3</ward>
</crime>
<crime id="13402533">
<dateTime>2013-08-28T17:06:00</dateTime>
<month>8-Aug</month>
<day>4-Wed</day>
<offense>THEFT F/AUTO</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
<crime id="13402547">
<dateTime>2013-08-31T20:05:00</dateTime>
<month>8-Aug</month>
<day>7-Sat</day>
<offense>THEFT F/AUTO</offense>
<method>OTHERS</method>
<ward>6</ward>
</crime>
<crime id="13402704">
<dateTime>2013-08-03T17:29:00</dateTime>
<month>8-Aug</month>
<day>7-Sat</day>
<offense>THEFT/OTHER</offense>
<method>OTHERS</method>
<ward>2</ward>
</crime>
</crimes>
Please help... 请帮忙...
This is one possible XQuery that: 1. uses global variable and 2. count number of crimes per day and return the result in the order of that number, descending : 这是一种可能的XQuery: 1.使用全局变量,并且2.计算每天的犯罪数量,并按该数量的顺序返回结果,降序为:
declare variable $crimes := doc('dc_crime.xml')//crime;
<results>{
for $day in distinct-values(doc('dc_crime.xml')//day)
(: get all crimes that happen at current day of week :)
let $details := $crimes[day=$day]
(: count number crimes that happen at current day of week :)
let $count := count($details)
(: return the result in the order of $count, descending :)
order by $count descending
return
<crime day="{$day}" crimes="{$count}">{$details}</crime>
}</results>
output : 输出:
<results>
<crime crimes="3" day="7-Sat">
<crime id="13402531">
<dateTime>2013-08-31T20:27:00</dateTime>
<month>8-Aug</month>
<day>7-Sat</day>
<offense>THEFT/OTHER</offense>
<method>OTHERS</method>
<ward>3</ward>
</crime>
<crime id="13402547">
<dateTime>2013-08-31T20:05:00</dateTime>
<month>8-Aug</month>
<day>7-Sat</day>
<offense>THEFT F/AUTO</offense>
<method>OTHERS</method>
<ward>6</ward>
</crime>
<crime id="13402704">
<dateTime>2013-08-03T17:29:00</dateTime>
<month>8-Aug</month>
<day>7-Sat</day>
<offense>THEFT/OTHER</offense>
<method>OTHERS</method>
<ward>2</ward>
</crime>
</crime>
<crime crimes="1" day="4-Wed">
<crime id="13402533">
<dateTime>2013-08-28T17:06:00</dateTime>
<month>8-Aug</month>
<day>4-Wed</day>
<offense>THEFT F/AUTO</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
</crime>
</results>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.