SPARQL 查询平均持续时间

发布于 2025-01-09 15:25:05 字数 264 浏览 0 评论 0原文

如何在 SPARQL 中计算平均持续时间?我使用以 xsd:duration 表示的持续时间,例如“PT4H15M53S”^^xsd:duration 以下查询不返回任何内容:

SELECT (AVG(?Duration) AS ?avg)
    WHERE {
        ?Time        rdf:type       tl:Interval ;
                     tl:duration    ?Duration .
}

How is it possible to compute in SPARQL the average of time durations? I am using durations expressed in xsd:duration e.g., "PT4H15M53S"^^xsd:duration
The following query does not return anything:

SELECT (AVG(?Duration) AS ?avg)
    WHERE {
        ?Time        rdf:type       tl:Interval ;
                     tl:duration    ?Duration .
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2025-01-16 15:25:05

它可能不是很干净,但如果您使用正则表达式提取持续时间的组成部分,它仍然与标准 SPARQL 兼容:

SELECT * WHERE {
  BIND("P20DT14H15M53S" AS ?duration)
  BIND(IF(STRSTARTS(?duration, "-"), -1, 1) AS ?coef)
  BIND(COALESCE(xsd:integer(REPLACE(?duration, "^.*[A-Z](\\d+)D.*$", "$1")), 0) AS ?d)
  BIND(COALESCE(xsd:integer(REPLACE(?duration, "^.*T(\\d+)H.*$", "$1")), 0) AS ?h)
  BIND(COALESCE(xsd:integer(REPLACE(?duration, "^.*T(?:|.*[A-Z])(\\d+)M.*$", "$1")), 0) AS ?m)
  BIND(COALESCE(xsd:decimal(REPLACE(?duration, "^.*T(?:|.*[A-Z])(\\d*\\.?\\d*)S.*$", "$1")), 0) AS ?s)
  BIND(?coef * ((((?d * 24 + ?h) * 60) + ?m) * 60 + ?s) AS ?total)
}

这会将持续时间转换为总秒数,然后您可以在您希望的任何计算中使用它,然后将其转换为以秒为单位的持续时间。

请注意,不会考虑基于月份的组件的持续时间,因为没有为该子集定义平均值(不能有小数月份)。换句话说,这仅对 xs:dayTimeDuration 的值有意义。

It may not be very clean, but it is nonetheless compatible with standard SPARQL, if you use regular expressions to extract the components of the duration:

SELECT * WHERE {
  BIND("P20DT14H15M53S" AS ?duration)
  BIND(IF(STRSTARTS(?duration, "-"), -1, 1) AS ?coef)
  BIND(COALESCE(xsd:integer(REPLACE(?duration, "^.*[A-Z](\\d+)D.*
quot;, "$1")), 0) AS ?d)
  BIND(COALESCE(xsd:integer(REPLACE(?duration, "^.*T(\\d+)H.*
quot;, "$1")), 0) AS ?h)
  BIND(COALESCE(xsd:integer(REPLACE(?duration, "^.*T(?:|.*[A-Z])(\\d+)M.*
quot;, "$1")), 0) AS ?m)
  BIND(COALESCE(xsd:decimal(REPLACE(?duration, "^.*T(?:|.*[A-Z])(\\d*\\.?\\d*)S.*
quot;, "$1")), 0) AS ?s)
  BIND(?coef * ((((?d * 24 + ?h) * 60) + ?m) * 60 + ?s) AS ?total)
}

This converts the duration into the total number of seconds, which you can then use in any calculations you wish, and then convert that to duration in seconds.

Note that durations with month-based components will not be taken into account, since average is not defined for that subset (you cannot have fractional months). In other words, this only makes sense for values of xs:dayTimeDuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文