我正在尝试从问题表中检索字段以及评论表和issue_assigneduser表中的相关记录计数。如果有2条评论和6个指定用户,则我获得的值对于两个计数均为12。知道如何解决这个问题吗?
SELECT issue.issueid, COUNT(comment.commentid) AS CountOfComments,
Count(issue_assigneduser.userid) as CountOfAssignedUsers,
issue.title, issue.detail, issue.enteredby,
issue.datetimeentered, issue.assignedto, issue.categoryid,
issue.severityid, issue.statusid,
issue.lastcommentdatetime as LastCommentDateTime,
issue.lastcommentbyuserid,
users.initials as LastCommentUserInitials,
lookupstatus.status as Status,
lookupcategory.category as Category,
lookupseverity.severity as Severity,
GetUTCDate() as UTCDateTime
FROM issue
INNER JOIN lookupcategory ON issue.categoryid = lookupcategory.categoryid
INNER JOIN lookupseverity ON issue.severityid = lookupseverity.severityid
INNER JOIN lookupstatus ON issue.statusid = lookupstatus.statusid
LEFT OUTER JOIN comment ON issue.issueid = comment.issueid
LEFT OUTER JOIN issue_assigneduser ON issue.issueid = issue_assigneduser.issueid
LEFT OUTER JOIN users ON issue.lastcommentbyuserid = users.userid
GROUP BY issue.issueid, issue.title, issue.detail,
issue.enteredby, issue.datetimeentered,
issue.assignedto, issue.categoryid,
issue.severityid, issue.statusid,
issue.lastcommentdatetime,
issue.lastcommentbyuserid,
users.initials, lookupstatus.status,
lookupcategory.category,
lookupseverity.severityid, users.initials,
lookupstatus.status, lookupcategory.category,
lookupseverity.severity
ORDER BY issue.lastcommentdatetime DESC;
答案 0 :(得分:6)
使用COUNT( DISTINCT fieldname )
代替COUNT ( fieldname )
例如用户
Count(DISTINCT issue_assigneduser.userid) as CountOfAssignedUsers