How to get grand total in a sparql group by query - sparql

I follow up on query where the schema.org database is used to find the number of children of a class. The answer gives for each class the number of children. In my application I need the grand total of all children (i.e. the sum of the counts for each group) in order to compute for each group the percentage of the total number of children.
The query I got from the previous question is:
prefix schema: <http://schema.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?child (count(?grandchild) as ?nGrandchildren)
from <http://localhost:3030/testDB/data/schemaOrg>
where {
?child rdfs:subClassOf schema:Event .
optional {
?grandchild rdfs:subClassOf ?child
}
} group by ?child
which gives the expected answer (events and number of children).
How to get the total number?
I tried a nested query as:
prefix schema: <http://schema.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?child (count(?grandchild) as ?nGrandchildren)
from <http://localhost:3030/testDB/data/schemaOrg>
where {
select (count(?grandchild) as ?grandTotal)
{?child rdfs:subClassOf schema:Event .
optional {
?grandchild rdfs:subClassOf ?child
}
}
} group by ?child
but got a single answer: " " -> 0.

This query uses two sub-SELECTs:
* the first computes the number of grandchildren per child
* the second returns the total number of grandchildren
prefix schema: <http://schema.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?child ?nGrandchildren
(round(?nGrandchildren/?totalGrandchildren * 100) as ?percentageGrandchildren) {
# compute number per child
{
select ?child (count(?grandchild) as ?nGrandchildren) where {
?child rdfs:subClassOf schema:Event .
optional {
?grandchild rdfs:subClassOf ?child
}
}
group by ?child
}
# compute total number
{
select (count(?grandchild) as ?totalGrandchildren) where {
?child rdfs:subClassOf schema:Event .
optional {
?grandchild rdfs:subClassOf ?child
}
}
}
}
Output
-----------------------------------------------------------------------------------------------
| child | nGrandchildren | percentageGrandchildren |
===============================================================================================
| schema:UserInteraction | 9 | "82"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:FoodEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:MusicEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:PublicationEvent | 2 | "18"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:LiteraryEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:SportsEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:DanceEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:ScreeningEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:DeliveryEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:ExhibitionEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:EducationEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:SaleEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:VisualArtsEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:CourseInstance | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:ChildrensEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:BusinessEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:Festival | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:ComedyEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:TheaterEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
| schema:SocialEvent | 0 | "0"^^<http://www.w3.org/2001/XMLSchema#decimal> |
-----------------------------------------------------------------------------------------------

Related

See sparql with specific information

I want to visualize everything (well not all only those attributes) concerning the area to be called, without the filter all are displayed and even information that I do not consider relevant, so I only want to see those attributes (predicates).
It's not even hard for me to understand sparql and I have to show results now !! so thanks in advance
Query
PREFIX dco: <http://192.168.1.10:8890/task/ontology/>
PREFIX dcr: <http://192.168.1.10:8890/digcomp/resource/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select * where {
?Sujeto ?Predicado ?Objeto .
?Sujeto rdf:type dco:Areas .
FILTER (?Objeto="Information and data literacy")
}
Result
| Subject | Predicate | Object |
| ------------- |:-------------:| -----:|
| dcr:areas/1 | rdfs:label | "Information and data literacy" |
Desired result
| Subject | Predicate | Object |
| ------------- |:-------------:| -----:|
| dcr:areas/1 | rdf:type | dco:Areas |
| dcr:areas/1 | rdfs:label | "Information and data literacy" |
| dcr:areas/1 | dcr:name_area | "Information and data literacy"|

Column result unexpectedly returning nothing

