SQL 查询以创建具有不同时间戳和不同列映射的合并 table
SQL query to create a merged table with varied timestamps and varied column mapping
我正在尝试编写一个复杂的 mySQL 查询,其中有 2 个 table 的操作和收入,我需要的是:
- 从拍卖table中取出位置,根据用户的邮政编码,cat_id,猫和花费并加入有收入列的收入table,以便给定cat_id、cat 和 date 我可以计算出每个 'postal' 生成的 returns。
复杂性:
用户在此处是唯一键
在拍卖中 table 有列 'spent' 但只有当 'event' 列有 'show' 但它有 'cat' 条目时才会填充.并且 'cat_id' 在除 show 之外的任何事件中开始填充。因此需要将 cat_id 从 'cat' 映射到事件 'show' 以获得该 cat_id 的花费。
必须设置日期,以便在加入 table 时将时间戳与正负 10 分钟进行比较。现在在我的查询中我有 24 小时的持续时间
按降序聚合邮政给予最高 returns
**Auction Table**
dt user cat_id cat location postal event spent
2020-11-01 22:12:25 1 0 A US X12 Show 2
2020-11-01 22:12:25 1 0 A US X12 Show 2 (duplicate also in table)
2020-11-01 22:12:25 1 6 A US X12 Mid null
2020-11-01 22:13:20 2 0 B UK L23 Show 2
2020-11-01 22:15:24 2 3 B UK L23 End null
**Revenue table**
dt user cat_id revenue
2020-11-01 22:14:45 1 6 null
2020-11-01 22:13:20 2 3 3
想要创建最终 table(通过汇总每个 'postal' 区域的收入):
location postal spend revenue returns
UK X12 2 0 0
US L23 2 3 3/2=1.5
我已经写了一个查询,但无法找出上述 3 个复杂性的解决方案:
Select s.location, s.postal, s.spend, e.revenue
From revenue e JOIN
auction s
on e.user = s.user
where s.event in ('Mid','End','Show') and
TO_DATE(CAST(UNIX_TIMESTAMP(e.dt, 'y-M-d') AS TIMESTAMP)) = TO_DATE(CAST(UNIX_TIMESTAMP(s.dt, 'y-M-d') AS TIMESTAMP)) and
s.cat_id in ('3') and
s.cat = 'B'
任何建议都会有所帮助
这回答了 MySQL 的问题,这是问题的原始标签,也是问题中提到的。
如果我没理解错的话,你的问题是在一个时间范围内“加入”。您可以使用相关子查询做您想做的事。然后剩下的就是聚合,我认为是:
select location, postal, max(spend), max(revenue)
from (select a.*,
(select sum(r.revenue)
from revenue r
where r.user = a.user and
r.dte >= s.dt - interval 10 minute and
r.dte <= s.dte + interval 10 minute
) as revenue
from auction a
where s.event in ('Mid', 'End', 'Show') and
s.cat_id in (3) and
s.cat = 'B'
) a
group by location, postal;
我正在尝试编写一个复杂的 mySQL 查询,其中有 2 个 table 的操作和收入,我需要的是:
- 从拍卖table中取出位置,根据用户的邮政编码,cat_id,猫和花费并加入有收入列的收入table,以便给定cat_id、cat 和 date 我可以计算出每个 'postal' 生成的 returns。
复杂性:
用户在此处是唯一键
在拍卖中 table 有列 'spent' 但只有当 'event' 列有 'show' 但它有 'cat' 条目时才会填充.并且 'cat_id' 在除 show 之外的任何事件中开始填充。因此需要将 cat_id 从 'cat' 映射到事件 'show' 以获得该 cat_id 的花费。
必须设置日期,以便在加入 table 时将时间戳与正负 10 分钟进行比较。现在在我的查询中我有 24 小时的持续时间
按降序聚合邮政给予最高 returns
**Auction Table**
dt user cat_id cat location postal event spent
2020-11-01 22:12:25 1 0 A US X12 Show 2
2020-11-01 22:12:25 1 0 A US X12 Show 2 (duplicate also in table)
2020-11-01 22:12:25 1 6 A US X12 Mid null
2020-11-01 22:13:20 2 0 B UK L23 Show 2
2020-11-01 22:15:24 2 3 B UK L23 End null
**Revenue table**
dt user cat_id revenue
2020-11-01 22:14:45 1 6 null
2020-11-01 22:13:20 2 3 3
想要创建最终 table(通过汇总每个 'postal' 区域的收入):
location postal spend revenue returns
UK X12 2 0 0
US L23 2 3 3/2=1.5
我已经写了一个查询,但无法找出上述 3 个复杂性的解决方案:
Select s.location, s.postal, s.spend, e.revenue
From revenue e JOIN
auction s
on e.user = s.user
where s.event in ('Mid','End','Show') and
TO_DATE(CAST(UNIX_TIMESTAMP(e.dt, 'y-M-d') AS TIMESTAMP)) = TO_DATE(CAST(UNIX_TIMESTAMP(s.dt, 'y-M-d') AS TIMESTAMP)) and
s.cat_id in ('3') and
s.cat = 'B'
任何建议都会有所帮助
这回答了 MySQL 的问题,这是问题的原始标签,也是问题中提到的。
如果我没理解错的话,你的问题是在一个时间范围内“加入”。您可以使用相关子查询做您想做的事。然后剩下的就是聚合,我认为是:
select location, postal, max(spend), max(revenue)
from (select a.*,
(select sum(r.revenue)
from revenue r
where r.user = a.user and
r.dte >= s.dt - interval 10 minute and
r.dte <= s.dte + interval 10 minute
) as revenue
from auction a
where s.event in ('Mid', 'End', 'Show') and
s.cat_id in (3) and
s.cat = 'B'
) a
group by location, postal;