首页 > 解决方案 > 如何在 SPARQL 中将 CONCAT 与来自 GROUP_CONCAT 的结果一起使用?

问题描述

我在使用 SPARQL 查询时遇到问题。我有两个图表:学科和教授。图 Discipline 包含不同的学科,并通过教授的 id 与教授相关联。例如,我有:

<http://www.rdcproject.com/graph/disciplines> {
    <http://www.rdfproject.com/77803>
        a       <http://www.rdfproject.com/Discipline> ;
        <http://www.rdfproject.com/cfu>
                "6" ;
        <http://www.rdfproject.com/disciplineAbbreviation>
                "SE" ;
        <http://www.rdfproject.com/disciplinename>
                "Software Engineering" ;
        <http://www.rdfproject.com/hasCourseof>
                <http://www.rdfproject.com/8028> ;
        <http://www.rdfproject.com/idDiscipline>
                "77803" ;
        <http://www.rdfproject.com/isTaughtBy>
                <http://www.rdfproject.com/0009004> , 
                <http://www.rdfproject.com/0004003> ;
        <http://www.rdfproject.com/obligatory>
                "false" ;
        <http://www.rdfproject.com/semester>
                "1" ;
        <http://www.rdfproject.com/totalhours>
                "50" ;
        <http://www.rdfproject.com/weekhours>
                "5" ;
        <http://www.rdfproject.com/year>
                "1" .
}

本课程由两位教授讲授:

<http://www.rdfproject.com/0009004>
        a       <http://www.rdfproject.com/Teacher> ;
        <http://www.rdfproject.com/firstName>
                "Koby" ;
        <http://www.rdfproject.com/idProfessor>
                "0009004" ;
        <http://www.rdfproject.com/lastName>
                "Bryant" ;
        <http://www.rdfproject.com/role>
                "contratto" .

<http://www.rdfproject.com/0004003>
        a       <http://www.rdfproject.com/Teacher> ;
        <http://www.rdfproject.com/firstName>
                "Lebron" ;
        <http://www.rdfproject.com/idProfessor>
                "0004003" ;
        <http://www.rdfproject.com/lastName>
                "James" ;
        <http://www.rdfproject.com/role>
                "associato" .

现在我想要所有学科的所有教授。我创建了这个查询:

PREFIX uni: <http://www.rdfproject.com/>
PREFIX un: <http://www.w3.org/2007/ont/unit#>

SELECT  ?idDiscipline ?disciplineName  
        (CONCAT('[',GROUP_CONCAT(?idProf;separator=","),', ',GROUP_CONCAT(?firstName;separator=","),', ',GROUP_CONCAT(?lastName;separator=","),', ',GROUP_CONCAT(?role;separator=","),']') as ?professors)
        
FROM <http://www.rdcproject.com/graph/disciplines>
FROM <http://www.rdcproject.com/graph/professor>
WHERE
{ 
    {
    ?x  a uni:Discipline;
    uni:disciplinename ?disciplineName;
    uni:idDiscipline ?idDiscipline;
    uni:disciplineAbbreviation ?sigleDiscipline;
    uni:cfu ?cfu;
    uni:hasCourseof ?hasCourseof;
    uni:obligatory ?obligatory;
    uni:semester ?semester;
    uni:totalhours ?totalhours;
    uni:weekhours ?weekhours;
    uni:year ?year;
    uni:isTaughtBy ?isTaughtBy.
    ?isTaughtBy a uni:Teacher;
        uni:idProfessor ?idProf;
        uni:firstName ?firstName;
        uni:role ?role;
        uni:lastName ?lastName.
    }
}
GROUP BY ?idDiscipline ?disciplineName 

如果一个学科只包含一位教授,则此查询可以正常工作,但在这种情况下,结果是:

ID -> 77803"  
NAME -> "Software Engineering" 
PROFESSOR -> "[0009004,0004003, Kobe,Lebron, Bryant ,James, 
        contract,contract]"

我怎样才能得到这个结果?

ID -> 77803"  
NAME -> "Software Engineering" 
PROFESSOR -> "[0009004, Kobe, Bryant, contract] 
              [0004003, Lebron,James, contract]

谢谢

编辑: 感谢@AKSW 我的新查询是:

PREFIX uni: <http://www.rdfproject.com/>
PREFIX un: <http://www.w3.org/2007/ont/unit#>

SELECT  ?idDiscipline ?sigleDiscipline ?disciplineName ?cfu ?hasCourseof ?obligatory ?semester ?totalhours ?weekhours ?year 
(GROUP_CONCAT(DISTINCT ?prof_str;separator=",") AS ?Professor)

FROM <http://www.rdcproject.com/graph/disciplines>
FROM <http://www.rdcproject.com/graph/professor>
WHERE
{ 
    {       
    ?x  a uni:Discipline;
    uni:disciplinename ?disciplineName;
    uni:idDiscipline ?idDiscipline;
    uni:disciplineAbbreviation ?sigleDiscipline;
    uni:cfu ?cfu;
    uni:hasCourseof ?hasCourseof;
    uni:obligatory ?obligatory;
    uni:semester ?semester;
    uni:totalhours ?totalhours;
    uni:weekhours ?weekhours;
    uni:year ?year;
    uni:isTaughtBy ?isTaughtBy.
        ?isTaughtBy a uni:Teacher;
        uni:idProfessor ?idProf;
        uni:firstName ?firstName;
        uni:lastName ?lastName;
        uni:role ?role.
        
    }

    BIND(CONCAT('[',?idProf,',',?firstName,',',?lastName,',',?role,']') AS ?prof_str)

}
GROUP BY ?idDiscipline ?sigleDiscipline ?disciplineName ?cfu ?hasCourseof ?obligatory ?semester ?totalhours ?weekhours ?year

标签: sparql

解决方案


推荐阅读