SQL 查找互惠关系

SQL Find Reciprocal Relationship

我正在尝试使用 Stack Exchange Data Explorer (SEDE) 寻找一种情况,其中 Stack Overflow 上的两个不同用户已经接受了彼此的回答。例如:

Post A { Id: 1, OwnerUserId: "user1", AcceptedAnswerId: "user2" }

Post B { Id: 2, OwnerUserId: "user2", AcceptedAnswerId: "user1" }

我目前有一个查询可以找到两个用户 协作 作为提问者-回答者的问题,但它不能确定这种关系是否是互惠的:

SELECT user1.Id AS User_1, user2.Id AS User_2
FROM Posts p
INNER JOIN Users user1 ON p.OwnerUserId = user1.Id
INNER JOIN Posts p2 ON p.AcceptedAnswerId = p2.Id
INNER JOIN Users user2 ON p2.OwnerUserId = user2.Id
WHERE p.OwnerUserId <> p2.OwnerUserId
AND p.OwnerUserId IS NOT NULL
AND p2.OwnerUserId IS NOT NULL
AND user1.Id <> user2.Id
GROUP BY user1.Id, user2.Id HAVING COUNT(*) > 1;

对于不熟悉架构的任何人,有两个表如下所示:

Posts
--------------------------------------
Id                      int
PostTypeId              tinyint
AcceptedAnswerId        int
ParentId                int
CreationDate            datetime
DeletionDate            datetime
Score                   int
ViewCount               int
Body                    nvarchar (max)
OwnerUserId             int
OwnerDisplayName        nvarchar (40)
LastEditorUserId        int
LastEditorDisplayName   nvarchar (40)
LastEditDate            datetime
LastActivityDate        datetime
Title                   nvarchar (250)
Tags                    nvarchar (250)
AnswerCount             int
CommentCount            int
FavoriteCount           int
ClosedDate              datetime
CommunityOwnedDate      datetime

Users
--------------------------------------
Id                      int
Reputation              int
CreationDate            datetime
DisplayName             nvarchar (40)
LastAccessDate          datetime
WebsiteUrl              nvarchar (200)
Location                nvarchar (100)
AboutMe                 nvarchar (max)
Views                   int
UpVotes                 int
DownVotes               int
ProfileImageUrl         nvarchar (200)
EmailHash               varchar (32)
AccountId               int

以下是我的处理方法。这是一些简化的数据:

if object_id('tempdb.dbo.#Posts') is not null drop table #Posts
create table #Posts
(
    PostId char(1),
    OwnerUserId int,
    AcceptedAnswerUserId int
)

insert into #Posts
values
('A', 1, 2),
('B', 2, 1),
('C', 2, 3),
('D', 2, 4),
('E', 3, 1),
('F', 4, 1)

为了我们的目的,我们并不真正关心 PostId,我们的起点是一组有序的 post 所有者对(OwnerUserId ) 和接受的回答者 (AcceptedAnswerUserId).

(虽然不是必须的,你可以像这样形象化集合)

select distinct OwnerUserId, AcceptedAnswerUserId
from #Posts

现在我们要查找此集合中所有将这两个字段颠倒的条目。 IE。其中所有者 if one post 是另一个接受的回答者。所以在一对是 (1, 2) 的地方,我们想要找到 (2, 1).

我使用左联接执行此操作,因此您可以看到它省略的行,但将其更改为内部联接会将其限制为您描述的集合。您可以随心所欲地收集信息(通过从帽子中挑选任何列,或者如果您希望它们在一行中,则从一个表中返回两列)。

select 
    u1.OwnerUserId, 
    u1.AcceptedAnswerUserId, 
    u2.OwnerUserId, 
    u2.AcceptedAnswerUserId
from #Posts u1
left outer join #Posts u2
    on u1.AcceptedAnswerUserId = u2.OwnerUserId
        and u1.OwnerUserId = u2.AcceptedAnswerUserId

