【问题标题】:Duplicated items on SPARQL query (Wikidata)SPARQL 查询中的重复项(Wikidata)
【发布时间】:2020-07-03 01:11:07
【问题描述】:

我正在尝试从 WikiData 查询中清除一些结果。例如,如果您查找 IBM,您会看到它的多个条目...我只想显示相同“wd:”项目的第一个结果。

有没有办法在这种情况下使用 FILTER 或 EXISTS?比如,如果找到 ?item 结果,继续……等等?如何处理 SPARQL sintax 中的这个示例?

我曾尝试使用“GROUP BY”来做到这一点,因为我看到一些人提到它,但它不起作用。

SELECT DISTINCT (SAMPLE (?item) AS ?item) ?itemLabel ?website ?countryLabel ?industryLabel ?headquartersLabel
WHERE  {
  
    ?item wdt:P452 ?industry ;
          wdt:P17  ?country .
          FILTER((?industry = wd:Q11661)  || 
                 (?industry = wd:Q11016)  ||
                 (?industry = wd:Q880371) ||
                 (?industry = wd:Q3966)   ||
                 (?industry = wd:Q1481411)||
                 (?industry = wd:Q1540863)||
                 (?industry = wd:Q638608))

    OPTIONAL{ ?item wdt:P856 ?website . }    # gets website
    OPTIONAL{ ?item wdt:P159 ?headquarters . } 
    SERVICE wikibase:label {
       bd:serviceParam wikibase:language "en"
    }

} GROUP BY ?item ?itemLabel ?website ?countryLabel ?industryLabel ?headquartersLabel

我也尝试过使用嵌套选择,它可以工作,但它不会返回表格的其余部分。

SELECT ?item ?itemLabel ?website ?country ?countryLabel ?industry ?industryLabel
WHERE  {
    SELECT DISTINCT ?item WHERE {
      
    ?item wdt:P452 ?industry ;
          wdt:P17  ?country .
          FILTER((?industry = wd:Q11661)  || 
                 (?industry = wd:Q11016)  ||
                 (?industry = wd:Q880371) ||
                 (?industry = wd:Q3966)   ||
                 (?industry = wd:Q1481411)||
                 (?industry = wd:Q1540863)||
                 (?industry = wd:Q638608))

    OPTIONAL{ ?item wdt:P856 ?website . }    # gets website
    
    SERVICE wikibase:label {
       bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,ar,be,bg,bn,ca,cs,da,de,el,en,es,et,fa,fi,he,hi,hu,hy,id,it,ja,jv,ko,nb,nl,eo,pa,pl,pt,ro,ru,sh,sk,sr,sv,sw,te,th,tr,uk,yue,vec,vi,zh"
    }}
} 
ORDER BY ?item

【问题讨论】:

    标签: sparql wikidata


    【解决方案1】:

    您最初方法的问题是,如果 ?item ?itemLabel ?website ?countryLabel ?industryLabel ?headquartersLabel 的组合不同,则将返回一个新行。 例如

    | wd:Q123 | Company1 | co1.com   | Tech   |
    | wd:Q123 | Company1 | co1.co.uk | Tech   |
    | wd:Q123 | Company1 | co1.com   | Pharma |
    | wd:Q123 | Company1 | co1.co.uk | Pharma |
    

    你可以做两件事: 1-返回所有行业、网站等的串联,但这会超时。它会返回类似的东西。

    | wd:Q123 | Company1 | co1.com , co1.co.uk | Tech , Pharma |
    

    2-返回每个行业、网站等的样本,可能是。

    | wd:Q123 | Company1 | co1.com | Pharma |
    

    当然,您的 Company2 可能与 Company1 共享一个或多个行业,但不是所有行业,但由于您使用样本,您可能会发现它们属于不同的行业。 这种最新的方法似乎对我有用:

    SELECT ?item ?itemLabel ?industryLabel ?countryLabel ?websiteLabel ?hqLabel
    WHERE{
    {SELECT ?item ?itemLabel
    (SAMPLE(?industry) AS ?industry) (SAMPLE(?country) AS ?country)
    (SAMPLE(?website) AS ?website) (SAMPLE(?hq) AS ?hq)
    WHERE {
      ?item wdt:P452 ?industry ;
            wdt:P17  ?country .
      
    OPTIONAL{ ?item wdt:P856 ?website . }    # gets website
    OPTIONAL{ ?item wdt:P159 ?hq . }
    
      {SELECT DISTINCT ?item ?itemLabel
    WHERE  {
    ?item wdt:P452 ?industry .
              VALUES ?industry { wd:Q11661 
                                 wd:Q11016
                                 wd:Q880371
                                 wd:Q3966
                                 wd:Q1481411
                                 wd:Q1540863
                                 wd:Q638608 }
                                 
        SERVICE wikibase:label {
           bd:serviceParam wikibase:language "en"
        }
       }
      }
     } GROUP BY ?item ?itemLabel}
    SERVICE wikibase:label {
           bd:serviceParam wikibase:language "en"
        }
    }
    ORDER BY ?itemLabel
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多