How do I get the unit of a value from Wikidata using SPARQL?

416 Views Asked by At

I'm new to SPARQL and I'm trying to get the boiling points of a chemical from Wikidata. However, when I try this query on ethanol as an example, I get two numbers, one being the boiling point in Fahrenheit and the other in Celsius, but without being able to tell which is which.

I'd ideally like to filter to only return the Celsius value, but I'd also be happy if I could just get the unit for each value and then I can sort it out later (that's what I've tried so far, as a first step).

I've tried this:

SELECT 
  ?temperature ?temperatureLabel ?unitLabel

WHERE {
  wd:Q153 p:P2102 ?statement.
  ?statement ps:P2102 [
    wikibase:quantityAmount ?temperature;
    wikibase:quantityUnit ?unit;
  ].
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

and this:

SELECT 
  ?temperature ?temperatureLabel ?unitLabel

WHERE {
  wd:Q153 p:P2102 ?statement.
  ?statement ps:P2102 ?temperature.
  ?temperature wikibase:quantityUnit ?unit.
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

...amongst other things, but receive no matching results. I've been banging my head against the wall with this for hours now, would really appreciate any help.

1

There are 1 best solutions below

1
On BEST ANSWER

ps: links a statement to a simple value. For quantities, this means a value without unit, lower bound and upper bound.

To get the unit, you first need to access the value node from a statement with psv:. From the value node you can then get the unit and the amount with wikibase:quantityUnit and wikibase:quantityAmount.

SELECT ?temperature ?unitLabel WHERE {
  wd:Q153 p:P2102 ?statement . 
  ?statement psv:P2102 ?valuenode.
  ?valuenode wikibase:quantityAmount ?temperature.
  ?valuenode wikibase:quantityUnit ?unit.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}