gpt4 book ai didi

mysql - SQL 计算一段时间内的累积值

转载 作者:行者123 更新时间:2023-12-04 10:14:21 24 4
gpt4 key购买 nike

我正在尝试计算自 2020 年 1 月 1 日以来的累计收入。我有以下架构的用户级收入数据

create table revenue
(
game_id varchar(255),
user_id varchar(255),
amount int,
activity_date varchar(255)
);

insert into revenue
(game_id, user_id, amount, activity_date)
values
('Racing', 'ABC123', 5, '2020-01-01'),
('Racing', 'ABC123', 1, '2020-01-04'),
('Racing', 'CDE123', 1, '2020-01-04'),
('DH', 'CDE123', 100, '2020-01-03'),
('DH', 'CDE456', 10, '2020-01-02'),
('DH', 'CDE789', 5, '2020-01-02'),
('DH', 'CDE456', 1, '2020-01-03'),
('DH', 'CDE456', 1, '2020-01-03');

预期产出
Game    Age    Cum_rev    Total_unique_payers_per_game
Racing 0 5 2
Racing 1 5 2
Racing 2 5 2
Racing 3 7 2
DH 0 0 3
DH 1 15 3
DH 2 117 3
DH 3 117 3

年龄计算为交易日期与 2020-01-01 之间的差值。
我正在使用以下逻辑
SELECT game_id, DATEDIFF(activity_date ,'2020-01-01') as Age,count(user_id) as Total_unique_payers
from REVENUE

SQL fiddle
如何计算累计收入?

最佳答案

使用 MySQL 5.7 的唯一方法是使用它的变量系统,尽管它有效。它模拟了@Used_By_Already 在他的 answer 上使用的窗口函数。

既然你提到你关心差距,你首先需要创建一个日期表,这很容易做到:

create table dates_view (
date_day date
);

insert into dates_view
select date_add( '2019-12-31', INTERVAL @rownum:=@rownum+1 day ) as date_day
from (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) a, (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) b, (select @rownum:=0) r;

-- Note: each set of select union above will multiply the number
-- of days by 10, so if you need more days in your table just add more
-- set as above "a" or "b" sets

拥有日期表后,您必须将其与当前的 revenue 交叉连接。表中的问题是您希望玩家数量独立于累积 amount所以你需要在子查询中独立计算它。

您还需要计算 max(activity_date)您的 revenue表以限制结果直到它。

所以下面的查询将做到这一点(基于您当前的示例数据):
set @_sum:=0;       -- Note: this two lines depends on the client
set @_currGame:=''; -- you are using. Some accumulate variable per session
-- some doesn't, below site, for instance does

select a.game_id,
a.age,
case when @_currGame = game_id
then @_sum:=coalesce(samount,0) + @_sum
else @_sum:=coalesce(samount,0) end as Cum_rev,
a.Total_unique_payers_per_game,
@_currGame := game_id varComputeCurrGame
from
(
select players.game_id,
rev.samount,
datediff(dv.date_day, '2020-01-01') age,
players.noPlayers Total_unique_payers_per_game
from (select @_sum:=0) am,
dates_view dv
cross join (select max(activity_date) maxDate from revenue) md
on dv.date_day <= md.maxDate
cross join (select game_id, count(distinct user_id) noPlayers
from revenue group by game_id) players
left join (select game_id, activity_date, sum(amount) samount
from revenue group by game_id, activity_date) rev
on players.game_id = rev.game_id
and dv.date_day = rev.activity_date
) a,
(select @_sum:=0) s,
(select @_currGame='') x
order by a.game_id desc, a.age;

这将导致:
  game_id   age  Cum_rev  Total_unique_payers_per_game   varComputeCurrGame
Racing 0 5 2 Racing
Racing 1 5 2 Racing
Racing 2 5 2 Racing
Racing 3 7 2 Racing
DH 0 0 3 DH
DH 1 15 3 DH
DH 2 117 3 DH
DH 3 117 3 DH

看到它在这里工作(你需要运行它): https://www.db-fiddle.com/f/qifZ6hmpvcSZYwhLDv613d/2

这是支持窗口功能的 MySQL 8.x 版本:
select distinct agetable.game_id,
agetable.age,
sum(coalesce(r1.amount,0))
over (partition by agetable.game_id
order by agetable.game_id, agetable.age) as sm,
agetable.ttplayers
from
(
select r.game_id, dv.date_day, datediff(dv.date_day, '2020-01-01') age, p.ttplayers
from dates_view dv
cross join (select distinct game_id, activity_date from revenue) r
on dv.date_day <= (select max(activity_date) from revenue)
left join (select game_id, count(distinct user_id) ttplayers from revenue group by game_id) p
on r.game_id = p.game_id
group by r.game_id desc, dv.date_day, age, p.ttplayers
) agetable
left join revenue r1
on agetable.date_day = r1.activity_date
and r1.game_id = agetable.game_id
order by agetable.game_id desc, agetable.age

关于mysql - SQL 计算一段时间内的累积值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61151692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com