Sparql - 连接节点的多个类别的计数

Sparql - Count of multiple categories of connected nodes

在我的图表中,一个类别 'File' 的节点 'Owns' 许多不同类别的节点。我正在尝试获取文件列表,包括特定类别的连接节点数。

我目前最好的查询是这样的:

SELECT ?uuid ?fileName ?tableCount ?toCount
WHERE {
    ?uuid 'Category' 'File' .
    ?uuid 'Name' ?fileName .
    {
        SELECT (COUNT(*) as ?tableCount)
        WHERE
        {
            VALUES (?category ) { ('BaseTable') }
            ?uuid 'Owns' ?elemUUID .
            ?elemUUID 'Category' ?category .
        }   
    }
    {
        SELECT (COUNT(*) as ?toCount)
        WHERE
        {
            VALUES (?category ) { ('TableOccurrence') }
            ?uuid 'Owns' ?elemUUID .
            ?elemUUID 'Category' ?category .
        }   
    }
}

输出是一个不同的文件列表,但计数是所有文件中该类别的计数(即每个文件共享相同的 ?toCount 值,并且每个文件共享相同的 ?tableCount 值)。

对于子查询的工作方式,我显然有一些不理解的地方。如有任何帮助,我们将不胜感激。

内部查询中的变量不在外部范围内,除非它们出现在选择中。 你应该做的是这样的查询(请原谅我的编辑,但这也会让其他用户更容易理解):

SELECT ?uuid ?fileName ?tableCount ?toCount
WHERE {
    ?uuid :category 'File' .
    ?uuid :name ?fileName .
    {
        SELECT ?uuid (COUNT(*) as ?tableCount)
        WHERE
        {
            VALUES (?category ) { ('BaseTable') }
            ?uuid :owns ?elemUUID .
            ?elemUUID :category ?category .
        }
        GROUP BY ?uuid   #This is the missing link
    }
    {
        SELECT ?uuid (COUNT(*) as ?toCount)
        WHERE
        {
            VALUES (?category ) { ('TableOccurrence') }
            ?uuid :owns ?elemUUID .
            ?elemUUID :category ?category .
        } 
        GROUP BY ?uuid  #and here again
    }
}