--- gitea: none include_toc: true --- # Sparql Queries ## Select ### Übersichts-Queries #### Finde alle Classes ``` turtle PREFIX rdf: SELECT DISTINCT ?type WHERE { ?s a ?type. } ``` #### Finde alle Classes mit bestimmten Prefix ``` turtle PREFIX bc: SELECT DISTINCT ?type WHERE { ?subject a ?type. FILTER( STRSTARTS(STR(?type),str(bc:)) ) } ``` #### Select all Triples ``` turtle select ?s ?p ?o where {?s ?p ?o} ``` #### Select Triples with rdf:type as ?p ``` turtle select ?s ?p ?o where { ?s rdf:type ?o . } ```` #### Select Triples with rdf:type and multiple ?o ``` turtle select ?s ?p ?o where { values ?o { } ?s rdf:type ?o } ``` #### Count ``` turtle prefix rico: SELECT (count (?c) as ?cCount) WHERE { ?c a } ``` ### Suche nach String #### Select resource with needle in 2nd level of haystack ``` turtle prefix rico: SELECT ?s WHERE { ?s ^rico:isOrWasIdentifierOf ?o . FILTER EXISTS { ?o rico:name "CH-000249-X:666377" } } ``` ### Date Queries #### Events zwischen Start- und Enddatum ``` turtle # Events zwischen Start- und Enddatum # *********************************** prefix rdf: prefix rdfs: prefix dcterms: prefix skos: prefix schema: prefix roru: SELECT ?s ?event_name ?datestart ?startmonth ?dateend ?dateend2 WHERE { ?s schema:name ?event_name . ?s schema:startDate ?datestart . ?s schema:endDate ?dateend . BIND(xsd:date(?dateend) AS ?dateend2) . BIND(MONTH(?datestart) AS ?startmonth) . #BIND(STRLEN(?dateend) AS ?len_dateend) . #FILTER (?len_dateend > 0) . # achtung, nur xsd:dateTime lassen sich vergleichen, xsd:date nicht FILTER (xsd:dateTime(?datestart) < "2022-02-14T00:00:00"^^xsd:dateTime) FILTER (xsd:dateTime(?dateend) > "2022-02-14T00:00:00"^^xsd:dateTime) } order by desc(?dateend) ``` #### Events genaues Datum ``` turtle # Events genaues Datum # ******************** prefix rdf: prefix rdfs: prefix dcterms: prefix skos: prefix schema: prefix roru: SELECT ?s ?event_name ?datestart ?dateend ?datestartyear ?dateendyear WHERE { ?s schema:name ?event_name . OPTIONAL { ?s schema:startDate ?datestart .} OPTIONAL { ?s schema:endDate ?dateend .} BIND ( STR(YEAR(?datestart)) AS ?datestartyear ) . BIND ( STR(MONTH(?datestart)) AS ?datestartmonth ) . BIND ( STR(DAY(?datestart)) AS ?datestartday ) . BIND ( STR(YEAR(?dateend)) AS ?dateendyear ) . BIND ( STR(MONTH(?dateend)) AS ?dateendmonth ) . BIND ( STR(DAY(?dateend)) AS ?dateendday ) . FILTER (?datestartyear = "2022" || ?dateendyear = "2022") . FILTER (?datestartmonth = "3" || ?dateendmonth = "3") . FILTER (?datestartday = "15" || ?dateendday = "15") . } order by desc(?dateend) ``` ### RANDOM select example 1 ``` turtle # select all unitdate values of any resource prefix rico: SELECT DISTINCT ?resource ?s ?date WHERE { { ?s rico:normalizedDateValue ?date . } UNION { ?s rico:expressedDate ?date . } ?s rico:hasOrHadCategory . ^rico:hasOrHadCategory ?relation . ?relation rico:relationHasSource ?resource . } ORDER BY ASC (?resource) ``` ## Update ### Delete Triples where ?s ?p ?o contains string ``` turtle DELETE { ?s ?p ?o } WHERE { ?s ?p ?o . FILTER(regex(str(?s), "needle")) } ```