Have a SPARQL query that returns nothing for Airport_Name when I believe it should.
PREFIX nas: <https://data.nasa.gov/ontologies/atmonto/NAS#>
PREFIX gen: <https://data.nasa.gov/ontologies/atmonto/general#>
SELECT *
WHERE{
{
{SELECT ?Airport_Name{
?Airport rdf:type nas:Airport ;
nas:airportName ?Airport_Name .
}
}
}UNION{
?Location rdf:type gen:PointLocation;
gen:longitude ?X ;
gen:latitude ?Y .
}
?Location rdf:type gen:PointLocation;
gen:longitude ?X ;
gen:latitude ?Y .
FILTER (?X > -80 && ?X < -64 && ?Y > 18 && ?Y < 32)
}
Expected result should return Location, X, Y, and Airport_Name
When I try to return Airport_Name at the bottom, the Location columns then appear blank, so im not sure what has been done wrongly. If anyone could point me in the right direction, that would be much appreciated.
Your query has several issues, the correct query would be something like below.
Query
SELECT *
WHERE
{ ?airport rdf:type/rdfs:subClassOf* nas:Airport ;
nas:airportName ?Airport_Name ;
nas:airportLocation ?location .
?location gen:longitude ?X ;
gen:latitude ?Y
FILTER ( ?X > "-80"^^xsd:float && ?X < "-64"^^xsd:float && ?Y > "18"^^xsd:float && ?Y < "32"^^xsd:float )
}
Result (sample with LIMIT 10)
--------------------------------------------------------------------------------------------------------------------------
| airport | Airport_Name | location | X | Y |
==========================================================================================================================
| nas:MYEMairport | "Governors Harbour" | nas:MYEMcoordinates | "-76.330178"^^xsd:float | "25.283586"^^xsd:float |
| nas:MDPCairport | "Punta Cana Intl" | nas:MDPCcoordinates | "-68.366186"^^xsd:float | "18.570781"^^xsd:float |
| nas:MUGTairport | "Mariana Grajales" | nas:MUGTcoordinates | "-75.158333"^^xsd:float | "20.085278"^^xsd:float |
| nas:MDSTairport | "Cibao Intl" | nas:MDSTcoordinates | "-70.604689"^^xsd:float | "19.406092"^^xsd:float |
| nas:MYLSairport | "Stella Maris" | nas:MYLScoordinates | "-75.268778"^^xsd:float | "23.583047"^^xsd:float |
| nas:MBNCairport | "North Caicos" | nas:MBNCcoordinates | "-71.939658"^^xsd:float | "21.917486"^^xsd:float |
| nas:MUMOairport | "Orestes Acosta" | nas:MUMOcoordinates | "-74.922222"^^xsd:float | "20.653889"^^xsd:float |
| nas:MYRPairport | "New Port Nelson" | nas:MYRPcoordinates | "-74.836186"^^xsd:float | "23.684378"^^xsd:float |
| nas:MYEGairport | "George Town" | nas:MYEGcoordinates | "-75.781670"^^xsd:float | "23.466667"^^xsd:float |
| nas:MUBYairport | "Carlos Manuel De Cespedes" | nas:MUBYcoordinates | "-76.621389"^^xsd:float | "20.396389"^^xsd:float |
--------------------------------------------------------------------------------------------------------------------------
Note, the query uses the property path rdf:type/rdfs:subClassOf* which only works if you've also loaded the ontology (NAS.ttl). The property path is necessary because the instance data (airportInst.ttl) uses subclasses of nas:Airport like nas:InternationAirport or nas:CanadianAirport.

Preferred combination SPARQL

