HM Land Registry SPARQL postcode query? - sparql

I'm looking at researching into the impact of particular energy sources, upon house prices. I would like to find information on property values for multiple post codes, in the vicinity of where energy is being produced.
I found the following information which was helpful: HM Land Registry SPARQL query for multiple postcodes and have adjusted the code sightly, so that I am able to obtain more categories of information. However, this only produces results when I use the example post codes specified in the link above. It doesn't return results for the other postcodes that I want. Apologies, if there is a straight forward solution, but I am completely new to SPARQL (and to coding in general). Hence I am not sure what to do next? I've attached the code below. Many thanks.
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/>
prefix pc: <http://data.ordnancesurvey.co.uk/ontology/postcode/>
prefix ukhpi: <http://landregistry.data.gov.uk/def/ukhpi/>
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/>
SELECT DISTINCT ?regionName ?postcode ?date ?ukhpi ?volume ?ukhpi_averagePrice ?ukhpi_averagePriceCash ?ukhpi_averagePriceDetached ?ukhpi_averagePriceExistingProperty ?ukhpi_averagePriceFirstTimeBuyer ?ukhpi_averagePriceFlatMaisonette ?ukhpi_averagePriceFormerOwnerOccupier ?ukhpi_averagePriceMortgage ?ukhpi_averagePriceNewBuild ?ukhpi_averagePriceSA ?ukhpi_averagePriceSemiDetached ?ukhpi_averagePriceTerraced ?ukhpi_housePriceIndex ?ukhpi_housePriceIndexCash ?ukhpi_housePriceIndexDetached ?ukhpi_housePriceIndexExistingProperty ?ukhpi_housePriceIndexFirstTimeBuyer ?ukhpi_housePriceIndexFlatMaisonette ?ukhpi_housePriceIndexFormerOwnerOccupier ?ukhpi_housePriceIndexMortgage ?ukhpi_housePriceIndexNewBuild ?ukhpi_housePriceIndexSA ?ukhpi_housePriceIndexSemiDetached ?ukhpi_housePriceIndexTerraced ?ukhpi_percentageAnnualChange ?ukhpi_percentageAnnualChangeCash ?ukhpi_percentageAnnualChangeDetached ?ukhpi_percentageAnnualChangeExistingProperty ?ukhpi_percentageAnnualChangeFirstTimeBuyer ?ukhpi_percentageAnnualChangeFlatMaisonette ?ukhpi_percentageAnnualChangeFormerOwnerOccupier ?ukhpi_percentageAnnualChangeMortgage ?ukhpi_percentageAnnualChangeNewBuild ?ukhpi_percentageAnnualChangeSemiDetached ?ukhpi_percentageAnnualChangeTerraced ?ukhpi_percentageChange ?ukhpi_percentageChangeCash ?ukhpi_percentageChangeDetached ?ukhpi_percentageChangeExistingProperty ?ukhpi_percentageChangeFirstTimeBuyer ?ukhpi_percentageChangeFlatMaisonette ?ukhpi_percentageChangeFormerOwnerOccupier ?ukhpi_percentageChangeMortgage ?ukhpi_percentageChangeNewBuild ?ukhpi_percentageChangeSemiDetached ?ukhpi_percentageChangeTerraced ?ukhpi_refPeriodDuration ?ukhpi_refPeriodStart ?ukhpi_salesVolume ?ukhpi_salesVolumeCash ?ukhpi_salesVolumeExistingProperty ?ukhpi_salesVolumeMortgage ?ukhpi_salesVolumeNewBuild{
SERVICE <http://data.ordnancesurvey.co.uk/datasets/os-linked-data/apis/sparql> {
VALUES ?postcode {
# Insert postcodes here (two example postcodes below)
"NP20 5AW"
"SW1W 0NY"
}
?postcodeUri rdfs:label ?postcode ;
pc:district ?ordnanceSurveyRegion .
}
?region owl:sameAs ?ordnanceSurveyRegion .
?region rdfs:label ?regionName .
FILTER (langMatches(lang(?regionName), "EN"))
?report
ukhpi:refRegion ?region;
ukhpi:refMonth ?date;
ukhpi:housePriceIndex ?ukhpi.
OPTIONAL {
?report ukhpi:salesVolume ?volume
} OPTIONAL
{ ?report ukhpi:averagePrice ?ukhpi_averagePrice }
OPTIONAL
{ ?report ukhpi:averagePriceCash ?ukhpi_averagePriceCash }
OPTIONAL
{ ?report ukhpi:averagePriceDetached ?ukhpi_averagePriceDetached }
OPTIONAL
{ ?report ukhpi:averagePriceExistingProperty ?ukhpi_averagePriceExistingProperty }
OPTIONAL
{ ?report ukhpi:averagePriceFirstTimeBuyer ?ukhpi_averagePriceFirstTimeBuyer }
OPTIONAL
{ ?report ukhpi:averagePriceFlatMaisonette ?ukhpi_averagePriceFlatMaisonette }
OPTIONAL
{ ?report ukhpi:averagePriceFormerOwnerOccupier ?ukhpi_averagePriceFormerOwnerOccupier }
OPTIONAL
{ ?report ukhpi:averagePriceMortgage ?ukhpi_averagePriceMortgage }
OPTIONAL
{ ?report ukhpi:averagePriceNewBuild ?ukhpi_averagePriceNewBuild }
OPTIONAL
{ ?report ukhpi:averagePriceSA ?ukhpi_averagePriceSA }
OPTIONAL
{ ?report ukhpi:averagePriceSemiDetached ?ukhpi_averagePriceSemiDetached }
OPTIONAL
{ ?report ukhpi:averagePriceTerraced ?ukhpi_averagePriceTerraced }
OPTIONAL
{ ?report ukhpi:housePriceIndex ?ukhpi_housePriceIndex }
OPTIONAL
{ ?report ukhpi:housePriceIndexCash ?ukhpi_housePriceIndexCash }
OPTIONAL
{ ?report ukhpi:housePriceIndexDetached ?ukhpi_housePriceIndexDetached }
OPTIONAL
{ ?report ukhpi:housePriceIndexExistingProperty ?ukhpi_housePriceIndexExistingProperty }
OPTIONAL
{ ?report ukhpi:housePriceIndexFirstTimeBuyer ?ukhpi_housePriceIndexFirstTimeBuyer }
OPTIONAL
{ ?report ukhpi:housePriceIndexFlatMaisonette ?ukhpi_housePriceIndexFlatMaisonette }
OPTIONAL
{ ?report ukhpi:housePriceIndexFormerOwnerOccupier ?ukhpi_housePriceIndexFormerOwnerOccupier }
OPTIONAL
{ ?report ukhpi:housePriceIndexMortgage ?ukhpi_housePriceIndexMortgage }
OPTIONAL
{ ?report ukhpi:housePriceIndexNewBuild ?ukhpi_housePriceIndexNewBuild }
OPTIONAL
{ ?report ukhpi:housePriceIndexSA ?ukhpi_housePriceIndexSA }
OPTIONAL
{ ?report ukhpi:housePriceIndexSemiDetached ?ukhpi_housePriceIndexSemiDetached }
OPTIONAL
{ ?report ukhpi:housePriceIndexTerraced ?ukhpi_housePriceIndexTerraced }
OPTIONAL
{ ?report ukhpi:percentageAnnualChange ?ukhpi_percentageAnnualChange }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeCash ?ukhpi_percentageAnnualChangeCash }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeDetached ?ukhpi_percentageAnnualChangeDetached }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeExistingProperty ?ukhpi_percentageAnnualChangeExistingProperty }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeFirstTimeBuyer ?ukhpi_percentageAnnualChangeFirstTimeBuyer }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeFlatMaisonette ?ukhpi_percentageAnnualChangeFlatMaisonette }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeFormerOwnerOccupier ?ukhpi_percentageAnnualChangeFormerOwnerOccupier }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeMortgage ?ukhpi_percentageAnnualChangeMortgage }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeNewBuild ?ukhpi_percentageAnnualChangeNewBuild }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeSemiDetached ?ukhpi_percentageAnnualChangeSemiDetached }
OPTIONAL
{ ?report ukhpi:percentageAnnualChangeTerraced ?ukhpi_percentageAnnualChangeTerraced }
OPTIONAL
{ ?report ukhpi:percentageChange ?ukhpi_percentageChange }
OPTIONAL
{ ?report ukhpi:percentageChangeCash ?ukhpi_percentageChangeCash }
OPTIONAL
{ ?report ukhpi:percentageChangeDetached ?ukhpi_percentageChangeDetached }
OPTIONAL
{ ?report ukhpi:percentageChangeExistingProperty ?ukhpi_percentageChangeExistingProperty }
OPTIONAL
{ ?report ukhpi:percentageChangeFirstTimeBuyer ?ukhpi_percentageChangeFirstTimeBuyer }
OPTIONAL
{ ?report ukhpi:percentageChangeFlatMaisonette ?ukhpi_percentageChangeFlatMaisonette }
OPTIONAL
{ ?report ukhpi:percentageChangeFormerOwnerOccupier ?ukhpi_percentageChangeFormerOwnerOccupier }
OPTIONAL
{ ?report ukhpi:percentageChangeMortgage ?ukhpi_percentageChangeMortgage }
OPTIONAL
{ ?report ukhpi:percentageChangeNewBuild ?ukhpi_percentageChangeNewBuild }
OPTIONAL
{ ?report ukhpi:percentageChangeSemiDetached ?ukhpi_percentageChangeSemiDetached }
OPTIONAL
{ ?report ukhpi:percentageChangeTerraced ?ukhpi_percentageChangeTerraced }
OPTIONAL
{ ?report ukhpi:refPeriodDuration ?ukhpi_refPeriodDuration }
OPTIONAL
{ ?report ukhpi:refPeriodStart ?ukhpi_refPeriodStart }
OPTIONAL
{ ?report ukhpi:salesVolume ?ukhpi_salesVolume }
OPTIONAL
{ ?report ukhpi:salesVolumeCash ?ukhpi_salesVolumeCash }
OPTIONAL
{ ?report ukhpi:salesVolumeExistingProperty ?ukhpi_salesVolumeExistingProperty }
OPTIONAL
{ ?report ukhpi:salesVolumeMortgage ?ukhpi_salesVolumeMortgage }
OPTIONAL
{ ?report ukhpi:salesVolumeNewBuild ?ukhpi_salesVolumeNewBuild }
} ORDER BY ?ukhpi_refMonth

