简体   繁体   English

如何将Sparql查询的两个不同但相关的结果集组合为一个?

[英]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. 这给了我以下结果。

Sparql结果

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. 任何帮助表示赞赏。

  1. Delete the two lines that say UNION . 删除显示UNION的两行。 Union is for returning rows that match the one pattern or the other pattern. 联合用于返回与一个模式另一模式匹配的行。 You want a row that matches the one pattern and the other pattern. 您需要与一个模式另一模式匹配的行。 That's what SPARQL does by default. 这就是SPARQL在默认情况下所做的。 Keep the curly braces {...} around each group. 将花括号{...}在每个组中。
  2. Delete the FILTER . 删除FILTER If you use variables in the right way, you won't need a filter. 如果以正确的方式使用变量,则不需要过滤器。
  3. Make sure that each of the three {...} groups has its own separate variables. 确保三个{...}组中的每个组都有自己独立的变量。 They should not share any variables. 它们不应共享任何变量。 If you share variables between groups, then they must have the same value or else the row will be removed. 如果在组之间共享变量,则它们必须具有相同的值,否则该行将被删除。
  4. By the way, ?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. 用将要绑定的变量命名变量。 So, ?age or ?voterBias make for better names. 因此, ?age?voterBias可以说是更好的名字。
  5. Make a new variable that is shared between the three groups, maybe call it ?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. 因此,它需要在三个组中的每个组中接收完全相同的值。 It looks like that value doesn't exist directly in your data, so it needs to be computed with an expression and then bound to the variable in each group, using 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM