简体   繁体   中英

How to make a sparql query inside another sparql query?

I am trying to make a sparql query inside another sparql query. In sql, we can do it like this:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

I want to do the same thing in SPARQL query. Specifically I have two sparql queries and I want to combine them together. My end goal is to find 'Siemens PLM Software Company's subsidiaries. In order to do this, first i need to find the company's id and then look for its subsidiaries.

Q1: Finds the unique identity of 'Siemens PLM Software Company'

SELECT DISTINCT ?item ?label ?articleLabel  WHERE {
  ?item ?label "Siemens PLM Software"@en;
    wdt:P31 wd:Q4830453.
  ?article schema:about ?item;
    schema:inLanguage "en".
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
  • Returns Q15898201

Q2: Find the subsidiary of 'Siemens PLM Software Company'

 SELECT ?Subsidiary ?SubsidiaryLabel ?parent_organization ?parent_organizationLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  ?Subsidiary wdt:P749 wd:Q15898201.
  OPTIONAL { ?Subsidiary wdt:P749 ?parent_organization. }   
  • Returns Siemens

I would like to combine them together to something like this:

SELECT ?Subsidiary ?SubsidiaryLabel ?parent_organization ?parent_organizationLabel WHERE {
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      ?Subsidiary wdt:P749 wd:
{SELECT DISTINCT ?item ?label ?articleLabel  WHERE {
      ?item ?label "Siemens PLM Software"@en;
        wdt:P31 wd:Q4830453.
      ?article schema:about ?item;
        schema:inLanguage "en".
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
}.
      OPTIONAL { ?Subsidiary wdt:P749 ?parent_organization. } 

Do you have any idea how can i do this? Thank you!

There were several things not quite clear:

  • Did you really mean to return the rdfs:label property as ?label ? I don't understand why you'd want to do that, so I assume you mean to just directly match on the rdfs:label property without returning it
  • Why return ?articleLabel ? It's not a real label anyway but just the value of ?label as a literal. I assume you mean to just return ?article .
  • It seems you have the parent organization relationship backwards? Your Q2 asks for subsidiaries of Siemens PLM Software. None exist. To ask for Siemens, you need to ask for

     wd:Q15898201 wdt:P749 ?parent_organization 

    and not

     ?Subsidiary wdt:P749 wd:Q15898201 

With that out of the way: There is no need for a subquery here. The query can be achieved simply by writing out the graph pattern for the desired graph structure, using OPTIONAL for parts that may not exist, and making sure that the variable names match up correctly throughout the query:

SELECT ?item ?itemLabel ?parent_organization ?parent_organizationLabel WHERE {

  # Find business by label
  ?item rdfs:label "Siemens PLM Software"@en;
    wdt:P31 wd:Q4830453.

  # Find English-language articles about the business
  ?article schema:about ?item;
    schema:inLanguage "en".

  # Find the business' parent organization, if one exists
  OPTIONAL { ?item wdt:P749 ?parent_organization. }   

  # For any variable ?xxx, add variable ?xxxLabel with the label
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

That being said, you can use a subquery if you really want to:

SELECT ?item ?itemLabel ?parent_organization ?parent_organizationLabel WHERE {
  {
    SELECT ?item {
      ?item rdfs:label "Siemens PLM Software"@en;
      wdt:P31 wd:Q4830453.
    }
  }
  ?article schema:about ?item;
    schema:inLanguage "en".
  OPTIONAL { ?item wdt:P749 ?parent_organization. }   
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

This is equivalent to the first query, but runs much slower because the query optimiser is not as good at handling subqueries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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