SPARQL - 返回主题列表的相互对象

rin*_*ahn 2 rdf sparql wikidata

如何在不知道这些主题的谓词/对象的情况下获取由主题列表共享的所有谓词+对象?
我们来看看维基数据中的这个示例查询:

SELECT ?chancellor WHERE{
    ?chancellor wdt:P39 wd:Q4970706. #P39 = position held, Q4970706 = Chancellor of Germany
}
Run Code Online (Sandbox Code Playgroud)

链接到此查询.

此查询返回德国所有前任总理.
现在我想要返回每个谓词+对象,每个大臣都有共同点,例如每个主题都是人类的一个实例,出生在德国等等.
我想这很简单.但是我不知道.

use*_*512 5

这个不错.这是一个接近命中:

prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>

select ?p ?o (count(distinct ?chancellor) as ?cs) where {
    ?chancellor wdt:P39 wd:Q4970706.  
    ?chancellor ?p ?o .
}
group by ?p ?o
order by desc(?cs)
Run Code Online (Sandbox Code Playgroud)

链接到查询

这需要所有的大臣,他们的属性和价值观.它计算每个prop/val的总理数.

通过命令,您可以在顶部看到最常见的道具/值.

现在你想要的只是所有大臣的结果.我们可以很容易地在一个查询中获得总理数量,并将两者结合在一起:

prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>

select ?p ?o where {
   {
       # Find number of chancellors 
       select (count(?chancellor) as ?num_chancellors) where { 
           ?chancellor wdt:P39 wd:Q4970706
       }
   }
   {
       # Find number of chancellors per predicate / value
       select ?p ?o (count(distinct ?chancellor) as ?chancellor_count) where {
           ?chancellor wdt:P39 wd:Q4970706.  
           ?chancellor ?p ?o .
       }
       group by ?p ?o 
   }
   # Choose results all chancellors share 
   filter (?num_chancellors = ?chancellor_count)
}
Run Code Online (Sandbox Code Playgroud)

链接到查询.

我认为这样做你想要的.我承认,不是很漂亮.