我想要实现的是获取当前计数和上个月计数,以便我可以做一个公式来获得增长百分比
(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')
My 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 :(得分: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;
希望这有帮助
答案 1 :(得分: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
答案 2 :(得分: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
;
答案 3 :(得分: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;