我正在尝试计算新列展示次数的值,该值等于(sum_retweet和sum_reply)的总和乘以列followers_count中的值。因此,对于第一行,它将是:(2 + 1)* 812,但条件是sum_retweet和sum_reply的总和必须大于零。如果总和等于零,则展示次数=仅对followers_count。
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, Reach
'9', '2008-06-11', 'A', '2', '1', '812', '1624'
'9', '2008-06-12', 'B', '0', '1', '813', '813'
这是我目前的代码:
CREATE VIEW `tweet_sum` AS
select `tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
MAX(`tweets`.`followers_count`) * ((SUM(`tweets`.`is_reply`) > 0) + (SUM(`tweets`.`is_retweet`) > 0)) as reach
from `tweets`
group by cast(`tweets`.`datetime` as date), tweets.username;
请问我是否在<?p>中添加了展示次数列的计算
答案 0 :(得分:2)
为此使用案例陈述:
case when SUM(`tweets`.`is_reply`) + SUM(`tweets`.`is_retweet`) > 0
then (SUM(`tweets`.`is_reply`) + SUM(`tweets`.`is_retweet`))
* `tweets`.`followers_count`
else `tweets`.`followers_count` END as newColumn
答案 1 :(得分:1)
这可以在SQL中实现,但是这种语法在mysql中可能不起作用(不知道):
CASE WHEN (Sum_Retweet + Sum_Reply) > 0
THEN (Sum_Retweet + Sum_Reply) * followers_count
ELSE followers_count
END
字段名称需要更改为您的实际字段名称。