使用聚合函数避免查询中的子选择

时间:2014-07-31 10:57:48

标签: mysql sql

我有这个问题:

SELECT A.msisdn, A.times, sum(A.user_usage) as 'usage' 
FROM (SELECT S.msisdn, S.times, D.user_usage, 
             IF (S.times = 0, S.activation_date, S.last_attribution_date) as ref_date,  
             DATEDIFF(NOW(), IF (S.times = 0,  S.activation_date, S.last_attribution_date) ) as days, 
             D.transdate 
     from subs S 
     INNER JOIN daily_usage D ON (S.msisdn=D.msisdn) 
     where S.status=1) as A 
where A.days % 30 = 0 
    AND A.transdate between A.ref_date AND DATE(now()) 
    AND A.times <= 6 
group by A.msisdn, A.times;

我想知道是否有办法在上述陈述中避免使用subselect。

感谢。

0 个答案:

没有答案