Suppose we have the following dataset measuring heart rate observations:
userId
observation
value
:alice
obs_1_1
102
:alice
obs_1_2
105
:alice
obs_1_3
97
:bob
obs_2_1
78
:bob
obs_2_2
86
:bob
obs_2_3
84
And we want to determine which of the users have an average heart rate of >100.
The following query returns empty values for the BIND:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX sosa: <http://www.w3.org/ns/sosa/>
SELECT ?user ?result (AVG(?res) AS ?avg_heart_rate) WHERE {
?obs rdf:type sosa:Observation ;
sosa:hasSimpleResult ?res ;
sosa:hasFeatureOfInterest ?user .
BIND (?avg_heart_rate>100 AS ?result) .
} GROUP BY ?user ?result
Resultset:
?user
?result
?avg_heart_reate
:alice
101.333
:bob
82.667
Any idea what I am doing wrong will be highly appreciated!
Related
I created the following SPARQL query to Wikidata. And the result of this query are records related to states in Germany. But as you can see, results are occurring four times in a row (you can test it here: https://query.wikidata.org/). I supposed that there is a problem with geo coordinates and languages but I can't resolve it anyway. What is wrong with this query and how can I fix it to receive a result without repetition?
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX schema: <http://schema.org/>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?latitude ?longitude ?description ?iso31662
WHERE
{ ?subject wdt:P31 wd:Q1221156 ;
rdfs:label ?name ;
wdt:P17 ?countryClass .
?countryClass
wdt:P297 ?countryCode .
?subject wdt:P31/(wdt:P279)* ?adminArea .
?adminArea wdt:P2452 "A.ADM1" ;
wdt:P2452 ?featureCode .
?subject wdt:P300 ?iso31662
OPTIONAL
{ ?subject schema:description ?description
FILTER ( lang(?description) = "en" )
?subject p:P625 ?coordinate .
?coordinate psv:P625 ?coordinateNode .
?coordinateNode
wikibase:geoLatitude ?latitude ;
wikibase:geoLongitude ?longitude
}
FILTER ( lang(?name) = "en" )
FILTER EXISTS { ?subject wdt:P300 ?iso31662 }
}
ORDER BY lcase(?name)
OFFSET 0
LIMIT 200
In short, "9.0411111111111"^^xsd:double and "9.0411111111111"^^xsd:decimal are distinct, though they might be equal in some sense.
Check this:
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?description ?iso31662
(datatype(?latitude) AS ?lat)
(datatype(?longitude) AS ?long)
and this:
SELECT DISTINCT ?subject ?featureCode ?countryCode ?name ?description ?iso31662
(xsd:decimal(?latitude) AS ?lat)
(xsd:decimal(?longitude) AS ?long)
I have exactly these triples in GraphDB:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
<http://example.com/greeting>
a <http://example.com/word> ;
rdfs:label "hello" .
I want to know if there is a thing in my triplestore with the label "hello" and another with the label "goodbye"
PREFIX : <http://example.com/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
ASK
WHERE
{ VALUES ?l { "goodbye" "hello" }
?s a :thing ;
rdfs:label ?l
}
I am being told that yes, this is true, as if it is saying at least one of those are true. But I want to know if all of those patterns are true.
Can I do that in a SPARQL ASK?
I also tried the following but got the same (unwanted) result:
PREFIX : <http://example.com/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
ASK
WHERE
{ VALUES (?l) { ("goodbye") ("hello") }
?s a :thing ;
rdfs:label ?l
}
Sanity check: the answer to this ASK is false
PREFIX : <http://example.com/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
ASK
WHERE
{ VALUES (?l) { ("goodbye") }
?s a :thing ;
rdfs:label ?l
}
Rather than asking "are all of these present", ask "is any of these not present" (and then negate the result either I've your application or with another "filter not exists":
ask {
values ?label { "hello" "goodbye" }
filter not exists {
?s a :thing ; rdfs:label ?word
}
}
My group is developing tools to convert tabular data about hospital records into RDF triples and then perform various cleanups and aggregations.
I have started this "answer" by writing out an illustration of this workflow with sample data.
Working implementations of the suggestions from #Joshua Taylor and #AKSW's are at the bottom.
The tabular data is first converted into "shortcut triples", which instantiate a minimal number of classes and link all literal values to those classes, even if the literal values are really "more about" something else.
So tabular data like this:
+-------+------------+----------+----------+
| EncID | EncDate | DiagCode | CodeType |
+-------+------------+----------+----------+
| 102 | 12/05/2015 | J44.9 | ICD-10 |
| 103 | 11/25/2015 | 602.9 | ICD-9 |
| 102 | 12/05/2015 | I50.9 | ICD-10 |
+-------+------------+----------+----------+
First becomes triples like this (ignoring the EncDates and CodeTypes.)
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX turbo: <http://example.org/ontologies/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT DATA {
GRAPH turbo:encounters_from_karma {
turbo:5f62d61cee174283a4f875ccb8bb91a1 rdf:type obo:OGMS_0000097 .
turbo:5f62d61cee174283a4f875ccb8bb91a1 turbo:ScEnc2DiagCode "J44.9" .
turbo:5f62d61cee174283a4f875ccb8bb91a1 turbo:ScEnc2DiagCodeRegText "ICD-10" .
turbo:5f62d61cee174283a4f875ccb8bb91a1 turbo:ScEnc2EncID "102" .
turbo:81fcbb5c5bd141c9bde7f23321648ff7 rdf:type obo:OGMS_0000097 .
turbo:81fcbb5c5bd141c9bde7f23321648ff7 turbo:ScEnc2DiagCode "I50.9" .
turbo:81fcbb5c5bd141c9bde7f23321648ff7 turbo:ScEnc2DiagCodeRegText "ICD-10" .
turbo:81fcbb5c5bd141c9bde7f23321648ff7 turbo:ScEnc2EncID "102" .
turbo:820dd597229244ab853ed845dd740f1f rdf:type obo:OGMS_0000097 .
turbo:820dd597229244ab853ed845dd740f1f turbo:ScEnc2DiagCode "602.9" .
turbo:820dd597229244ab853ed845dd740f1f turbo:ScEnc2DiagCodeRegText "ICD-9" .
turbo:820dd597229244ab853ed845dd740f1f turbo:ScEnc2EncID "103" . }
}
And is then expanded like this
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX turbo: <http://example.org/ontologies/>
INSERT {
GRAPH turbo:expanded_encounters {
?NewEnc rdf:type obo:OGMS_0000097 .
?NewEnc turbo:previousUriText ?previousUriText .
?NewEnc obo:OBI_0000299 ?DiagCrid .
?DiagCrid rdf:type turbo:DiagCrid .
?DiagCrid obo:BFO_0000051 ?DiagSymb .
?DiagSymb rdf:type turbo:EncounterDiagCodeSymbol .
?DiagSymb turbo:thingLiteralValue ?DiagSymbVal .
}
}
WHERE
{ GRAPH turbo:encounters_from_karma
{ ?EncFromKarma
rdf:type obo:OGMS_0000097 ;
turbo:ScEnc2DiagCode ?DiagSymbVal
BIND(str(?EncFromKarma) AS ?previousUriText)
BIND(uri(concat("http://transformunify.org/ontologies/", struuid())) AS ?NewEnc)
BIND(uri(concat("http://transformunify.org/ontologies/", struuid())) AS ?DiagCrid)
BIND(uri(concat("http://transformunify.org/ontologies/", struuid())) AS ?DiagSymb)
}
}
And therefore looks like this:
#prefix turbo: <http://purl.obolibrary.org/obo/> .
#prefix obo: <http://example.org/ontologies/> .
<http://example.org/ontologies/b9dc5b08-cf1b-465e-8773-4b19bfbcf803>
a <http://purl.obolibrary.org/obo/OGMS_0000097> ;
turbo:OBI_0000299 <http://example.org/ontologies/8a04f52f-22d2-4aab-bacf-d96e1c7fe900> ;
obo:previousUriText "http://example.org/ontologies/5f62d61cee174283a4f875ccb8bb91a1" .
obo:8a04f52f-22d2-4aab-bacf-d96e1c7fe900
a obo:DiagCrid ;
turbo:BFO_0000051 obo:6738d8c0-8bb8-4078-8430-5e9294e5af15 .
obo:6738d8c0-8bb8-4078-8430-5e9294e5af15
a obo:EncounterDiagCodeSymbol ;
obo:thingLiteralValue "J44.9" .
obo:d3a8a700-2eb9-420d-a863-d47462fa393c
a turbo:OGMS_0000097 ;
turbo:OBI_0000299 obo:c12acc26-6dbe-486d-9ae9-9f34c9561aea ;
obo:previousUriText "http://example.org/ontologies/81fcbb5c5bd141c9bde7f23321648ff7" .
obo:c12acc26-6dbe-486d-9ae9-9f34c9561aea
a obo:DiagCrid ;
turbo:BFO_0000051 obo:3b784151-b369-4594-9ce3-285f5fe60850 .
obo:3b784151-b369-4594-9ce3-285f5fe60850
a obo:EncounterDiagCodeSymbol ;
obo:thingLiteralValue "I50.9" .
obo:af0e949a-99e4-48cd-885b-7cb1aa3dd265
a turbo:OGMS_0000097 ;
turbo:OBI_0000299 obo:c2da52ec-7331-4011-b8f8-6fbf8b419708 ;
obo:previousUriText "http://example.org/ontologies/820dd597229244ab853ed845dd740f1f" .
obo:c2da52ec-7331-4011-b8f8-6fbf8b419708
a obo:DiagCrid ;
turbo:BFO_0000051 obo:7f8399ef-5fcd-447c-80a4-18dfb160e99c .
obo:7f8399ef-5fcd-447c-80a4-18dfb160e99c
a obo:EncounterDiagCodeSymbol ;
obo:thingLiteralValue "602.9" .
Finally, I can check for the correct transformation with the suggestions from #Joshua Taylor or #AKSW:
#Joshua Taylor
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX turbo: <http://example.org/ontologies/>
ASK
WHERE
{ { GRAPH turbo:expanded_encounters
{ VALUES ( ?previousUriTextVal ?DiagSymbVal ) {
( "http://example.org/ontologies/5f62d61cee174283a4f875ccb8bb91a1" "J44.9" )
( "http://example.org/ontologies/820dd597229244ab853ed845dd740f1f" "602.9" )
( "http://example.org/ontologies/81fcbb5c5bd141c9bde7f23321648ff7" "I50.9" )
}
FILTER NOT EXISTS { ?NewEnc rdf:type obo:OGMS_0000097 ;
turbo:previousUriText ?previousUriTextVal ;
obo:OBI_0000299 ?DiagCrid .
?DiagCrid rdf:type turbo:DiagCrid ;
obo:BFO_0000051 ?DiagSymb .
?DiagSymb rdf:type turbo:EncounterDiagCodeSymbol ;
turbo:thingLiteralValue ?DiagSymbVal
}
}
}
}
#AKSW
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX obo: <http://purl.obolibrary.org/obo/>
PREFIX turbo: <http://example.org/ontologies/>
ASK
WHERE
{ GRAPH turbo:expanded_encounters
{ FILTER ( ?count = 3 )
{ SELECT (COUNT(DISTINCT ?NewEnc) AS ?count)
WHERE
{ VALUES ( ?previousUriTextVal ?DiagSymbVal ) {
( "http://example.org/ontologies/5f62d61cee174283a4f875ccb8bb91a1" "J44.9" )
( "http://example.org/ontologies/820dd597229244ab853ed845dd740f1f" "602.9" )
( "http://example.org/ontologies/81fcbb5c5bd141c9bde7f23321648ff7" "I50.9" )
}
?NewEnc rdf:type obo:OGMS_0000097 ;
turbo:previousUriText ?previousUriTextVal ;
obo:OBI_0000299 ?DiagCrid .
?DiagCrid rdf:type turbo:DiagCrid ;
obo:BFO_0000051 ?DiagSymb .
?DiagSymb rdf:type turbo:EncounterDiagCodeSymbol ;
turbo:thingLiteralValue ?DiagSymbVal
}
}
}
}
I created this query to return all books that are notable works by George Orwell but it returns no result.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?title where {
?person foaf:name ?name .
?title dbo:author ?person .
?title dbo:notableWork dbp:George_Orwell .
}
I cannot seem to figure out why there is no result.
I am running the query in http://dbpedia.org/snorql
Don't you have the triples about notable works in the wrong order?
Try rewriting based on this working query
SELECT *
WHERE {
:George_Orwell dbo:notableWork ?title
}
.
title
:Nineteen_Eighty-Four
:Animal_Farm
You can also bind :George_Orwell to a variable and ask more about that:
SELECT *
WHERE {
values ?author { :George_Orwell } .
?author rdfs:label ?l .
?title ?p ?author .
?title rdf:type dbo:Book .
filter (lang(?l) = "en")
}
and DESCRIBE things
describe :Animal_Farm
My query returns result dependent on which one of two sparql OPTIONS clauses come first for a List of Images. Although using Jena ARQ is not an option at this point, and I'd like to solve this with a pure SPARQL query, still I'd like to know how it could be solved with Jena as well.
My data presentation is attached below, the data may contain a list of images. My image representation is also below. I'm attaching my query as well.
The sprql query has two variables urlX, and urlY declared in the 2 OPTIONS blocks, if a List of images exists. Depending on which of the OPTIONS comes first, I get the value for that one variable, while the other one doesn't get reached. It seems the issue has to do with using OPTIONS clause. I'm not sure what else I can try instead, I'm far from being an expert on sparql queries. I want the query to do the following: if a collection of images is present, I want to see if both image sizes (dc:conformsTo) are present and get both urlX and urlY values, or get the ones that exist. Much appreciate your time.
My data representation:
#prefix dc: <http://purl.org/dc/terms/> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix lews: <http://lews.com/content/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
lews:26331340 lews:name "the Human Good Luck Charm is Back"^^xsd:token ;
dc:created "2014-10-20T17:14:55.357-07:00"^^xsd:dateTime ;
dc:identifier "26331340"^^xsd:int ;
dc:modified "2016-08-04T13:43:00.897-07:00"^^xsd:dateTime ;
dc:title "the Human Good Luck Charm is Back" ;
dc:hasPart <http://lews.com/content/26331340#Images> ;
dc:abstract "As the World Series gets underway..." ;
dc:description "The super fan who rooted for the Royals is back to boost morale." ;
dc:subject "hoping for a World Series victory".
<http://lews.com/content/26331340#Images> dc:identifier "Images"^^xsd:token ;
rdf:first lews:26331375 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 ;
rdf:li <http://lews.com/content/26331340#Images> , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 ;
a rdf:List .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 rdf:first lews:26331376 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 rdf:first lews:26331377 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 rdf:first lews:26331378 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 rdf:first lews:26331379 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 rdf:first lews:26331380 ;
rdf:rest rdf:nil .
My image representation:
#prefix dc: <http://purl.org/dc/terms/> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix lews: <http://abcnews.com/content/> .
#prefix mrss: <http://search.yahoo.com/mrss/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
lews:26331376 lews:name "141020_wn_donvan0_704x396.jpg"^^xsd:token ;
lews:section "wnt"^^xsd:token ;
lews:type "Image"^^xsd:token ;
dc:conformsTo "704x396"^^xsd:token ;
dc:created "2014-10-20T17:15:09.637-07:00"^^xsd:dateTime ;
dc:hasFormat <http://lews.go.com/images/WNT/141020_wn_donvan0_704x396.jpg> ;
dc:identifier "26331376"^^xsd:int ;
dc:isPartOf <http://lews.go.com/WNT> ;
dc:modified "2014-10-20T17:15:09.947-07:00"^^xsd:dateTime ;
dc:type "StillImage"^^xsd:token ;
mrss:height "396"^^xsd:int ;
mrss:width "704"^^xsd:int ;
xsd:date "2014-10-20"^^xsd:date ;
xsd:gMonthDay "--10-20"^^xsd:gMonthDay ;
xsd:gYear "2014"^^xsd:gYear ;
xsd:gYearMonth "2014-10"^^xsd:gYearMonth .
My query:
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX mrss: <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName
?subject dc:identifier ?id.
OPTIONAL {?subject dc:title ?title.}
OPTIONAL {?subject dc:description ?description.}
OPTIONAL {?subject dc:isPartOf ?section.}
OPTIONAL {
?subject dc:hasPart ?imageCol.
?imageCol dc:identifier "Images"^^xsd:token.
OPTIONAL{
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
dc:conformsTo "4x3";
dc:hasFormat ?urlX.
}
OPTIONAL{
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
dc:conformsTo "16x9";
dc:hasFormat ?urlY.
}
}
}
LIMIT ${limit}
If I understood correctly what it is you want, you just need to group the optionals differently:
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX mrss: <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName {
?subject dc:identifier ?id.
OPTIONAL { ?subject dc:title ?title. }
OPTIONAL { ?subject dc:description ?description. }
OPTIONAL { ?subject dc:isPartOf ?section. }
OPTIONAL {
?subject dc:hasPart ?imageCol.
?imageCol dc:identifier "Images"^^xsd:token.
OPTIONAL {
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
# Here we optionally bind ?urlX and/or ?urlY
OPTIONAL {
?image dc:conformsTo "4x3";
dc:hasFormat ?urlX.
}
OPTIONAL {
?image dc:conformsTo "16x9";
dc:hasFormat ?urlY.
}
}
}
}
I am trying to query dbpedia to get some people data and I don't have subjects just names of the people I want to query and their birth/death dates.
I am trying to do a query along these lines. I want the name, birth date, death date and thumbnail of everyone with the surname Presley. What I then intend to do is loop through the results returned and find the best match for Elvis Presley 1935-1977 which is the data I have.
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?thumbnail ?birthDate ?deathDate WHERE {
{
dbo:name ?Name ;
dbo:birthDate ?birthDate ;
dbo:birthDate ?deathDate ;
dbo:thumbnail ?thumbnail ;
FILTER contains(?Name#en, "Presley")
}
What is the best way to construct my sparql query?
UPDATE:
I have put together this query which seems to work to some extent but I don't entirely understand it, and I can't figure out the contains, but it does at least run and return results.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?subject ?thumbnail ?birthdate ?deathdate WHERE {
{
?subject rdfs:label "Elvis Presley"#en ;
dbo:thumbnail ?thumbnail ;
dbo:birthDate ?birthdate ;
dbo:deathDate ?deathdate ;
a owl:Thing .
}
UNION
{
?altName rdfs:label "Elvis Presley"#en ;
dbo:thumbnail ?thumbnail ;
dbo:birthDate ?birthdate ;
dbo:deathDate ?deathdate ;
dbo:wikiPageRedirects ?s .
}
}
Some entities might not have all of that information, so it's better to use optional. You can use foaf:surname to check for surname directly.
select * where {
?s foaf:surname "Presley"#en
optional { ?s dbo:name ?name }
optional { ?s dbo:birthDate ?birth }
optional { ?s dbo:deathDate ?death }
optional { ?s dbo:thumbnail ?thumb }
}