[英]How to combine two different but correlated result sets of a Sparql query into one?
I am working on a project that finds a correlation between the voting trends in American elections to the demographic data of a state and to their respective voting preferences. 我正在做一个项目,旨在发现美国大选的投票趋势与州的人口统计数据及其各自的投票偏好之间的相关性。 I have three separate rdf documents which contain the same keys, but hold different types of data.
我有三个单独的rdf文档,它们包含相同的键,但是保存不同类型的数据。 So I tried the following SPARQL query on Fuseki server,
所以我在Fuseki服务器上尝试了以下SPARQL查询,
SELECT ?p ?o1 ?object3
WHERE {
{
?subject <http://semanticspiders.org/demographic#AZ> ?object .
?object <http://semanticspiders.org/demographic#age> ?o .
?o ?p ?o1
}
UNION
{
?subject <http://semanticspiders.org/voterTurnout#AZ> ?object .
?object <http://semanticspiders.org/voterTurnout#age> ?o .
?o ?p ?o1
}
UNION
{
?subject <http://semanticspiders.org/voterBias#age> ?object .
?object ?p ?object2 .
?object2 <http://semanticspiders.org/voterBias#left> ?object3
}
FILTER (?p = <http://semanticspiders.org/demographic#18-29> || ?p = <http://semanticspiders.org/voterBias#18-29>)
}
which gives me the following result. 这给了我以下结果。
However I want result set to look something like the following - {18-29,"21.9","5.161"} 但是,我希望结果集看起来像以下内容-{18-29,“ 21.9”,“ 5.161”}
I have tried group by and other filters but couldn't make it work. 我曾尝试使用group by和其他过滤器,但无法正常工作。 Any help is appreciated.
任何帮助表示赞赏。
UNION
. UNION
的两行。 Union is for returning rows that match the one pattern or the other pattern. {...}
around each group. {...}
在每个组中。 FILTER
. FILTER
。 If you use variables in the right way, you won't need a filter. {...}
groups has its own separate variables. {...}
组中的每个组都有自己独立的变量。 They should not share any variables. ?subject
, ?object
and ?p
are pretty terrible variable names. ?subject
, ?object
和?p
是非常糟糕的变量名。 Name your variables after the kind of thing that is going to be bound to it. ?age
or ?voterBias
make for better names. ?age
或?voterBias
可以说是更好的名字。 ?key
, or ?ageRange
as proposed in the comments. ?key
,或?ageRange
作为意见建议。 This variable needs to hold the value that you want to use to join between the groups, so 18-29
for example. 18-29
。 So it needs to receive exactly the same value in each of the three groups. BIND(... AS ?key)
. BIND(... AS ?key)
将其绑定到每个组中的变量。 While doing this, you probably want to work on each of the three groups individually, and only put them all into the query once each group works. 在执行此操作时,您可能希望分别处理这三个组,并且只有在每个组工作后才将它们全部放入查询中。 Basically, the result for each group needs to have
?key
with values that are consistent between the groups, and otherwise only have variables in the result that are not shared with another group. 基本上,每个组的结果都必须具有
?key
,且各组之间的值必须一致,否则,结果中仅具有不与另一组共享的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.