gpt4 book ai didi

SQL - 使用 LEFT OUTER JOIN 计数不正确

转载 作者:行者123 更新时间:2023-12-05 00:37:33 25 4
gpt4 key购买 nike

我正在尝试从问题表中检索字段以及来自评论表和 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;

最佳答案

使用 COUNT( DISTINCT fieldname )而不是 COUNT ( fieldname )
例如对于用户

Count(DISTINCT issue_assigneduser.userid) as CountOfAssignedUsers

关于SQL - 使用 LEFT OUTER JOIN 计数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6728720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com