Thursday 15 April 2010

mysql - sql multiplication on condition -



mysql - sql multiplication on condition -

i trying calculate values new column impressions equal sum of ( sum_retweet , sum_reply ) multiply value in column followers_count. first row : (2+1)*812 on status total sum of sum_retweet , sum_reply must greater zero. if sum equal 0 impressions = 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'

here current code:

create view `tweet_sum` select `tweets`.`account_id` `account_id`, `tweets`.`user_screenname` `user_screenname`, cast(`tweets`.`datetime` date) `period`, max(`tweets`.`followers_count`) `followers_count`, sum(`tweets`.`is_reply`) `sum_reply`, sum(`tweets`.`is_retweet`) `sum_retweet`, max(`tweets`.`followers_count`) * ((sum(`tweets`.`is_reply`) > 0) + (sum(`tweets`.`is_retweet`) > 0)) reach `tweets` grouping cast(`tweets`.`datetime` date), tweets.username;

how add together calculations impressions column in?

use case statement this:

case when sum(`tweets`.`is_reply`) + sum(`tweets`.`is_retweet`) > 0 (sum(`tweets`.`is_reply`) + sum(`tweets`.`is_retweet`)) * `tweets`.`followers_count` else `tweets`.`followers_count` end newcolumn

mysql sql

No comments:

Post a Comment