[英]how to subtract count values in hive in same table same column
嗨,我上傳的屏幕截圖是帶有第一列post_id,score,answerCount,CommentCount的表。嗨,我被困在一個蜂巢問題上,我在sql和hive中非常陌生。我正在處理堆棧溢出數據集,我正在嘗試查找已回答問題的百分比。 我所做的是我數了所有問題,並數了所有已回答的問題,但我堅持如何減去它們
select AnswerCount
> from posts
> LEFT JOIN posts
> ON AnswerCount = AnswerCount
> WHERE AnswerCount IS NULL;
我希望結果是全部-回答一些answerCounts的問題數為空我這樣做是為了計算答案
`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
更新了我的答案以進行清理。
這簽出:
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
該查詢包含一個子查詢,該子查詢選擇AnswerCount IS NULL
的帖子的COUNT()
,將其除以帖子的總數。 剩下的就是CAST
的整數DECIMAL
因為派系的結果將被報告為0
,如果不為int
。
SELECT SUM(if(AnswerCount IS NULL OR AnswerCount = 0, 1, 0))/COUNT(*) * 100 as Percent_unanswered
FROM posts;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.