2014-09-22 210 views
-1

什么即时试图实现的是获得当前计数和上个月计数,所以我可以做一个公式得到的百分比增长MySQL查询获得当月和上月的数量来获得增长的百分比

(CountCurrent - CountLastMonth)/CountLastMonth 

我的表有以下字段

activity, upload_date 

这是查询即时现在尝试。

SELECT 
     Y.CurrentMonth,  Y.CountCurrent, 
     Z.LastMonth, Z.CountLastMonth 


FROM 
     (SELECT 
     upload_date, 
     activity, 
      DATE_FORMAT(upload_date,'%M %Y') AS CurrentMonth, 
     COUNT(activity) AS CountCurrent 
      FROM appmaster 
      WHERE activity = 'com.google.test' 
      GROUP BY DATE_FORMAT(upload_date,'%m%y')) 
    Y INNER JOIN 
     (SELECT 
     activity, 
     DATE_FORMAT(upload_date,'%M %Y') AS CurrentMonth2, 
      DATE_FORMAT(upload_date - INTERVAL 1 MONTH,'%M %Y') AS LastMonth, 
      COUNT(activity) AS CountLastMonth 
     FROM appmaster 
     WHERE activity = 'com.google.test' 
     GROUP BY DATE_FORMAT(upload_date - INTERVAL 1 MONTH,'%m%y')) 
    Z ON Z.CurrentMonth2 = Y.CurrentMonth 

GROUP BY DATE_FORMAT(upload_date,'%Y%m') 
ORDER BY DATE_FORMAT(upload_date,'%Y%m') 

我的CurrentMonth,CountCurrent和LastMonth正常工作。但CountLastMonth与CountCurrent相同。

我之前想这和它会给我的一切,但CountLastMonth

SELECT b.CurrentMonth, sum(b.CountCurrent), b.LastMonth 
FROM (SELECT DATE(a.upload_date - INTERVAL 1 MONTH) AS LastMonth, DATE(a.upload_date) AS CurrentMonth, COUNT(a.activity) AS CountCurrent 
FROM appmaster a WHERE a.activity = 'com.google.android.googlequicksearchbox' 
group BY MONTH(a.upload_date)) AS b 

group BY MONTH(b.CurrentMonth) 

回答

0

没有临时表要求:

SELECT 
    a.ym, 
    CASE @totals 
     WHEN 0 THEN 0 
     ELSE (a.totals - @totals)/@totals 
    END increment, 
    @totals := a.totals totals 
FROM 
    (
    SELECT 
     EXTRACT(YEAR_MONTH FROM upload_date) ym, 
     COUNT(1) AS totals 
    FROM 
     appmaster 
    WHERE 
     activity = 'com.google.test' 
    GROUP BY ym -- no ORDER BY required 
    ) a 
CROSS JOIN (SELECT @totals := 0) x 
0

也许有这样做一个简单的方法,使用与用户变量的小动作。

首先,您需要编写一个查询按月对数据进行分组;我将其存储在一个临时表来缓解事情有点:现在

drop table if exists temp_count; 
create temporary table temp_count 
select last_day(upload_date) as month -- A little trick to get 
             -- the last day of the month 
    , count(activity) as count_current 
    -- Add any other fields or expressions you need 
from app_master 
-- Add the needed joins 
-- Include any WHERE conditions here 
group by last_day(upload_date); 
-- Let's add an index to this temp table... add any indexes you may need 
alter table temp_count 
    add index idx_month(month); 

而且,让我们用这个临时表来获得你所需要的:

select a.month 
    , @count_last as count_last -- This is the value of the user variable 
            -- before reassigning it 
    , (a.count_current - @count_last)/@count_last as increment 
    , @count_last := a.count_current -- Here the variable is updated with the 
             -- current value 
from 
    ( -- This subquery is used to initialize the user variable 
     select @count_last := 0 
    ) as init 
    , temp_count as a 
-- It's important to order the data, otherwise God knows what may happen ;) 
order by a.month; 

希望这有助于

+0

很好的解释,但对方的回答似乎只是要好得多。 – Bignadad 2014-09-22 22:29:40

+0

@Badadad :)确实......但是,对于更复杂的任务,临时表是一种有用的资源(一种方便的“分而治之”的工具,可以在未来帮助你) – Barranka 2014-09-22 23:12:06

0

收入增长每一个月 - 这是基于answer provided by Barranka,但它适用于月收入增长:

SELECT 
    a.YM, 
    CASE @Revenue 
      WHEN 0 THEN 0 
      ELSE (a.Revenue - @Revenue)/@Revenue 
     END Increment, 
     @Revenue := a.Revenue Revenue 
    FROM 
     (
     SELECT 
     LEFT(payment_date, 7) YM, 
     SUM(amount) AS Revenue -- Toatl is SUM of Amount 
     FROM 
      payment 

     GROUP BY YM -- no ORDER BY required 
     ) a 
     CROSS JOIN (SELECT @Revenue := 0) x 
; 


SELECT 
    a.YM, 
    CASE @Revenue 
     WHEN 0 THEN 0 
     ELSE (a.Revenue - @Revenue)/@Revenue 
END Increment, 
    @Revenue := a.Revenue Revenue 
    FROM 
(
SELECT 
     EXTRACT(YEAR_MONTH FROM payment_date) YM, 
     SUM(amount) AS Revenue 
FROM 
     payment 

GROUP BY YM -- no ORDER BY required 
) a 
CROSS JOIN (SELECT @Revenue := 0) x 
; 
0
  #Rental number Growth per month -- This is the example for monthly growth where "Total count of activity per month" is concerned -- This answer was developed based on Barranka answer and credit goes to him. 
     SELECT 
      a.YM, 
      CASE @Num_of_Rentals 
       WHEN 0 THEN 0 
       ELSE (a.Num_of_Rentals - @Num_of_Rentals)/@Num_of_Rentals 
      END increment, 
      @Num_of_Rentals := a.Num_of_Rentals Num_of_Rentals 
     FROM 
      (
      SELECT 
       LEFT(rental_date,7) YM, 
       COUNT(rental_id) AS Num_of_Rentals 
      FROM 
       rental 
       GROUP BY ym -- no ORDER BY required 
      ) a 
     CROSS JOIN (SELECT @Num_of_Rentals := 0) x; 

     -- OR 
     SELECT 
      a.YM, 
      CASE @Num_of_Rentals 
       WHEN 0 THEN 0 
       ELSE (a.Num_of_Rentals - @Num_of_Rentals)/@Num_of_Rentals 
      END increment, 
      @Num_of_Rentals := a.Num_of_Rentals Num_of_Rentals 
     FROM 
      (
      SELECT 
       LEFT(rental_date,7) YM, 
       COUNT(1) AS Num_of_Rentals 
      FROM 
       rental 
       GROUP BY ym -- no ORDER BY required 
      ) a 
     CROSS JOIN (SELECT @Num_of_Rentals := 0) x;