I am trying to write an elegant SPARQL that gives me one solution for multiple possible queries. I have a number of subjects and a number of predicates and I want to receive a single object. The existence of a single solution is very uncertain so I give multiple options. If I am not mistaken, this can be done with the following query:
SELECT ?object
WHERE {
:subjA|:subjB|:subjC :propA|:propB|:propC ?object.
}
LIMIT 1
The real problem is that I do not want any solution. I want order by :subjA and then :propA. To make it clear; I want the first solution in the following list of combinations:
:subjA :propA
:subjA :propB
:subjA :propC
:subjB :propA
:subjB :propB
:subjB :propC
:subjC :propA
:subjC :propB
:subjC :propC
How to rewrite my query to get the first possible solution?
This is for sure not a valid SPARQL query. You can only use | for the predicate which will then be called a property path but you're losing the variable binding.
SPARQL returns a set of rows but you can use for example the lexicographical order. Not sure, if this is what you want:
sample data
#prefix : <http://ex.org/> .
:subjA :propA :o1 .
:subjA :propC :o1 .
:subjB :propB :o1 .
:subjC :propB :o1 .
:subjC :propA :o1 .
:subjA :propC :o2 .
:subjB :propB :o2 .
:subjB :propC :o2 .
query
PREFIX : <http://ex.org/>
SELECT ?s ?p ?o
WHERE
{ VALUES ?s { :subjA :subjB :subjC }
VALUES ?p { :propA :propB :propC }
?s ?p ?o
}
ORDER BY ?s ?p
result
-------------------------
| s | p | o |
=========================
| :subjA | :propA | :o1 |
| :subjA | :propC | :o1 |
| :subjA | :propC | :o2 |
| :subjB | :propB | :o1 |
| :subjB | :propB | :o2 |
| :subjB | :propC | :o2 |
| :subjC | :propA | :o1 |
| :subjC | :propB | :o1 |
-------------------------
Update
Since a used-defined order is wanted, as a workaround an index for entities can be used (I changed the order of :subjB and :subjC resp. :propB and :propC to show the difference compared to lexicographical order):
query
PREFIX : <http://ex.org/>
SELECT ?s ?p ?o
WHERE
{ VALUES (?sid ?s) { (1 :subjA) (2 :subjC) (3 :subjB) }
VALUES (?pid ?p) { (1 :propA) (2 :propC) (3 :propB) }
?s ?p ?o
}
ORDER BY ?sid ?pid
result
-------------------------
| s | p | o |
=========================
| :subjA | :propA | :o1 |
| :subjA | :propC | :o1 |
| :subjA | :propC | :o2 |
| :subjC | :propB | :o1 |
| :subjC | :propA | :o1 |
| :subjB | :propB | :o1 |
| :subjB | :propB | :o2 |
| :subjB | :propC | :o2 |
-------------------------
You can do it by two ways. They are different by your needs to alphabetic sort or not.
First for alphabetic sort:
Select ?object where {
{Select (min(?sub) as ?msub) where {
?sub :propA|:propB|:propC ?obj1
Filter ( ?sub = :subjA or :sub=?...)
} .
{Select (min(?prop) as ?mrop_by_msub) where {
?msub ?prop ?obj2
Filter ( ?prop = :propA or ?prop=?...)
} .
?msub ?mrop_by_msub ?object
}
Limit 1
Second way for custom sort:
Select ?object where {
{:subjA :propA ?object}
Union
{:subjA :propB ?object}
.......
} limit 1
I write all of it by my memory, so there is may be some errors in this code.
Add third way:
You can add to your rdf store something like this:
:subjA :order_num 1
:subjB :order_num 2
:subjC :order_num 3
:propA :order_num 1
:propB :order_num 2
:propC :order_num 3
And then use query like this:
select ?object where{
?sub :order_num ?ord_num_sub .
?prop :order_num ?ord_num_prop .
?sub ?prop ?object
} order by ?ord_num_sub ?ord_num_prop
limit 1
or if you want to use different of subjs and props you can add filter:
filter ( (?ord_num_sub=2 or ?ord_num_sub=3) and (?ord_num_prop=1 or ?ord_num_prop=2) )

Following namespaces in SPARQL query

Given this data structure
#prefix core <http://www.w3.org/2004/02/skos/core#>
<http://localhost/myvocab/1> core#notation 1 .
<http://localhost/myvocab/1> <http://www.w3.org/2006/time#inDateTime> <http://localhost/myvocab/item1#DateDescription> .
<http://localhost/myvocab/item1#DateDescription> <http://www.w3.org/2006/time#year> 2016 ;
<http://www.w3.org/2006/time#month> "June"
If I run
select * where {?s ?p ?o .
<http://www.w3.org/2004/02/skos/core#notation> 1 . }
then only the first 2 triples (:hasID and :hasTime) are returned. Is there a sparql query (preferably not using a regex filter for matching the id) to return all triples from the child namespaces too?
I'm hoping I can achieve a result something along the lines of
-------------------------------------------------------------------------------------------------------------------------------------------------------
| s | p | o |
=======================================================================================================================================================
| <http://localhost/myvocab/item1> | <http://www.w3.org/2006/time#inDateTime> | <http://localhost/myvocab/item1#DateDescription |
| <http://localhost/myvocab/item1> | <http://www.w3.org/2004/02/skos/core#notation> | 1 |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#month> | "June" |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#year> | 2016 |
-------------------------------------------------------------------------------------------------------------------------------------------------------
It always helps if you provide data that we can actually load and property formed queries that you've tried. The data that you've shown isn't actually legal, nor is the query that you provided. Here's some sample data that's actually loadable:
#prefix core: <http://www.w3.org/2004/02/skos/core#>
<http://localhost/myvocab/1> core:notation 1 ;
<http://www.w3.org/2006/time#inDateTime> <http://localhost/myvocab/item1#DateDescription> .
<http://localhost/myvocab/item1#DateDescription> <http://www.w3.org/2006/time#year> 2016 ;
<http://www.w3.org/2006/time#month> "June"
If I understand you, you want to follow the :item1_DateTime object's properties too. You can do that with a property path that follows :item1's properties and value, and so on. In this query, I use (:|!:) as a wildcard, since every property is either : or it isn't. The * at the end means to follow paths of arbitrary length.
prefix core: <http://www.w3.org/2004/02/skos/core#>
select ?s ?p ?o where {
?s_ core:notation 1 ;
(<>|!<>)* ?s .
?s ?p ?o .
}
--------------------------------------------------------------------------------------------------------------------------------------------------
| s | p | o |
==================================================================================================================================================
| <http://localhost/myvocab/1> | <http://www.w3.org/2006/time#inDateTime> | <http://localhost/myvocab/item1#DateDescription> |
| <http://localhost/myvocab/1> | core:notation | 1 |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#month> | "June" |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#year> | 2016 |
--------------------------------------------------------------------------------------------------------------------------------------------------

sparql use subquery as result/variable for outer query

I want to use result of nested query as a variable/sub graph for outer query.
My use-case is I have One-to-Many relation between product and offers, and I want to get all/selected products as well as offers count, minPrice and condition from offer record.
Here is my query
SELECT ?ProductID ?priceMIN ?total ?condition
WHERE {
{
SELECT ?ProductID (COUNT(?cond) AS ?total) (MIN(?pr) AS ?priceMIN ) ?condition WHERE{
?ProductID ^mod:isOfferOf ?oid.
?oid dprop:priceMIN ?pr.
?oid dprop:total ?cond.
BIND (if( ?cond = 1, "New", "Used") AS ?condition).
}
}
VALUES ?ProductID { prod:Rl5RVl5R prod:Rl5RVl5Q prod:Rl5RVl5W prod:Rl5RVl5Y prod:Rl5RVl5U }
}
and i am getting this data.
ProductID |priceMIN |condition |total
product:Rl5RVl5R | 3267 | Used | 1
product:Rl5RVl5R | 3216 | New | 4
product:Rl5RVl5Y | 327 | New | 1
product:Rl5RVl5Q | 323 | New | 1
product:Rl5RVl5Q | 3268 | Used | 1
product:Rl5RVl5W | 326 | New | 1
product:Rl5RVl5W | 3271 | Used | 4
product:Rl5RVl5U | 325 | New | 2
product:Rl5RVl5U | 3270 | Used | 1
Now I want to assign this value like
product:Rl5RVl5U dprop:newTotal ?total
product:Rl5RVl5U dprop:newMin ?priceMIN
product:Rl5RVl5U dprop:usedTotal ?total
product:Rl5RVl5U dprop:usedMin ?priceMIN