为来自不同表的两个单独操作运行多个子查询(相关)并加入一个表[关闭]
Posted
技术标签:
【中文标题】为来自不同表的两个单独操作运行多个子查询(相关)并加入一个表[关闭]【英文标题】:Run multiple subqueries(correlated) for the two separate operations from different tables and joining into one table [closed] 【发布时间】:2021-03-31 04:51:56 【问题描述】:我正在尝试使用两个子查询,但找不到正确的方法,我的以下查询不是正确的方法。请帮助我了解如何使用创建列revenue
和spend
的子查询。一个子查询有效,但如何同时包含两者。同样的逻辑也可以通过joins
推导出来,执行时间更短吗?
select country, location, postalcode, MAX(spend), MAX(revenue)
from (select a.*,
(select SUM(r.revenue)
from return r
where r.uid = a.uid and
r.dt >= a.dt - interval 10 minute and
r.dt <= a.dt + interval 10 minute and
r.cat_id in ('1200') and
) as revenue
(select SUM(spend)
from invest a
where r.uid = a.uid and
a.category = '433'
a.cat_id in ('0', '1200') and
a.event in ('begin')
) as spend
from invest a
where a.event in ('show1', 'show2', 'begin') and
a.category = '433' and
) a
group by country, location, postalcode
同样的逻辑也可以通过joins
推导出来,执行时间更短?
**Invest 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
想要创建决赛桌(通过汇总每个“邮政”区域的收入):
location postal spend revenue returns
UK X12 2 0 0
US L23 2 3 3/2=1.5
【问题讨论】:
请提供样本数据、期望的结果以及您想要完成的目标的清晰说明。 见meta.***.com/questions/333952/… 【参考方案1】:可以完全删除其中一个子查询(支出):
select country, location, postalcode,
SUM(case when a.event= 'begin' and a.cat_id in ('0', '1200') then spend end) as spend,
(select SUM(r.revenue)
from return r
where r.uid = a.uid and
r.dt >= a.dt - interval 10 minute and
r.dt <= a.dt + interval 10 minute and
r.pixel_id in ('1200') and -------------why is this and ?
) as revenue
from invest a
where a.event in ('show1', 'show2', 'begin') and
a.category = '433'
group by country, location, postalcode
可以加入第二个子查询:
select country, location, postalcode,
SUM(case when a.event= 'begin' and a.cat_id in ('0', '1200') then spend end) as spend,
b.revenue
from invest a
left join
(select SUM(r.revenue) revenue, dt, uid
from return r
where
r.pixel_id in ('1200')
group by dt
) b ON b.uid = a.uid and
b.dt >= a.dt - interval 10 minute and
b.dt <= a.dt + interval 10 minute
where a.event in ('show1', 'show2', 'begin') and
a.category = '433'
group by country, location, postalcode, b.revenue
【讨论】:
我按照您建议的逻辑进行了尝试,return r
部分似乎存在语法问题。请参阅此You have an error in your SQL syntax; it seems the error is around: 'return r where r.uid = a.uid and r.dt >= a.dt - interval 10 minu' at line 16
。问题似乎在FROM return r
部分
@SachinKumar 当然它在 WHERE 中包含 and
没有条件,就像在您的原始查询中一样。删除它或写一些布尔表达式
@SachinKumar 只有你可以调试它,很遗憾我无法重现,因为没有你的表和数据
谢谢,我已经用表格更新了问题。在 uid 上进行连接,但是当“where”子句的条件类似于where r.uid = a.uid
时,会不断弹出错误cannot resolve '`a.`' given input columns: [r.dt, r.referer, r.revenue, r.stamp]
,这意味着它只查看一个表列,即r
。是不是因为加入操作出错的原因,可以建议吗
@SachinKumar 哦,是的。错误显然在这里:r.dt >= a.dt - 间隔 10 分钟和 r.dt
以上是关于为来自不同表的两个单独操作运行多个子查询(相关)并加入一个表[关闭]的主要内容,如果未能解决你的问题,请参考以下文章