Retrieve count of total no of answers corresponding to each tag at user level

谁都会走 提交于 2020-04-18 06:33:09

问题


I am trying to create a query, to find the count of total number of answers (that I have give in stackoverflow) corresponding to each tag.

Through this, I am able to find total count of my accepted, scored and unscored answers.

And using this, I am able to find how many upvotes do I have of each tag.

I ran the below query in the data.StackExcange to get the desired result, but I did not succeed.

This is my query:

DECLARE @UserId int = ##UserId##

SELECT --TOP 20 
    TagName,
    count(a.Id) as [Accepted Answers]
from
    Posts q
  inner join
    Posts a
  on a.Id = q.AcceptedAnswerId

WHERE 
    Posts.OwnerUserId = @UserId
    a.CommunityOwnedDate is null
  and a.OwnerUserId = ##UserId##
  and q.OwnerUserId != ##UserId##
  and a.postTypeId = 2
GROUP BY TagName 

Update 1:

I also have to find url of the questions, title and all the other tags corresponding to that answer.


回答1:


Below query finds the total number of answers against each tag for a user. It does not consider the self-answered questions of the user, as it can give little additional count.

--Self answered questions dont count
select t.TagName, COUNT(q.Id) as countofAnsweredQuestions
from Posts q
inner join PostTags AS pt
ON pt.PostId = q.Id
inner join Posts a
on a.parentId = q.Id
inner join Tags as t
on pt.tagId = t.Id
where q.CommunityOwnedDate is null and q.ClosedDate is null 
  and a.OwnerUserId = ##UserId##
  and q.OwnerUserId != ##UserId##
  and a.postTypeId = 2
GROUP BY t.TagName
ORDER BY countofAnsweredQuestions desc

I have created permanent link for the query




回答2:


If you're looking for the COUNT() of your accepted answers for each tag then

SELECT T.TagName, COUNT(Q.Id) CntAcceptedAnswer
FROM Posts Q
JOIN PostTags PT ON Q.Id = PT.PostId
JOIN Tags T ON T.Id = PT.TagId
JOIN Posts A ON Q.AcceptedAnswerId = A.Id
WHERE A.OwnerUserId = <YourUserId>
      AND A.PostTypeId = 2
      AND Q.ClosedDate IS NULL
      AND Q.DeletionDate IS NULL
GROUP BY T.TagName;

If you're looking for all your answers for each tag then

SELECT T.TagName, COUNT(A.Id) Cnt
FROM Posts A 
JOIN Posts Q ON Q.Id = A.ParentId
JOIN PostTags PT ON Q.Id = PT.PostId
JOIN Tags T ON T.Id = PT.TagId
WHERE A.OwnerUserId = <YourUserId>
-- You can filter closed and deleted posts here as well if needed
GROUP BY T.TagName;

UPDATE:

For your update

I also need to find url of the questions, title and all the other tags related to that answer.

You could use the query as a subquery and join it with Posts table to get the other columns as well.



来源:https://stackoverflow.com/questions/60967044/retrieve-count-of-total-no-of-answers-corresponding-to-each-tag-at-user-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!