hi the screenshot I uploaded is table with first column post_id, score, answerCount, CommentCount 您好,我被困在一个蜂巢问题上,我在sql和蜂巢中非常陌生。我正在处理堆栈溢出数据集,我试图找到所回答问题的百分比。我所做的是我数了所有问题,并数了所有已经回答的问题,但是我坚持如何减去它们
select AnswerCount
> from posts
> LEFT JOIN posts
> ON AnswerCount = AnswerCount
> WHERE AnswerCount IS NULL;
我希望结果为全部-已回答的问题数 有些answerCounts为null我这样做是为了计算答案
`select AnswerCount
>from posts
>where AnswerCount > 0;`
这是架构
post_id score AnswerCount CommentCount
385106 2 NULL 0
385107 2 0 2
385108 14 NULL 4
385109 -2 NULL 3
385110 8 NULL 5
385113 -8 NULL 2
385114 16 NULL 0
385116 30 2 6
385118 -2 NULL 0
答案 0 :(得分:0)
更新了我的答案以进行清理。
已签出:
except Exception
该查询包含一个子查询,该子查询选择 SELECT
CAST(( SELECT COUNT(ua.post_id) FROM posts ua
WHERE ua.AnswerCount IS NOT NULL) AS DECIMAL(3,2)) /
CAST(COUNT(t.post_id) AS DECIMAL(3,2))
FROM posts t
个帖子中的COUNT()
,将其除以帖子总数。其余部分将AnswerCount IS NULL
的整数CAST
保留,因为如果将派生结果保留为DECIMAL
,则会报告为0
。
答案 1 :(得分:0)
SELECT SUM(if(AnswerCount IS NULL OR AnswerCount = 0, 1, 0))/COUNT(*) * 100 as Percent_unanswered
FROM posts;