编辑 如果您想排除自己的回答,只需将 and u1.AcceptedAnswerUserId != u1.OwnerUserId 添加到 on 子句。

就我个人而言,我一直觉得 SQL 和关系代数在集合论中的根深蒂固很有趣,但是在 SQL 中进行这样的基于集合的操作往往会让人觉得很笨重。主要是因为为了保持无序,您必须在单个列中表示集合成员。但是要比较 SQL 中的集合成员,您需要将集合成员表示为单独的列。

现在思考一下,您如何将其扩展到评论同一个 post 的三位用户?

最简单形式的查询(这样查询 1600 万个问题不会超时)将是:

WITH accepter_acceptee(a, b) AS (
    SELECT q.OwnerUserId, a.OwnerUserId
    FROM Posts AS q
    INNER JOIN Posts AS a ON q.AcceptedAnswerId = a.Id
    WHERE q.PostTypeId = 1 AND q.OwnerUserId <> a.OwnerUserId
), collaborations(a, b, type) AS (
    SELECT a, b, 'a accepter b' FROM accepter_acceptee
    UNION ALL
    SELECT b, a, 'a acceptee b' FROM accepter_acceptee
)
SELECT a, b, COUNT(*) AS [collaboration count]
FROM collaborations
GROUP BY a, b
HAVING COUNT(DISTINCT type) = 2
ORDER BY a, b

结果:

一个 CTE 和简单的 inner joins 就可以完成这项工作。不需要像我在其他答案中观察到的那么多代码。注意我的很多评论。

Link 到 StackExchange Data Explorer,样本结果已保存

with questions as ( -- this is needed so that we have ids of users asking and answering
select
   p1.owneruserid as question_userid
 , p2.owneruserid as answer_userid
 --, p1.id -- to view sample ids
from posts p1
inner join posts p2 on -- to fetch answer post
  p1.acceptedanswerid = p2.id
)
select distinct -- unique pairs
    q1.question_userid as userid1
  , q1.answer_userid as userid2
  --, q1.id, q2.id -- to view sample ids
from questions q1
inner join questions q2 on
      q1.question_userid = q2.answer_userid -- accepted answer from someone
  and q1.answer_userid = q2.question_userid -- who also accepted our answer
  and q1.question_userid <> q1.answer_userid -- and we aren't self-accepting

这里以帖子为例:

  • Can I run rubygems in ironruby?Xian 提问,接受了 Orion Edwards
  • 的回答
  • Will the Garbage Collector call IDisposable.Dispose for me?Orion Edwards 提问,已接受来自 Xian
  • 的回答

不过,StackExchange 可能会因为大型数据集和 distinct 部分而使您超时。如果您想查看一些数据,请删除 distinct 并在开始处添加 top N

with questions as (
...
)
select top 3 ...

预计到达时间:糟糕。误读了问题; Op 想要 Accepted 答案,下面是 any 相互答案。 (修改起来很容易,但我还是对后者更感兴趣。)


由于数据集非常大(并且需要不超时 SEDE),我选择限制集合 AMAP 并从那里构建。

所以这个查询:

  1. 只有 returns 存在互惠关系的任何行。
  2. Returns 所有此类问答对。
  3. 不包括自我回答。
  4. 利用 SEDE's query parameters and magic columns 的可用性。

See it live in SEDE.

