2014-03-04 35 views
0

我有一個MYSQL中的twitter數據表,其中列is_retweet,is_reply由二進制值組成,其中1 =是,0 =否。如果用戶在一天內多次轉發,那麼該用戶在當天的轉推庫侖中將會有多行。在有條件的基礎上計算列並在一個VIEW中執行乘法MYSQL

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 

最後,我希望有一個更欄河段(等於FOLLOWERS_COUNT次列(is_retweet數量,is_reply)大於零)。 例如,在下面的輸出表中,2008-06-11的sum_retweet和sum_reply列都大於零,因此我需要爲reach列取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

您可以通過在現有視圖中添加一列做到這一點很容易:

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時假。所以,你可以將它們加在一起以獲得倍增因子。

+0

太棒了,工作完美。謝謝! – jxn

+0

如果不是僅僅檢查列是否說'is_reply'大於0,那麼讓值= 1,如果我對is_reply的值感興趣(例如值爲3),我希望MAX(' tweets'.''lowlowers_count')* 3給定值> 0? – jxn

+0

@jenn。 。 。這可能是最好的另一個問題。 –