首页 > 解决方案 > SPARQL 中计数列的总和

问题描述

嗨,我是 SPARQL 的新手,需要帮助。我编写了一个查询,其中 GROUP_CONCAT 我的图像,然后我对我的图像字段进行了计数,结果如下所示

在此处输入图像描述

我现在想要的是 imageCount 字段的总和,如下图所示。在这个例子中,总数为 6

在此处输入图像描述

SELECT ?id  
       (GROUP_CONCAT(?images; separator=";") as ?images) 
       (count( ?images) as ?imageCount )   
{
  GRAPH <virtual://sample> {
     ....
    ....

  }
      GROUP BY ?id
      LIMIT 10

对此有任何帮助!谢谢

标签: countsumsparql

解决方案


@fasalshah 评论的长度有限,因此发布相关数据作为答案

是的,使用以下示例数据:

insert data {
    <id:1> <id:image> "aq.png" ; <id:select> 1 .
    <id:2> <id:image> "a.png", "ab.png" ; <id:select> 1 .
    <id:3> <id:image> "qw.png" ; <id:select> 1 .
    <id:4> <id:image> "d1.png", "d2.png" ; <id:select> 1 .
    <id:5> <id:image> "x.png", "y.png" ; <id:select> 2 .
}

和以下查询:

select ?key (group_concat(?image;separator=";") as ?list) (count(*) as ?count) where { 
    ?id <id:image> ?image .
    ?id <id:select> 1 .
    {
    } union {
        bind("this row accumulates everything" as ?totals)
    }
    bind (coalesce(?totals, ?id) as ?key)
} group by ?key

推荐阅读