Most time both subject and object of a close/reopen/delete/undelete decision

1

Please login or register to vote for this query.

(click on this box to dismiss)

Woodworking

Q&A for professional and amateur woodworkers

CREATE TABLE #tmp (
  SubjectUserId INT NOT NULL,
  PostHistoryId INT NOT NULL,
  PostHistoryTypeId INT NOT NULL,
  PostId INT NOT NULL,
  ObjectUserId INT NOT NULL
);

INSERT INTO #tmp
SELECT
  CAST(SUBSTRING(spl.value, 1, CHARINDEX(',', spl.value) - 1) AS INT)
    AS SubjectUserId,
  PostHistory.Id,
  PostHistory.PostHistoryTypeId,
  PostHistory.PostId,
  Posts.OwnerUserId AS ObjectUserId
FROM PostHistory
CROSS APPLY STRING_SPLIT(Text, ':') spl
LEFT JOIN Posts ON PostHistory.PostId = Posts.Id
WHERE
  PostHistory.PostHistoryTypeId BETWEEN 10 AND 13
  AND
  SUBSTRING(spl.value, 1, 1) BETWEEN '1' AND '9'
  AND
  Posts.OwnerUserId > 0;/*

Raw2 AS (
  SELECT
    SubjectUserId,
    ObjectUserId,
    COUNT(*) AS Count
  FROM #tmp
  WHERE SubjectUserId <> ObjectUserId
  GROUP BY SubjectUserId, ObjectUserId
)
SELECT
  A.SubjectUserId AS UserAId,
  B.SubjectUserId AS UserBId,
  A.Count AS CountA,
  B.Count AS CountB,
  CASE WHEN A.Count < B.Count THEN A.Count ELSE B.Count END AS Count
FROM Raw2 A, Raw2 B
WHERE
  A.SubjectUserId = B.ObjectUserId
  AND
  A.ObjectUserId = B.SubjectUserId
  AND
  A.SubjectUserId < B.SubjectUserId
ORDER BY Count DESC, UserAId, UserBId

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results