-- UserA: Enter ID of user A
-- UserB: Enter ID of user B
WITH possibleAnswers AS (
    SELECT
                a.Id                AS aId
                , a.ParentId        AS qId
                , a.OwnerUserId   
                , a.CreationDate
    FROM        Posts a
    WHERE       a.PostTypeId        = 2  --  answers
    AND         a.OwnerUserId       IN (##UserA:INT##, ##UserB:INT##)
),
possibleQuestions AS (
    SELECT
                q.Id                AS qId
                , q.OwnerUserId   
                , q.Tags
    FROM        Posts q
    INNER JOIN  possibleAnswers pa  ON q.Id = pa.qId
    WHERE       q.PostTypeId        = 1  --  questions
    AND         q.OwnerUserId       IN (##UserA:INT##, ##UserB:INT##)
    AND         q.OwnerUserId       != pa.OwnerUserId  --  No self answers
)
SELECT 
            pa.OwnerUserId          AS [User Link]
            , 'answers'             AS [Action]
            , pq.OwnerUserId        AS [User Link]
            , pa.CreationDate       AS [at]
            , pq.qId                AS [Post Link]
            , pq.Tags
FROM        possibleQuestions pq
INNER JOIN  possibleAnswers pa      ON pq.qId = pa.qId
WHERE       pq.OwnerUserId          =  ##UserB:INT##
AND         EXISTS (SELECT * FROM possibleQuestions pq2  WHERE pq2.OwnerUserId =  ##UserA:INT##)

UNION ALL SELECT 
            pa.OwnerUserId          AS [User Link]
            , 'answers'             AS [Action]
            , pq.OwnerUserId        AS [User Link]
            , pa.CreationDate       AS [at]
            , pq.qId                AS [Post Link]
            , pq.Tags
FROM        possibleQuestions pq
INNER JOIN  possibleAnswers pa      ON pq.qId = pa.qId
WHERE       pq.OwnerUserId          =  ##UserA:INT##
AND         EXISTS (SELECT * FROM possibleQuestions pq2  WHERE pq2.OwnerUserId =  ##UserB:INT##)

ORDER BY    pa.CreationDate

它产生的结果如下(点击查看大图):


有关所有此类用户对的列表,请参阅 this SEDE query

使用 中的技术,改进了排序并添加了一些更有用的列。

结合 中的查询,它显示了一些有趣的关系。

See it in SEDE.

WITH QandA_users AS (
    SELECT      q.OwnerUserId   AS userQ
                , a.OwnerUserId AS userA
    FROM        Posts q
    INNER JOIN  Posts a         ON q.AcceptedAnswerId = a.Id
    WHERE       q.PostTypeId    = 1
),
pairsUnion (user1, user2, whoAnswered) AS (
    SELECT  userQ, userA, 'usr 2 answered'
    FROM    QandA_users
    WHERE   userQ <> userA
    UNION ALL
    SELECT  userA, userQ, 'usr 1 answered'
    FROM    QandA_users
    WHERE   userQ <> userA
),
collaborators AS (
    SELECT      user1, user2, COUNT(*) AS [Reciprocations]
    FROM        pairsUnion
    GROUP BY    user1, user2
    HAVING COUNT (DISTINCT whoAnswered) > 1
)
SELECT
            'site://u/' + CAST(c.user1 AS NVARCHAR) + '|Usr ' + u1.DisplayName      AS [User 1]
            , 'site://u/' + CAST(c.user2 AS NVARCHAR) + '|Usr ' + u2.DisplayName    AS [User 2]
            , c.Reciprocations                                                      AS [Reciprocal Accptd posts]
            , (SELECT COUNT(*)  FROM QandA_users qau  WHERE qau.userQ = c.user1)    AS [Usr 1 Qstns wt Accptd]
            , (SELECT COUNT(*)  FROM QandA_users qau  WHERE qau.userQ = c.user1  AND qau.userA = c.user2) AS [Accptd Ansr by Usr 2]
            , (SELECT COUNT(*)  FROM QandA_users qau  WHERE qau.userA = c.user2)    AS [Usr 2 Ttl Accptd Answrs]
FROM        collaborators c
INNER JOIN  Users u1        ON u1.Id = c.user1
INNER JOIN  Users u2        ON u2.Id = c.user2
ORDER BY    c.Reciprocations DESC
            , u1.DisplayName
            , u2.DisplayName

结果如下: