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)

IT Security

Q&A for information security professionals

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;

DELETE FROM #tmp WHERE ObjectUserId = SubjectUserId;

WITH S AS (
  SELECT
    SubjectUserId AS UserId,
    COUNT(*) AS Count
  FROM #tmp
  GROUP BY SubjectUserId
),
O AS (
  SELECT
    ObjectUserId AS UserId,
    COUNT(*) AS Count
  FROM #tmp
  GROUP BY ObjectUserId
)
SELECT
  S.UserId AS [User Link],
  S.Count AS Subject,
  O.Count AS Object,
  CASE WHEN S.Count < O.Count THEN S.Count ELSE O.Count END AS Count
FROM S, O
WHERE S.UserId = O.UserId
ORDER BY Count DESC, S.UserId DESC

Enter Parameters

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