sql - 将具有 self JOIN 的 SQL 转换为 SPARQL

标签 sql sparql

我正在尝试将 SQL 查询转换为 SPARQL,而且已经很接近了,但我不太明白如何超越 SQL 查询所做的自连接。

这是 SQL(问题:计算 stackoverflow 用户接受的答案百分比):

SELECT 
    ( Count(a.Id) / (SELECT Count(*) FROM posts WHERE OwnerUserId = 45 AND PostTypeId = 2) * 100) AS AcceptedPercentage
FROM
    posts q
  INNER JOIN
    posts a ON q.AcceptedAnswerId = a.Id
WHERE
    a.OwnerUserId = 45
  AND
    a.PostTypeId = 2;

这是迄今为止我在 SPARQL 中获得的内容:

SELECT
    (count(?answers) AS ?totalAnswers) 
    (count(?acceptedAnswers) AS ?totalAcceptedAnswers)
    ((count(?acceptedAnswers)/count(?answers))*100 AS ?acceptedPercentage)
WHERE {
    {
        #all answers
        ?answers a vocab:posts .

        #user 45
        ?answers vocab:posts_OwnerUserId 45 .

        #accepted answers
        ?answers vocab:posts_PostTypeId 2 .

        #set the answers id variable
        ?answers vocab:posts_Id ?answerId .

    } UNION {

        #all answers
        ?acceptedAnswers a vocab:posts .

        #user 45
        ?acceptedAnswers vocab:posts_OwnerUserId 45 .

        #is type answer
        ?acceptedAnswers vocab:posts_PostTypeId 2 .

        #accepted answer ID matches the overal ID of the question
        ?acceptedAnswers vocab:posts_AcceptedAnswerId ?acceptedAnswerId
        FILTER( ?answerId = ?acceptedAnswerId )

    }
}

以下是典型帖子中包含的数据(其中 vocab:posts_PostTypeId=2 表示“答案”):

vocab:posts_AcceptedAnswerId    104 -
vocab:posts_AnswerCount         11  -
vocab:posts_Body                "here's the body of the question"   -
vocab:posts_CommentCount        0   -
vocab:posts_CreationDate        "2009-04-30T07:48:06"^^xsd:dateTime -
vocab:posts_FavoriteCount       11  -
vocab:posts_Id                  3   -
vocab:posts_LastActivityDate    "2009-06-05T04:01:09"^^xsd:dateTime -
vocab:posts_LastEditDate        "2009-04-30T08:05:02"^^xsd:dateTime -
vocab:posts_LastEditorUserId    22  -
vocab:posts_OwnerUserId         22  -
vocab:posts_PostTypeId          1   -
vocab:posts_Score               21  -
vocab:posts_Tags                "<unix><package-management><server-management>" -
vocab:posts_Title               "Best practices for keeping UNIX packages up to date?"  -
vocab:posts_ViewCount           548 -
rdf:type                        vocab:posts -
rdfs:label                      "posts #3"

这将如何在 SPARQL 中执行?

最佳答案

一些数据

如果我们有一些示例数据可以使用,那就更容易了。根据您正在使用的属性(尽管为了便于阅读,我稍微更改了名称),这里有一些示例数据,描述了十个答案,其中六个已被接受:

@prefix : <http://stackoverflow.com/q/28101179/1281433/> .

:answer0 a :Post ; :hasId 0 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question0 a :Post ; :hasAcceptedAnswer 0.
:answer1 a :Post ; :hasId 1 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question1 a :Post ; :hasAcceptedAnswer 1.
:answer2 a :Post ; :hasId 2 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question2 a :Post ; :hasAcceptedAnswer 2.
:answer3 a :Post ; :hasId 3 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question3 a :Post ; :hasAcceptedAnswer 3.
:answer4 a :Post ; :hasId 4 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question4 a :Post ; :hasAcceptedAnswer 4.
:answer5 a :Post ; :hasId 5 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question5 a :Post ; :hasAcceptedAnswer 5.
:answer6 a :Post ; :hasId 6 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question6 a :Post ; :hasAcceptedAnswer 96.
:answer7 a :Post ; :hasId 7 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question7 a :Post ; :hasAcceptedAnswer 97.
:answer8 a :Post ; :hasId 8 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question8 a :Post ; :hasAcceptedAnswer 98.
:answer9 a :Post ; :hasId 9 ; :hasOwnerUserId 45 ; :hasPostTypeId 2 . :question9 a :Post ; :hasAcceptedAnswer 99.

