Search code examples
sparqlwikidata

How to get film cost currency in Wikidata SPARQL?


I know how to get the cost of specified films:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?item (GROUP_CONCAT( ?_cost; SEPARATOR = "~~~") AS ?budget) 
WHERE {
VALUES ?selectedMovies { wd:Q24515019 wd:Q20762698 }
?item wdt:P31/wdt:P279* wd:Q11424 filter (?item = ?selectedMovies).
OPTIONAL {
?item wdt:P2130 ?_cost.
}
}
GROUP BY ?item

But when I try to get the currency for cost, I get nothing:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?item (GROUP_CONCAT( ?_cost; SEPARATOR = "~~~") AS ?budget) 
(GROUP_CONCAT( ?_currency; SEPARATOR = "~~~") AS ?_currency)
WHERE {
VALUES ?selectedMovies { wd:Q24515019 wd:Q20762698 }
?item wdt:P31/wdt:P279* wd:Q11424 filter (?item = ?selectedMovies).
OPTIONAL {
?item wdt:P2130 ?_cost.
?_cost wdt:P2237 ?_currency.
}
}
GROUP BY ?item

I checked, on the page of these movies, the cost currency is present.

So, how can I get the currency?


Solution

  • Well, it's a bit more complicated as you're asking for properties of units:

    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    SELECT ?item (GROUP_CONCAT( DISTINCT ?cost_with_unit; SEPARATOR = "~~~") AS ?budget) 
    WHERE {
    VALUES ?selectedMovies { wd:Q24515019 wd:Q20762698 }
    ?item wdt:P31/wdt:P279* wd:Q11424 filter (?item = ?selectedMovies).
    OPTIONAL {
    ?item wdt:P2130 ?_cost.
    # get the node to the cost statement
    ?item          p:P2130                   ?stmnode.
    # then its value node
    ?stmnode       psv:P2130                   ?valuenode.
    # then its unit, i.e. currency as entity
    ?valuenode     wikibase:quantityUnit       ?unit.
    # then finally, its label 
    ?unit rdfs:label ?unitLabel.
    FILTER(LANGMATCHES(LANG(?unitLabel), 'en'))
    # put everything together
    BIND(CONCAT(str(?_cost), " ", str(?unitLabel)) as ?cost_with_unit)
    }
    }
    GROUP BY ?item
    

    Update

    To get the ISO 4217 code of the unit, replace

    # then finally, its label 
    ?unit rdfs:label ?unitLabel.
    FILTER(LANGMATCHES(LANG(?unitLabel), 'en'))
    

    with

    # then finally, the ISO 4217 code of the unit, e.g. USD 
    ?unit wdt:P498 ?unitLabel .