我有一个名为'rawTweets'的表,如下所示
tweet, date, id, username, compoundScore
我想计算某些关键字的平均值,并按日期对结果进行分组,但是有点卡住(仍然是一个sql'newb')。在我看来,它看起来像下面这样。
可以使用一点指导 - 谢谢!
SELECT
`date`,
AVG(
SELECT compoundScore
FROM rawTweets
WHERE tweet LIKE '%trump%'
) AS Trump,
AVG(
SELECT compoundScore
FROM rawTweets
WHERE tweet LIKE '%chaffetz%'
) as Chaffetz
FROM rawTweets
WHERE
compoundScore <> 0.0
AND compoundScore IS NOT NULL
GROUP BY `date`;
答案 0 :(得分:0)
您可以尝试这一个配偶,而不是使用nested select
:
SELECT
`date`,
AVG(COALESCE(rt2.compoundScore, 0)) AS Trump,
AVG(COALESCE(rt3.compoundScore, 0)) AS Chaffetz
FROM
rawTweets rt
-- Trump
LEFT JOIN rawTweets rt2 ON
rt2.id = rt.id
AND rt2.tweet LIKE '%trump%'
-- Chaffetz
LEFT JOIN rawTweets rt3 ON
rt3.id = rt.id
AND rt3.tweet LIKE '%chaffetz%'
WHERE
compoundScore <> 0.0
AND compoundScore IS NOT NULL
GROUP BY `date`;
答案 1 :(得分:0)
您可以使用avg
和case when
执行此操作:
select date
,avg(case when tweet like '%trump%' then compoundScore else null end) as Trump
,avg(case when tweet like '%chaffetz%' then compoundScore else null end) as Chaffetz
from rawTweets
where compoundScore <> 0.0 and compoundScore is not null
group by date