Search code examples
sparqlgraphdbknowledge-graph

Unable to see Max in SPARQL Query


Im trying to query a knowledge graph and im trying print the max occurrence of ?n in the result and i have tried running following query but it just doesn't prints anything

here is my SPARQL Query

PREFIX : <http://www.tafsirtabari.com/ontology#>


PREFIX RDF:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>


select
?n
(MAX( xsd:int(?countOfSharedLikedItems)) as ?max) 
(COUNT(?n) as ?countOfSharedLikedItems)  

where {

?h :hasTheme :lugha .
?h RDF:type :Hadith .
?h :hasHadithNo ?o.



?p :isPartOfHadith ?h.
{
    ?p :hasNarratorSegment ?nc.
    ?nc :refersTo+/:hasName ?n.
}
Union
{
    ?p :hasRootNarratorSegment ?rnc.
    ?rnc :refersTo+/:hasName ?n.
}

 } 

i have also tried following by using group by ?n

PREFIX : <http://www.tafsirtabari.com/ontology#>


PREFIX RDF:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>


select 
(MAX(?countOfSharedLikedItems) as ?max) 
(COUNT(?n) as ?countOfSharedLikedItems)  
where {
 ?h :hasTheme :lugha .
?h RDF:type :Hadith .
?h :hasHadithNo ?o.



?p :isPartOfHadith ?h.
{
    ?p :hasNarratorSegment ?nc.
    ?nc :refersTo+/:hasName ?n.
}
Union
{
    ?p :hasRootNarratorSegment ?rnc.
    ?rnc :refersTo+/:hasName ?n.
}

} group by ?n

THIS IS THE RESULT OF THE ABOVE MENTIONED QUERY I JUST CANT SEE VALUES


Solution

  • You can try this

    PREFIX : <http://www.tafsirtabari.com/ontology#>
    
    select  (COUNT(?o ) AS ?triples) where {
     ?k :heardFrom ?o 
    }
    
    6. Which RAWI narrated most hadiths about TOPIC_A 
    
    PREFIX hash: <http://www.tafsirtabari.com/ontology#>
    PREFIX W3:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX : <http://www.tafsirtabari.com/ontology#>
    
    SELECT  ?total WHERE{
    select DISTINCT ?n (COUNT(?n) as ?total)  where {
        ?commentary hash:mentions hash:اهل_المعرفه .
       ?segment hash:containsCommentary ?commentary.
       ?segment ?Fr ?h .
        ?h W3:type hash:Hadith.
        ?p :isPartOfHadith ?h.
        {
            ?p :hasNarratorSegment ?nc.
            ?nc :refersTo+/:hasName ?n.
            
        }
        Union
        {
            ?p :hasRootNarratorSegment ?rnc.
            ?rnc :refersTo+/:hasName ?n.
        }
    }GROUP BY ?n
    }ORDER BY DESC(?total)
    LIMIT 1