Related

Grouping qualifiers in the output of a Wikidata SPARQL query

I am building a Wikidata SPARQL query to retrieve details about specific people. When asking for their educational details (P69 - educatedAt)- I am not sure how to craft the query such that it will collate their degrees and majors in the case that in one statement about being educated at an institution they have multiple degrees or majors (I used this other query to find people with multiple degrees from Harvard).
This is the query:
SELECT ?itemLabel (GROUP_CONCAT(DISTINCT ?altNames; SEPARATOR = ";") AS ?aliases) ?itemDesc ?genderLabel ?birthday ?placeOfBirthLabel ?image (GROUP_CONCAT(DISTINCT ?ed; SEPARATOR = "|") AS ?education) WHERE {
VALUES ?item {
wd:Q5402996
}
OPTIONAL {
?item skos:altLabel ?altNames.
FILTER((LANG(?altNames)) = "en")
}
{
OPTIONAL { ?item wdt:P21 ?gender. }
OPTIONAL { ?item wdt:P569 ?birthday. }
OPTIONAL { ?item wdt:P19 ?placeOfBirth. }
OPTIONAL { ?item wdt:P18 ?image. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
{
OPTIONAL {
?item p:P69 ?statement.
?statement (ps:P69/rdfs:label) ?eduLabel.
FILTER((LANG(?eduLabel)) = "en")
OPTIONAL { ?statement pq:P580 ?edStart. }
OPTIONAL { ?statement pq:P582 ?edEnd. }
OPTIONAL {
?statement (pq:P512/rdfs:label) ?edDegrees.
FILTER((LANG(?edDegrees)) = "en")
}
OPTIONAL {
?statement (pq:P812/rdfs:label) ?edMajors.
FILTER((LANG(?edMajors)) = "en")
}
BIND(IF(BOUND(?edStart), CONCAT("::start:", STR(YEAR(?edStart))), "") AS ?edStartText)
BIND(IF(BOUND(?edEnd), CONCAT("::end:", STR(YEAR(?edEnd))), "") AS ?edEndText)
BIND(IF(BOUND(?edDegrees), CONCAT("::degrees:", STR(?edDegrees)), "") AS ?edDegreeText)
BIND(IF(BOUND(?edMajors), CONCAT("::majors:", STR(?edMajors)), "") AS ?edMajorText)
BIND(CONCAT(?eduLabel, ?edStartText, ?edEndText, ?edDegreeText, ?edMajorText) AS ?ed)
}
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
?item schema:description ?itemDesc.
}
}
GROUP BY ?itemLabel ?itemDesc ?genderLabel ?birthday ?image ?placeOfBirthLabel
For the education result I get:
Harvard University::end:1980::degrees:Master of Arts::majors:astronomy|
Harvard University::end:1980::degrees:Doctor of Philosophy::majors:astronomy|
University of Rochester::end:1976::degrees:Bachelor of Arts|
University of Rochester::end:1976::degrees:Bachelor of Science
I would like to get:
Harvard University::end:1980::degrees:Master of Arts;Doctor of Philosophy::majors:astronomy|
University of Rochester::end:1976::degrees:Bachelor of Arts:Bachelor of Science|
How can I group the degrees in line like this in my query?
Or even better have them be nested in the JSON output rather than using delimiters?

Missing data fields in Wikidata Query Service results despite items having these data

I am trying to retrieve some municipalities from Wikidata using SPARQL but several items returned have much of their fields empty despite these items having these data. I do not understand what is wrong with the query below (link to WQS). For example, the municipality Almelo has its coordinates (P625), and parent place (P131) erroneously missing in the results:
SELECT ?mun ?munLabel ?coords ?parentPlace ?area WHERE {
?mun p:P31 ?instanceOf # Get statement because we need this later
.
?instanceOf ps:P31/wdt:279* wd:Q2039348.
OPTIONAL {
?mun wdt:P625 ?coords;
wdt:P131 ?parentPlace;
wdt:P2046 ?area
.
}
MINUS { ?instanceOf pq:P582 ?endTime. } # Don't show municipalities that have an end time
service wikibase:label { bd:serviceParam wikibase:language "en". }
} ORDER BY ?munLabel
This is because you are using one OPTIONAL statement instead of 3 separately.
In this case, Almelo doesn't have an 'area', wdt:P2046, so the whole OPTIONAL statement evaluates as false, and so it binds no variables.
The following query works:
Notice that we have 3 distinct optional statements, so that they may fail to bind variables independently of each other.
SELECT ?mun ?munLabel ?coords ?parentPlace ?area WHERE {
?mun p:P31 ?instanceOf # Get statement because we need this later
.
?instanceOf ps:P31/wdt:279* wd:Q2039348.
OPTIONAL {?mun wdt:P625 ?coords }
OPTIONAL {?mun wdt:P131 ?parentPlace }
OPTIONAL {?mun wdt:P2046 ?area }
MINUS { ?instanceOf pq:P582 ?endTime. } # Don't show municipalities that have an end time
service wikibase:label { bd:serviceParam wikibase:language "en". }
} ORDER BY ?munLabel
You have to declare OPTIONAL each statement independently:
OPTIONAL { ?mun wdt:P625 ?coords . }
OPTIONAL { ?mun wdt:P131 ?parentPlace . }
OPTIONAL { ?mun wdt:P2046 ?area . }
Otherwise, if one of them is missing, then the whole OPTIONAL block is ignored.
See also Multiple Optional Graph Patterns.

Optimise HM Land Registry SPARQL query

I got the following query that successfully extracts house price index data from the UK land registry (https://api.triplydb.com/s/BIN1LSLxO):
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix sr: <http://data.ordnancesurvey.co.uk/ontology/spatialrelations/>
prefix ukhpi: <http://landregistry.data.gov.uk/def/ukhpi/>
prefix lrppi: <http://landregistry.data.gov.uk/def/ppi/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix lrcommon: <http://landregistry.data.gov.uk/def/common/>
# House price index for aa within a given date range
SELECT *
{
?region ukhpi:refPeriodStart ?date ;
ukhpi:refRegion ?refRegion .
?refRegion rdfs:label ?regionLabel ;
OPTIONAL { ?region ukhpi:averagePrice ?averagePrice }
OPTIONAL { ?region ukhpi:averagePriceCash ?averagePriceCash }
OPTIONAL { ?region ukhpi:averagePriceDetached ?averagePriceDetached }
OPTIONAL { ?region ukhpi:averagePriceExistingProperty ?averagePriceExistingProperty }
OPTIONAL { ?region ukhpi:averagePriceFirstTimeBuyer ?averagePriceFirstTimeBuyer }
OPTIONAL { ?region ukhpi:averagePriceFlatMaisonette ?averagePriceFlatMaisonette }
OPTIONAL { ?region ukhpi:averagePriceFormerOwnerOccupier ?averagePriceFormerOwnerOccupier }
OPTIONAL { ?region ukhpi:averagePriceMortgage ?averagePriceMortgage }
OPTIONAL { ?region ukhpi:averagePriceNewBuild ?averagePriceNewBuild }
OPTIONAL { ?region ukhpi:averagePriceSemiDetached ?averagePriceSemiDetached }
OPTIONAL { ?region ukhpi:averagePriceTerraced ?averagePriceTerraced }
OPTIONAL { ?region ukhpi:housePriceIndex ?housePriceIndex }
OPTIONAL { ?region ukhpi:housePriceIndexCash ?housePriceIndexCash }
OPTIONAL { ?region ukhpi:housePriceIndexDetached ?housePriceIndexDetached }
OPTIONAL { ?region ukhpi:housePriceIndexExistingProperty ?housePriceIndexExistingProperty }
OPTIONAL { ?region ukhpi:housePriceIndexFirstTimeBuyer ?housePriceIndexFirstTimeBuyer }
OPTIONAL { ?region ukhpi:housePriceIndexFlatMaisonette ?housePriceIndexFlatMaisonette }
OPTIONAL { ?region ukhpi:housePriceIndexFormerOwnerOccupier ?housePriceIndexFormerOwnerOccupier }
OPTIONAL { ?region ukhpi:housePriceIndexMortgage ?housePriceIndexMortgage }
OPTIONAL { ?region ukhpi:housePriceIndexNewBuild ?housePriceIndexNewBuild }
OPTIONAL { ?region ukhpi:housePriceIndexSemiDetached ?housePriceIndexSemiDetached }
OPTIONAL { ?region ukhpi:housePriceIndexTerraced ?housePriceIndexTerraced }
OPTIONAL { ?region ukhpi:percentageAnnualChange ?percentageAnnualChange }
OPTIONAL { ?region ukhpi:percentageAnnualChangeCash ?percentageAnnualChangeCash }
OPTIONAL { ?region ukhpi:percentageAnnualChangeDetached ?percentageAnnualChangeDetached }
OPTIONAL { ?region ukhpi:percentageAnnualChangeExistingProperty ?percentageAnnualChangeExistingProperty }
OPTIONAL { ?region ukhpi:percentageAnnualChangeFirstTimeBuyer ?percentageAnnualChangeFirstTimeBuyer }
OPTIONAL { ?region ukhpi:percentageAnnualChangeFlatMaisonette ?percentageAnnualChangeFlatMaisonette }
OPTIONAL { ?region ukhpi:percentageAnnualChangeFormerOwnerOccupier ?percentageAnnualChangeFormerOwnerOccupier }
OPTIONAL { ?region ukhpi:percentageAnnualChangeMortgage ?percentageAnnualChangeMortgage }
OPTIONAL { ?region ukhpi:percentageAnnualChangeNewBuild ?percentageAnnualChangeNewBuild }
OPTIONAL { ?region ukhpi:percentageAnnualChangeSemiDetached ?percentageAnnualChangeSemiDetached }
OPTIONAL { ?region ukhpi:percentageAnnualChangeTerraced ?percentageAnnualChangeTerraced }
OPTIONAL { ?region ukhpi:percentageChange ?percentageChange }
OPTIONAL { ?region ukhpi:percentageChangeCash ?percentageChangeCash }
OPTIONAL { ?region ukhpi:percentageChangeDetached ?percentageChangeDetached }
OPTIONAL { ?region ukhpi:percentageChangeExistingProperty ?percentageChangeExistingProperty }
OPTIONAL { ?region ukhpi:percentageChangeFirstTimeBuyer ?percentageChangeFirstTimeBuyer }
OPTIONAL { ?region ukhpi:percentageChangeFlatMaisonette ?percentageChangeFlatMaisonette }
OPTIONAL { ?region ukhpi:percentageChangeFormerOwnerOccupier ?percentageChangeFormerOwnerOccupier }
OPTIONAL { ?region ukhpi:percentageChangeMortgage ?percentageChangeMortgage }
OPTIONAL { ?region ukhpi:percentageChangeNewBuild ?percentageChangeNewBuild }
OPTIONAL { ?region ukhpi:percentageChangeSemiDetached ?percentageChangeSemiDetached }
OPTIONAL { ?region ukhpi:percentageChangeTerraced ?percentageChangeTerraced }
OPTIONAL { ?region ukhpi:refMonth ?refMonth }
OPTIONAL { ?region ukhpi:refPeriodDuration ?refPeriodDuration }
OPTIONAL { ?region ukhpi:refPeriodStart ?refPeriodStart }
OPTIONAL { ?region ukhpi:salesVolume ?salesVolume }
OPTIONAL { ?region ukhpi:salesVolumeCash ?salesVolumeCash }
OPTIONAL { ?region ukhpi:salesVolumeExistingProperty ?salesVolumeExistingProperty }
OPTIONAL { ?region ukhpi:salesVolumeMortgage ?salesVolumeMortgage }
OPTIONAL { ?region ukhpi:salesVolumeNewBuild ?salesVolumeNewBuild }
FILTER (
?date > "2008-12-31"^^xsd:date &&
?date < "2019-03-01"^^xsd:date &&
contains (lcase(str(?refRegion)),"nottingham")
)
}
However, this suffers of a problem: if I would like to search for Chester, I got back Manchster, Colchester among the others.
The best way would be to lookup for the right region name, in this case Cheshire West and Chester on Ordnance Survey, and filter by that name. However, I have no idea how to do it.
Can anyone help?

BINDing to inlined data provided by VALUES, and braces

I tried the following query on WDQS:
SELECT ?item ?itemLabel ?string ?StringLabel ?iri ?iriLabel
WHERE {
VALUES ?item { wd:Q1339 }
BIND( STR(?item) AS ?string ).
BIND( IRI(?string) AS ?iri ).
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
And the result has ?string and ?iri value. However, if I place an extra pair of braces in the query expression
SELECT ?item ?itemLabel ?string ?StringLabel ?iri ?iriLabel
WHERE {
VALUES ?item { wd:Q1339 }
{
BIND( STR(?item) AS ?string ).
BIND( IRI(?string) AS ?iri ).
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
}
?string and ?iri in the result are empty, looking as if ?item is missing in the first BIND expression. Why is the result different?
SPARQL's "bottom-up evaluation" is often better understood by its other label, i.e., "inside-out evaluation". That is, nesting is evaluated from the innermost to the outermost.
If you flip the nesting, you'll see the results you expected --
SELECT ?item ?itemLabel ?string ?StringLabel ?iri ?iriLabel
WHERE
{
BIND( STR(?item) AS ?string ).
BIND( IRI(?string) AS ?iri ).
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
{
VALUES ?item { wd:Q1339 }
}
}

SPARQL distinct rowcount not returning correct number

I have the following SPARQL query , please pay attention to the rowcount in the selection predicate and the group by clause at the end of the query.
I want the query to return the correct row count in every record , I noticed that the row count that is being returned is not correct meaning if there is a single record I get 1 or sometimes 2 and if there are more than 2 records , I still get a 2, basically it seems to return random value.
I know its because of some issue with my query , can someone please let me know what I might be doing wrong ? I am using Apache Jena.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX pf: <http://jena.hpl.hp.com/ARQ/property#>
PREFIX d: <http://data-vocabulary.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX s: <http://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
SELECT DISTINCT (count(*) AS ?rowCount) ?productName ?offerImage ?offerName ?productCategory ?salePrice ?suggestedRetailPrice ?productImage ?productThumbNail ?productUrl (GROUP_CONCAT(DISTINCT ?productdescription) AS ?productdescriptions) ?productBrand ?productId ?productSku ?productModel ?productMPN ?productManufacturer ?productGtin13 ?productGtin14 ?productGtin8 ?productAvailable ?productUnAvailable ?productUsedCondition ?productNewCondition ?productColor ?productAggregateRatingValue ?productReviewCount
WHERE
{
?p2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> d:Product .
?p2 <http://data-vocabulary.org/Product/offerDetails> ?schOffer .
?schOffer <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> d:Offer .
?schOffer <http://data-vocabulary.org/Offer/price> ?salePrice
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/name> ?offerName }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/name> ?productName }
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/image> ?offerImage }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/url> ?productUrl }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/image> ?productImage }
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/category> ?productCategory }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/description> ?productdescription }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/brand> ?pBrandNode
OPTIONAL
{ ?pBrandNode <http://data-vocabulary.org/Brand/name> ?brandNodeName }
OPTIONAL
{ ?pBrandNode <http://data-vocabulary.org/Organization/name> ?brandNodeName }
BIND(str(coalesce(?brandNodeName, ?pBrandNode)) AS ?productBrand)
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/manufacturer> ?pManufacturerNode
OPTIONAL
{ ?pManufacturerNode <http://data-vocabulary.org/manufacturer/name> ?manufacturerNodeName }
BIND(str(coalesce(?manufacturerNodeName, ?pManufacturerNode)) AS ?productManufacturer)
}
OPTIONAL
{ ?p2 <http://schema.org/Product/aggregateRating> ?pAggregateRating
OPTIONAL
{ ?pAggregateRating <http://schema.org/AggregateRating/ratingValue> ?productAggregateRatingValue }
OPTIONAL
{ ?pAggregateRating <http://schema.org/AggregateRating/reviewCount> ?productReviewCount }
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/productID> ?productId }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/sku> ?productSku }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/itemCondition> ?productNewCondition .
?productNewCondition <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> s:NewCondition
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/itemCondition> ?productUsedCondition .
?productUsedCondition <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> s:UsedCondition
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/gtin13> ?productGtin13 }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/gtin14> ?productGtin14 }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/gtin8> ?productGtin8 }
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/availability> ?productAvailable
FILTER ( ?productAvailable = s:InStock )
}
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/availability> ?productUnAvailable
FILTER ( ?productUnAvailable = s:OutOfStock )
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/model> ?productModel }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/mpn> ?productMPN }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/color> ?productColor }
}
GROUP BY ?productName ?offerImage ?offerName ?productCategory ?salePrice ?suggestedRetailPrice ?productImage ?productThumbNail ?productUrl ?productBrand ?productId ?productSku ?productModel ?productMPN ?productManufacturer ?productGtin13 ?productGtin14 ?productGtin8 ?productAvailable ?productUnAvailable ?productUsedCondition ?productNewCondition ?productColor ?productAggregateRatingValue ?productReviewCount
It is saying that the pattern you have more than one item in a group. Try running without the count and group by and look at the results where you get 2.