Optimise HM Land Registry SPARQL query - sparql

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?

Related

query in Wikidata: how to retrieve the labels for instances

I'm trying to get info e.g. for Belgium for aerodromes or stations with having the label of all instances for each item (e.g. Brussels South Charleroi Airpor can be an airport, business, etc..)
I've written two separate queries but struggling to join them
SELECT DISTINCT ?poi ?itemLabel_nl ?itemLabel_fr ?itemLabel_en ?itemLabel_wa ?itemLabel_vls
WHERE {
?poi p:P17 ?statement0.
?statement0 (ps:P17/(wdt:P279*)) wd:Q31.
{
?poi p:P31 ?statement1.
?statement1 (ps:P31/(wdt:P279*)) wd:Q62447.
}
UNION
{
?poi p:P31 ?statement2.
?statement2 (ps:P31/(wdt:P279*)) wd:Q12819564.
}
OPTIONAL { ?poi rdfs:label ?itemLabel_nl filter (lang(?itemLabel_nl) = "nl") . }
OPTIONAL { ?poi rdfs:label ?itemLabel_fr filter (lang(?itemLabel_fr) = "fr") . }
OPTIONAL { ?poi rdfs:label ?itemLabel_en filter (lang(?itemLabel_en) = "en") . }
OPTIONAL { ?poi rdfs:label ?itemLabel_wa filter (lang(?itemLabel_wa) = "wa") . }
OPTIONAL { ?poi rdfs:label ?itemLabel_vls filter (lang(?itemLabel_vls) = "vls") . }
}
and the second part to get labels for the exact item (e.g. Brussels South Charleroi Airpor):
SELECT ?instance ?instanceLabel
WHERE
{
wd:Q1431012 p:P31 ?statement.
?statement ps:P31 ?instance.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?instanceLabel
any thought here? :)

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?

HM Land Registry SPARQL postcode query?

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

Optimizing a SPARQL query

I'm trying to get the details of an organization based on the official website of the company using the below query. It constantly gets timed out.
I require all the below fields. Is there a way to optimize it? I understand that OPTIONAL is equivalent to an INNER JOIN and is the cause of the timeouts but is there any other way of getting these fields?
I'm using the python api and setting a timeout of 5mins doesn't help either. The timeout value doesn't get set.
SELECT distinct
(GROUP_CONCAT( DISTINCT ?official_name; separator=";") AS ?official_name)
(GROUP_CONCAT( DISTINCT ?isin; separator=";") AS ?isin)
?item
?itemLabel
?stock_exchange
?stock_exchangeLabel
(GROUP_CONCAT( DISTINCT ?ticker; separator=";") AS ?ticker)
(GROUP_CONCAT( DISTINCT ?other_name; separator=";") AS ?other_name)
(GROUP_CONCAT(DISTINCT ?parent_orgLabel; SEPARATOR = ";") AS ?parent_orgLabel)
(GROUP_CONCAT(DISTINCT ?owned_byLabel; SEPARATOR = ";") AS ?owned_byLabel)
(GROUP_CONCAT(DISTINCT ?instance_of; SEPARATOR = ";") AS ?instance_of)
(GROUP_CONCAT(DISTINCT ?instance_ofLabel; SEPARATOR = ";") AS ?instance_ofLabel)
(GROUP_CONCAT(DISTINCT ?domains; SEPARATOR = ";") AS ?domains)
(GROUP_CONCAT(DISTINCT ?subsidiaryLabel; SEPARATOR = ";") AS ?subsidiaryLabel)
(GROUP_CONCAT(DISTINCT ?owner_ofLabel; SEPARATOR = ";") AS ?owner_ofLabel)
(GROUP_CONCAT(DISTINCT ?part_ofLabel; SEPARATOR = ";") AS ?part_ofLabel)
WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
{
{ ?item p:P856 [ ps:P856 <https://www.amazon.com> ]}}
OPTIONAL {
?item p:P856 ?web_domains.
?web_domains ps:P856 ?domains .
}
OPTIONAL { ?item wdt:P1448 ?official_name. }
OPTIONAL { ?item wdt:P946 ?isin. }
OPTIONAL {
?item p:P414 ?SE .
?SE ps:P414 ?stock_exchange .
?SE pq:P249 ?ticker .
}
OPTIONAL { ?item skos:altLabel ?other_name. FILTER (LANG (?other_name) = "en") }
OPTIONAL {
?item wdt:P361 ?part_of.
?part_of rdfs:label ?part_ofLabel.
filter(lang(?part_ofLabel)="en")
}
OPTIONAL {
?item wdt:P749 ?parent_org.
?parent_org rdfs:label ?parent_orgLabel.
filter(lang(?parent_orgLabel)="en")
}
OPTIONAL {
?item wdt:P127 ?owned_by.
?owned_by rdfs:label ?owned_byLabel.
filter(lang(?owned_byLabel)="en")
}
OPTIONAL {
?item wdt:P31 ?instance_of.
?instance_of rdfs:label ?instance_ofLabel.
filter(lang(?instance_ofLabel)="en")
}
OPTIONAL {
?item wdt:P355 ?subsidiary.
?subsidiary rdfs:label ?subsidiaryLabel.
filter(lang(?subsidiaryLabel)="en")
}
OPTIONAL {
?item wdt:P1830 ?owner_of.
?owner_of rdfs:label ?owner_ofLabel.
filter(lang(?owner_ofLabel)="en")
}
}
GROUP BY ?item ?itemLabel ?stock_exchange ?stock_exchangeLabel

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.