GoodRelations? SPARQL Reference

The following collection of sample SPARQL queries, devised in conjunction with Martin Hepp, showcase how the Web of Linked Data can be used for E-Commerce, based on the GoodRelations? ontology .

Filter All Amounts Greater Than Given Value


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?amount
where
  {
    _:n rdf:type gr:TypeAndQuantityNode.
    _:n gr:amountOfThisGood ?amount. FILTER(?amount > 10)
  }
LIMIT 10

Sum All Amounts


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select SUM (?amount)
where
  {
    _:n rdf:type gr:TypeAndQuantityNode.
    _:n gr:amountOfThisGood ?amount.
  }
LIMIT 10

Get The Minimum Amount Of Good


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select MIN (?amount)
where
  {
    _:n rdf:type gr:TypeAndQuantityNode.
    _:n gr:amountOfThisGood ?amount.
  }
LIMIT 10

Get The Maximum Amount Of Good


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select MAX (?amount)
where
  {
    _:n rdf:type gr:TypeAndQuantityNode.
    _:n gr:amountOfThisGood ?amount.
  }
LIMIT 10

Calculate Average Amounts Of Good


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select AVG (?amount)
where
  {
    _:n rdf:type gr:TypeAndQuantityNode.
    _:n gr:amountOfThisGood ?amount.
  }
LIMIT 10

Get All Products With Their Properties And Models


PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
CONSTRUCT { ?product ?property ?valueModel }
WHERE
  {
    {
      { ?product a gr:ActualProductOrServiceInstance }
      UNION
      { ?product a gr:ProductOrServicesSomeInstancesPlaceholder }
    }
    ?model a gr:ProductOrServiceModel.
    ?product gr:hasMakeAndModel ?model.
    ?model ?property ?valueModel.
    {
      { ?property rdfs:subPropertyOf gr:qualitativeProductOrServiceProperty. }
      UNION
      { ?property rdfs:subPropertyOf gr:quantitativeProductOrServiceProperty. }
      UNION
      { ?property rdfs:subPropertyOf gr:datatypeProductOrServiceProperty. }
    }
    OPTIONAL { ?product ?property ?valueProduct. }
  }

Find How Many Products Each Manufacturer Has And The One With Most Products


PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?a COUNT(?c) AS ?total
WHERE 
  {
    ?a rdf:type gr:BusinessEntity .
    ?b gr:hasManufacturer ?a .
    ?b rdf:type gr:ProductOrServiceModel .
    ?c gr:hasMakeAndModel  ?b .
    ?c rdf:type gr:ProductOrServicesSomeInstancesPlaceholder .

  }
ORDER BY DESC (?total)
LIMIT 1000

Find All Products For Given Manufacturer


PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT COUNT(?prd) AS ?total
WHERE 
  {
    ?prd gr:hasManufacturer ?mnf .
    ?mnf rdfs:isDefinedBy ?name . FILTER(?name = <http://openean.kaufkauf.net/id/>)
  }

Find All Vendors With All Offers And Their Prices


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * 
WHERE
  {
    ?xx a gr:BusinessEntity .
    ?xx gr:offers ?ab .
    ?ab rdf:type gr:Offering .
    ?ab gr:hasBusinessFunction ?bp .
    ?ab gr:eligibleCustomerTypes ?el .
    ?ab gr:includesObject ?b .
    ?b rdf:type gr:TypeAndQuantityNode . 
    ?b gr:typeOfGood ?c .
    ?c rdf:type gr:ProductOrServicesSomeInstancesPlaceholder .
    ?ab gr:hasPriceSpecification ?p .
    ?p gr:hasCurrencyValue ?cv .
}
LIMIT 1000

Show All Manufacturers With Their Products And Models With Prices


PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT *
WHERE 
  {
    ?a rdf:type gr:BusinessEntity .
    ?b gr:hasManufacturer ?a .
    ?b rdf:type gr:ProductOrServiceModel .
    ?c gr:hasMakeAndModel  ?b .
    ?c rdf:type gr:ProductOrServicesSomeInstancesPlaceholder .
    ?c1 gr:typeOfGood   ?c  .
    ?c1 rdf:type gr:TypeAndQuantityNode .
    ?d gr:includesObject ?c1 . 
    ?d gr:hasPriceSpecification ?p .
    ?p gr:hasCurrencyValue ?cv .
    ?p gr:hasCurrency ?cur .
  }
LIMIT 1000

Find The Most Lightweight Camcorders


PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX ceo: <http://www.ebusiness-unibw.org/ontologies/consumerelectronics/v1#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label MIN(?weight) as ?wgt ?ean 
WHERE
  {
    ?m a ceo:Camcorder.
    ?m rdfs:label ?label.
    ?m ceo:hasWeight ?v.
    ?v gr:hasValueFloat ?weight.
    ?v gr:hasUnitOfMeasurement "GRM"^^xsd:string.
    OPTIONAL {?m gr:hasEAN_UCC-13 ?ean}
  }
ORDER BY ?wgt
LIMIT 10

Describe Camera Ascending

The following query describes camera and order results in ASC weight/zoom_factor ratio:


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX ceo: <http://www.ebusiness-unibw.org/ontologies/consumerelectronics/v1#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?m ?label ?weight ?ean ?zoomfactor (( ?weight / ?zoomfactor )) as ?weight_zoom_factor_ratio
WHERE
  {
    ?m a ceo:Camcorder.
    ?m rdfs:label ?label.
    ?m ceo:hasWeight ?v.
    ?m ceo:hasDigitalZoomFactor ?z. ?z gr:hasValueInteger ?zoomfactor . filter ( ?zoomfactor > 0 ) .
    ?v gr:hasValueFloat ?weight.
    ?v gr:hasUnitOfMeasurement "GRM"^^xsd:string.
    OPTIONAL {?m gr:hasEAN_UCC-13 ?ean}
  }
ORDER BY ASC ( ?weight / ?zoomfactor )

Describe Camera Descending

The following query describes camera and order results in DESC weight/zoom_factor ratio:


PREFIX gr: <http://purl.org/goodrelations/v1#>
PREFIX ceo: <http://www.ebusiness-unibw.org/ontologies/consumerelectronics/v1#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?m ?label ?weight ?ean ?zoomfactor (( ?weight / ?zoomfactor )) as ?weight_zoom_factor_ratio
WHERE
  {
    ?m a ceo:Camcorder.
    ?m rdfs:label ?label.
    ?m ceo:hasWeight ?v.
    ?m ceo:hasDigitalZoomFactor ?z. ?z gr:hasValueInteger ?zoomfactor . filter ( ?zoomfactor > 0 ) .
    ?v gr:hasValueFloat ?weight.
    ?v gr:hasUnitOfMeasurement "GRM"^^xsd:string.
    OPTIONAL {?m gr:hasEAN_UCC-13 ?ean}
  }
ORDER BY DESC ( ?weight / ?zoomfactor )

Get All Offerings In LOC Space By Currency

This query gets first 50 items in the space that cost between 5 and 10 EUR or 7 and 14 USD.


PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX gr:<http://purl.org/goodrelations/v1#>

SELECT ?s ?p ?c
WHERE
  {
    ?s a gr:Offering.
    ?s gr:hasPriceSpecification ?ps.
    ?ps gr:hasCurrencyValue ?p.
    ?ps gr:hasCurrency ?c.
    FILTER ((regex(?c, "EUR") && ?p >"5"^^xsd:float && ?p <"10"^^xsd:float)
    || (regex(?c, "USD") && ?p >"7"^^xsd:float && ?p <"14"^^xsd:float))
  }
LIMIT 50

List Of Class Frequency


SELECT ?class, COUNT(?instance) as ?freq
WHERE
 {
   ?instance a ?class.
   {
     SELECT ?class
     FROM <http://purl.org/goodrelations/v1#>
     WHERE
       {
         ?class a owl:Class.
       }
   }
 }

List of Class Frequency Order By Descending


SELECT ?class, ?freq
WHERE
  {
    {
      SELECT ?class, COUNT(?instance) as ?freq
      WHERE
        {
          ?instance a ?class.
          {
            SELECT ?class
            FROM <http://purl.org/goodrelations/v1#>
            WHERE
              {
                ?class a owl:Class.
              }
          }
        }
    }
  }
ORDER BY DESC (?freq)

List Of Object Property Frequency


SELECT ?prop, ?label, COUNT(?instance) as ?freq
WHERE
  {
    ?instance ?prop ?object.
     {
       SELECT ?prop, ?label
       FROM <http://purl.org/goodrelations/v1#>
       WHERE
         {
           ?prop a owl:ObjectProperty.
           ?prop rdfs:label ?label.
         }
     }
  }

List Of Datatype Property Frequency


SELECT ?prop, ?label, COUNT(?instance) as ?freq
WHERE
  {
    ?instance ?prop ?object.
    {
      SELECT ?prop, ?label
      FROM <http://purl.org/goodrelations/v1#>
      WHERE
        {
          ?prop a owl:DatatypeProperty.
          ?prop rdfs:label ?label.
        }
    }
  }

Consolidate Business Entities

The following query consolidates Business Entities that have the exact same gr:legalName:


PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
CONSTRUCT { ?u2 owl:sameAs ?u1 }
WHERE
  {
    ?u1 a gr:BusinessEntity.
    ?u2 a gr:BusinessEntity.
    ?u1 gr:legalName ?name1.
    ?u2 gr:legalName ?name2.
    FILTER ( ?u1 != ?u2 && ?name1 = ?name2 )
  }

Consolidate Product Models

The following query consolidates Product Models that have the exact same gr:hasEAN_UCC-13:


PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
CONSTRUCT { ?u2 owl:sameAs ?u1 }
WHERE
  {
    ?u1 a gr:ProductOrServiceModel.
    ?u2 a gr:ProductOrServiceModel.
    ?u1 gr:hasEAN_UCC-13 ?ean1.
    ?u2 gr:hasEAN_UCC-13 ?ean2.
    FILTER ( ?u1 != ?u2 && ?ean1 = ?ean2 &&  ! bif:isnull (?ean1) )
  }

Reference