我在MYSQL中有一个twitter数据表,其中列is_retweet,is_reply由二进制值组成,其中1 =是,0 =否。如果用户在一天内转发了多次,那么当天该转发的用户将会有多行的转发。
account_id, datetime, user_screenname, is_retweet, is_reply,followers_count
'9', '2008-06-11 20:06:35','Access2', '1', '0', '811'
'9', '2008-06-11 23:06:35','Access2', '1', '1', '812'
'9', '2008-06-12 20:01:21','Access2', '0', '1', '813'
'7', '2008-06-11 17:01:00','actingparty', '1', '1', '2000'
我将我的sql输出重新排列到下面的表格中,告诉我:对于任何一天的用户名,转发,回复和最高关注者数量的总数是多少。
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count
'9', '2008-06-11', 'Access2', '2', '0', '812'
'9', '2008-06-12', 'Access2', '0', '1', '813'
这是我的sql代码:
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`,
from
`tweets`
group by cast(`tweets`.`datetime` as date), tweets.username
最终,我想再增加一个列Reach(等于followers_count乘以大于零的列数(is_retweet,is_reply)。) 例如,在下面的输出表中,2008-06-11的sum_retweet和sum_reply列都大于零,因此我需要对达到列采用followers_count * 2 = 1624.
我如何构建我的sql代码来做到这一点?
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, **Reach**
'9', '2008-06-11', 'Access2', '2', '1', '812', '1624'
'9', '2008-06-12', 'Access2', '0', '1', '813', '813'
我想过这样做:
1.create a new view
2.count the number of columns that have values >0
3.then take that number multiply by followers count for that day
以下代码:
CREATE VIEW tweet_reach AS
SELECT
COUNT(t.sum_reply,t.sum_retweet,t.sun_mention,t.sum_direct,t.sum_mytweet)*t.followers_count AS Reach
FROM information_schema.columns
WHERE table_name='tweet_sum' t AND
t.sum_reply>0 OR
t.sum_retweet>0 OR
t.sun_mention>0 OR
t.sum_direct>0 OR
t.sum_mytweet>0;
这段代码错了,但希望能做到这样的事情。有可能吗?
谢谢, Ĵ
答案 0 :(得分:0)
您可以通过在现有视图中添加列来轻松完成此操作:
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;
MySQL将布尔表达式(如x = y
)视为整数1
(如果为真),将0
视为假。因此,您可以将它们加在一起作为乘法因子。