If I understand you correctly, you're asking to find the maximum score in each category, and then to find, for each category, the student with that highest score in that category. It's easier to work with data (in the future, please try to provide minimal data that we can work with), so here's some sample data:
@prefix : <urn:ex:>
:student1 :hasScore [ :inCategory :category1 ; :value 90 ] ,
[ :inCategory :category2 ; :value 75 ] ,
[ :inCategory :category3 ; :value 85 ] .
:student2 :hasScore [ :inCategory :category2 ; :value 75 ] ,
[ :inCategory :category3 ; :value 90 ] ,
[ :inCategory :category4 ; :value 90 ] .
:student3 :hasScore [ :inCategory :category1 ; :value 85 ] ,
[ :inCategory :category2 ; :value 80 ] ,
[ :inCategory :category4 ; :value 95 ] .
There are four categories, and student1 has the highest score in category1, student3 has the highest score in categories 2 and 4, and student2 has the highest score in category 3. We can write a query like this:
prefix : <urn:ex:>
select ?category ?student ?highScore where {
#-- Find the high score in each category
{ select ?category (max(?score) as ?highScore) {
?student :hasScore [ :inCategory ?category ; :value ?score ] .
}
group by ?category
}
#-- Then find the student that had that high
#-- score in the category.
?student :hasScore [ :inCategory ?category ; :value ?highScore ] .
}
--------------------------------------
| category | student | highScore |
======================================
| :category1 | :student1 | 90 |
| :category2 | :student3 | 80 |
| :category3 | :student2 | 90 |
| :category4 | :student3 | 95 |
--------------------------------------
If you don't care about which student got the highest score, then you just want that inner subquery:
prefix : <urn:ex:>
select ?category (max(?score) as ?highScore) {
?student :hasScore [ :inCategory ?category ; :value ?score ] .
}
group by ?category
--------------------------
| category | highScore |
==========================
| :category1 | 90 |
| :category2 | 80 |
| :category3 | 90 |
| :category4 | 95 |
--------------------------
If you're using different properties
In a comment, you asked,
I have my ontology like this: Student1 :Englishscore 90;
PhyscicsScore 67; ChemScore 78. Similarly for other students. Should I
introduce a blank node like hasScore which reference to Englishscore,
PhyscicsScore [sic], and ChemScore?
First, I'd recommend that you standardize your naming convention. First, be sure to use correct spelling (e.g., Physics). Then, either abbreviate or don't. You're abbreviating Chemistry to Chem, but not English to Eng. Finally, be consistent in your capitalization (e.g., EnglishScore, not Englishscore).
It's not necessary to use the kind of representation that I used. You didn't provide sample data (please do in the future), so I used what I considered a fairly easy one to use. Your representation seems a bit less flexible, but you can still get the information you want. Here's some new sample data:
@prefix : <urn:ex:>
:student1 :hasCat1Score 90 ;
:hasCat2Score 75 ;
:hasCat3Score 85 .
:student2 :hasCat2Score 75 ;
:hasCat3Score 90 ;
:hasCat4Score 90 .
:student3 :hasCat1Score 85 ;
:hasCat2Score 80 ;
:hasCat4Score 95 .
Then the query just needs to use a variable for the property, and that variable simultaneously relates the student to the score, and also indicates the category. So you'd still just group by that property and ask for the highest score:
prefix : <urn:ex:>
select ?hasScore (max(?score) as ?highScore) {
?student ?hasScore ?score
}
group by ?hasScore
-----------------------------
| hasScore | highScore |
=============================
| :hasCat1Score | 90 |
| :hasCat2Score | 80 |
| :hasCat3Score | 90 |
| :hasCat4Score | 95 |
-----------------------------