-1

I would like to change this query which only provides the start date of marriage for one item (wd:Q42)

WHERE {wd:Q42 p:P26 ?statement.   
       ?statement ps:P26 ?spouse;
                  pq:P580 ?starttime;
                  pq:P582 ?endtime.   
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } 
} 
ORDER BY (?starttime)

to work for multiple items (e.g. all writers, all citizens of US).

F Gh
  • 65
  • 8
  • 1
    Just replace `wd:Q42` with a variable (e.g. `?x`) and then state something about that variable, (e.g. `?x wdt:P106 wd:Q36180.` for writers or `?x wdt:P27 wd:Q30.` for citizens of US). – logi-kal Jul 20 '23 at 06:01

1 Answers1

0

Use a variable (?foo) instead of wd:Q42.

Then you have three options:

  • Leave it like that to query all items.
    (Not a good idea if the additional triple patterns aren’t restrictive enough.)
  • Add triple patterns using the ?variable to narrow down which items to query.
    (Just like you did with ?statement.)
  • Provide a list of subjects that should be queried (using VALUES).

Example for querying writers (occuption = Q36180):

SELECT ?writerLabel ?spouseLabel ?starttimeLabel ?endtimeLabel
WHERE {

  VALUES ?writer { wd:Q42 wd:Q47087 }
  
  ?writer p:P26 ?statement_spouse ;
          p:P106 ?statement_occupation .
  
  ?statement_occupation ps:P106 wd:Q36180 .
  
  ?statement_spouse ps:P26 ?spouse ;
                    pq:P580 ?starttime ;
                    pq:P582 ?endtime .   

  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 

} 
ORDER BY ?starttime
LIMIT 10

By commenting out the VALUES line, you would query all writers instead of just the two listed.


Note that your query requires an end time. To also find ongoing marriages, you can use OPTIONAL for that triple pattern:

  ?statement_spouse ps:P26 ?spouse ;
                    pq:P580 ?starttime .

  OPTIONAL {
    ?statement_spouse pq:P582 ?endtime .
  }