这有点简洁,但每一行都有这样的数据,一个问题和一个答案:

:answer9  a              :Post ;
        :hasId           9 ;
        :hasOwnerUserId  45 ;
        :hasPostTypeId   2 .

:question9  a               :Post ;
        :hasAcceptedAnswer  99 .

一些 SPARQL

现在我们可以使用这样的查询来获取我们正在寻找的结果:

prefix : <http://stackoverflow.com/q/28101179/1281433/>

select (count(?answer) as ?nanswers) 
       (count(?question) as ?naccepted)
       (100*?naccepted/?nanswers as ?percentAccepted)
where { 
  ?answer a :Post ;
          :hasId ?id ;
          :hasOwnerUserId 45 ;
          :hasPostTypeId 2 .
  optional { 
    ?question a :Post ;
              :hasAcceptedAnswer ?id .
  }
}
------------------------------------------
| nanswers | naccepted | percentAccepted |
==========================================
| 10       | 6         | 60.0            |
------------------------------------------

关于 SQL

值得注意的是,在 SPARQL 查询中,我使用了可选项,因此最终得到的“行”如下:

-------------------------
| answer   | question   |
=========================
| :answer0 | :question0 |
| :answer1 | :question1 |
| :answer2 | :question2 |
| :answer3 | :question3 |
| :answer4 | :question4 |
| :answer5 | :question5 |
| :answer6 |            |
| :answer7 |            |
| :answer8 |            |
| :answer9 |            |
-------------------------

实际上,您也可以在 SQL 版本中执行此操作,但不能使用内连接。我找到了Jeff Atwood's A Visual Explanation of SQL Joins这里有用。您实际上正在寻找左外连接:

left join screenshot from atwood article

例如,this query on the Stack Exchange Data Explorer计算我的答案已被接受的百分比(但 UserId 是一个参数,因此您可以轻松更改它):

select count(a.id) as nAnswers,
       count(q.id) as nAccepted,
       100.0*count(q.id)/count(a.id) as rate
from Posts a
left outer join Posts q
     on q.AcceptedAnswerId = a.id
where a.OwnerUserId = ##UserId##
      and a.PostTypeId = 2

鉴于此,我们可以进行更多的逐行比较。可选值负责左外连接,并且隐式地表明它基于哪些变量;这是他们有共同的变量。在本例中,这只是 ?id

select (count(?answer) as ?nanswers)
       (count(?question) as ?naccepted)
       (100*?naccepted/?nanswers as ?percentAccepted)
where { 
  ?answer a :Post ;                     #-- from Posts a
          :hasId ?id ;                  
          :hasOwnerUserId 45 ;          #-- where a.OwnerUserId = 45
          :hasPostTypeId 2 .            #--       and a.PostTypeId = 2
  optional {                            #-- left outer join on a.id 
    ?question a :Post ;                 #-- from Posts q
              :hasAcceptedAnswer ?id .  
  }
}

不过,实际上也可以转换使用子查询的原始 SQL 查询。它看起来更像是这样的:

prefix : <http://stackoverflow.com/q/28101179/1281433/>

select ?nAnswers ?nAccepted (100*?nAccepted/?nAnswers as ?percentAccepted)
where { 
  { 
    select (count(?answer) as ?nAnswers) where {
      ?answer a :Post ;
             :hasId ?id ;
             :hasOwnerUserId 45 ;
             :hasPostTypeId 2 .
    }
  }
  {
    select (count(?answer) as ?nAccepted) where {
      ?answer a :Post ;
              :hasId ?id ;
              :hasOwnerUserId 45 ;
              :hasPostTypeId 2 .
      ?question a :Post ;
               :hasAcceptedAnswer ?id .
    }
  }
}
------------------------------------------
| nAnswers | nAccepted | percentAccepted |
==========================================
| 10       | 6         | 60.0            |
------------------------------------------

关于sql - 将具有 self JOIN 的 SQL 转换为 SPARQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28101179/

相关文章:

mysql - 选择另一个表中没有外键的主键

sql - 获取两个值彼此唯一的行

mysql - 如何根据字段的计数选择值?

mysql - 如何按多个条件对 MySQL 查询进行分组?

sql - 查询 SSRS 报告的存储过程

SPARQL查询大学

sparql - 命名图和联合 SPARQL 端点

sparql - 查询WikiData,p和wdt默认前缀的区别

sparql - 维基数据 SPARQL 查询演职人员

java - Sparql 插件使用