使用 GROUP_CONCAT 和 BIND 生成单个语句
Using GROUP_CONCAT and BIND to produce single statement
我正在尝试弄清楚 SPARQL 的 GROUP_CONCAT
函数是如何工作的。
这是我正在处理的数据:
http://api.aucklandmuseum.com/id/humanhistory/object/1203
此对象具有三个 P43_has_dimension
属性,每个属性都有自己的值和类型(1, 2, 3:它的直径为 52 毫米,高度为 77 毫米,深度为 15 毫米)。
我要返回的是“52mm(直径)”; “77mm(高度)”; “15 毫米(深度)”。换句话说,括号中的每个值及其对应的类型。
在浏览了这里的许多其他问题后,我尝试使用 the following SPARQL query,但收效甚微:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX tdwg: <http://rs.tdwg.org/ontology/voc/Specimen#>
PREFIX am: <http://collections.aucklandmuseum.com/ontology/core/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?measure ?size (GROUP_CONCAT(?dimension ; separator='; ') AS ?dimensions)
WHERE
{ { <http://api.aucklandmuseum.com/id/humanhistory/object/1203> ecrm:P43_has_dimension/ecrm:P90_has_value ?size }
UNION
{ <http://api.aucklandmuseum.com/id/humanhistory/object/1203> ecrm:P43_has_dimension/ecrm:P2_has_type ?measure }
BIND(coalesce(?size, "") AS ?size1)
BIND(coalesce(?measure, "") AS ?measure1)
BIND(concat(?size1, " (", ?measure1, ")") AS ?dimension)
}
GROUP BY ?measure ?size ?dimension
这 returns 6 个结果,每个 measure
和 size
在其各自的列中,但其中 none 合并在 dimension
列中.我将不胜感激任何有助于我出错的地方的帮助或链接。谢谢。
部分问题在于您对错误的功能进行了分组。您希望为每个单独的主题组合不同的维度类型和值。因此该主题需要在您的 GROUP_BY
中。在您的原始查询中,只有一个主题 (<http://api.aucklandmuseum.com/id/humanhistory/object/1203>
),但我们需要将其作为变量提供,以便能够对其进行分组。
此外,我认为您不需要联合和大多数附加绑定。假设每个主题的维度总是有一个类型和一个值,您可以简单地这样做:
SELECT (GROUP_CONCAT(?dim) as ?dimensions)
WHERE
{
VALUES ?subject { <http://api.aucklandmuseum.com/id/humanhistory/object/1203> }
?subject ecrm:P43_has_dimension [ ecrm:P90_has_value ?size ;
ecrm:P2_has_type ?measure ].
BIND(concat(?size, " (", ?measure, ")") AS ?dim)
} GROUP BY ?subject
当然,如果一个维度不总是有大小和度量的情况,您可以使用 OPTIONAL 并再次将合并构造放回原处。
我正在尝试弄清楚 SPARQL 的 GROUP_CONCAT
函数是如何工作的。
这是我正在处理的数据: http://api.aucklandmuseum.com/id/humanhistory/object/1203
此对象具有三个 P43_has_dimension
属性,每个属性都有自己的值和类型(1, 2, 3:它的直径为 52 毫米,高度为 77 毫米,深度为 15 毫米)。
我要返回的是“52mm(直径)”; “77mm(高度)”; “15 毫米(深度)”。换句话说,括号中的每个值及其对应的类型。
在浏览了这里的许多其他问题后,我尝试使用 the following SPARQL query,但收效甚微:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX tdwg: <http://rs.tdwg.org/ontology/voc/Specimen#>
PREFIX am: <http://collections.aucklandmuseum.com/ontology/core/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?measure ?size (GROUP_CONCAT(?dimension ; separator='; ') AS ?dimensions)
WHERE
{ { <http://api.aucklandmuseum.com/id/humanhistory/object/1203> ecrm:P43_has_dimension/ecrm:P90_has_value ?size }
UNION
{ <http://api.aucklandmuseum.com/id/humanhistory/object/1203> ecrm:P43_has_dimension/ecrm:P2_has_type ?measure }
BIND(coalesce(?size, "") AS ?size1)
BIND(coalesce(?measure, "") AS ?measure1)
BIND(concat(?size1, " (", ?measure1, ")") AS ?dimension)
}
GROUP BY ?measure ?size ?dimension
这 returns 6 个结果,每个 measure
和 size
在其各自的列中,但其中 none 合并在 dimension
列中.我将不胜感激任何有助于我出错的地方的帮助或链接。谢谢。
部分问题在于您对错误的功能进行了分组。您希望为每个单独的主题组合不同的维度类型和值。因此该主题需要在您的 GROUP_BY
中。在您的原始查询中,只有一个主题 (<http://api.aucklandmuseum.com/id/humanhistory/object/1203>
),但我们需要将其作为变量提供,以便能够对其进行分组。
此外,我认为您不需要联合和大多数附加绑定。假设每个主题的维度总是有一个类型和一个值,您可以简单地这样做:
SELECT (GROUP_CONCAT(?dim) as ?dimensions)
WHERE
{
VALUES ?subject { <http://api.aucklandmuseum.com/id/humanhistory/object/1203> }
?subject ecrm:P43_has_dimension [ ecrm:P90_has_value ?size ;
ecrm:P2_has_type ?measure ].
BIND(concat(?size, " (", ?measure, ")") AS ?dim)
} GROUP BY ?subject
当然,如果一个维度不总是有大小和度量的情况,您可以使用 OPTIONAL 并再次将合并构造放回原处。