【问题标题】:MySQL Query to XQueryMySQL 查询到 XQuery
【发布时间】:2012-11-15 02:21:44
【问题描述】:

我正在尝试将以下 MySQL Query 等效为 XQuery

SELECT S.Classification, COUNT(S.Classification) AS "No. Students", AVG(S.GPA) AS "AVG
    Class. GPA"
FROM Student S
GROUP BY S.Classification
ORDER BY S.Classification ASC;

这输出: http://i.stack.imgur.com/Iu0d4.png

我的数据库的样子:

CREATE TABLE 'Student' (
'StudentID' CHAR(9) NOT NULL,
'Classification' CHAR(10) NULL DEFAULT NULL,
'GPA` DOUBLE NULL DEFAULT NULL,
'MentorID' CHAR(9) NULL DEFAULT NULL,
'CreditHours' INT(11) NULL DEFAULT NULL,
PRIMARY KEY ('StudentID')
)

在 XML 中:

<Document>
  <Table>
    <StudentID>118784412</StudentID>
    <Classification>Sophomore</Classification>
    <GPA>3.19</GPA>
    <MentorID>201586985</MentorID>
    <CreditHours>39</CreditHours>
  </Table>
</Document>

我不确定如何在 xquery 中使用 count() 和 avg()。 我从哪里开始?任何帮助表示赞赏,谢谢。

【问题讨论】:

  • 您的 XQuery 处理器是什么? XQuery 1.0 中没有明确支持 Group by。它在 1.1 和 3.0 中,但目前支持这些规范的实现并不多。但是,有特定于实现的扩展和解决方法。如果可以使用,XSLT 2 也是 group by 的一个不错的选择。理想的解决方案将取决于这些变量。
  • XQuery 1.0。我知道我不能使用“分组依据”。
  • 您使用的是什么 xQuery 处理器?解决方案的最佳分组将是特定于实现的。

标签: mysql xml xquery xquery-sql


【解决方案1】:

您可以使用 XQuery 3.0 中定义的 group by 运算符轻松实现此目的。假设给定的 xml 在变量 $doc 中,并且数据库的每一行都在另一个 &lt;Table /&gt; 节点中,您可以这样做:

for $student in $doc/Table
let $classification := $student/Classification
group by $classification
order by $classification
return 
  <output>
    <Classification>{$classification}</Classification>
    <NoStudents>{count($student)}</NoStudents>
    <AvgGPA>{avg($student/GPA)}</AvgGPA>
  </output>

有许多(优秀的)XQuery 处理器支持 XQuery 3.0,例如BaseXeXistZorba

【讨论】:

  • 我认为我不能“分组”。我试过了,但出现错误,因此我认为我无法使用 XQuery 3.0。
  • 你用的是什么处理器?
猜你喜欢
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多