繁体   English   中英

如何在连接的子查询中使用主列值

[英]How to use a main column value on the sub-query of a join

我有一个表“链接”。 链接属于“活动”。 链接有很多“点击”、“社交点击”和“展示”

对于属于活动广告系列的每个活动链接,我想每天计算以下费率:(点击次数 + social_clicks)/展示次数

以下查询正常工作。 但我认为再次查询链接表 3 次并不是最佳选择。 我无法使用主查询中的链接 ID,我尝试用“l.id”和“t1.link_id”替换子查询,但它返回“未知列”

如何在连接的子查询中访问主查询的列,是否有更好的方法来获得我需要的结果?

表格:

links: id int, campaign_id int, status int
campaigns: id int, is_active int
clicks: id int, link_id int, created datetime
social_clicks: id int, link_id int, time datetime
impressions: id int, link_id int, created datetime

询问:

select l.id,
    sum(case when t1.col = 'clicks' then ct else 0 end) as total_clicks,
    sum(case when t1.col = 'impressions' then ct else 0 end) as total_impressions,
    coalesce(sum(case when t1.col = 'clicks' then ct else 0 end) / nullif(sum(case when t1.col = 'impressions' then ct else 0 end),0), 1000) as ctr,
    t1.dt
    from links l
inner join campaigns c
    on c.id = l.campaign_id
    and c.is_active = 1
left join
    (
    select count(1) as ct, link_id, date(created) dt, 'clicks' as col
        from clicks
        where created >= '2020-11-10 00:00:00' 
        and link_id IN (SELECT l2.id from links l2 INNER JOIN campaigns c2 on c2.id = l2.campaign_id AND c2.is_active = 1 WHERE l2.status = 1)
        group by date(created), link_id

    union all

    select count(1) as ct, link_id, date(time) dt, 'clicks' 
        from social_clicks
        where time >= '2020-11-10 00:00:00' 
        and link_id IN (SELECT l2.id from links l2 INNER JOIN campaigns c2 on c2.id = l2.campaign_id AND c2.is_active = 1 WHERE l2.status = 1)
        group by date(time), link_id

    union all

    select count(1) as ct, link_id, date(created) dt, 'impressions' 
        from impressions
        where created > '2020-11-10 00:00:00' 
        and link_id IN (SELECT l2.id from links l2 INNER JOIN campaigns c2 on c2.id = l2.campaign_id AND c2.is_active = 1 WHERE l2.status = 1)
        group by date(created), link_id

    ) t1 on t1.link_id = l.id
where l.status = 1
group by l.id, t1.dt
having total_clicks > 0

我认为您可以将外循环中的内连接和左连接放入union all语句中。 以下更改是否对您的案例有帮助?

select res2.id, res2.dt, sum(res2.clicks_count), sum(res2.impressions_count),
coalesce(sum(res2.clicks_count) / nullif(res2.impressions_count), 1000) as ctr,
from (
select res.id, res.dt, 
case when res.col = 'clicks' then 1 else 0 end clicks_count,
case when res.col = 'impressions' then 1 else 0 end impressions_count
from
(
select li.id, cl.link_id, date(cl.created) dt, 'clicks' as col
    from clicks cl inner join links li on cl.link_id = li.id
    inner join campaigns ca on li.campaign_id = cl.id
    where cl.created >= '2020-11-10 00:00:00' 
    and ca.is_active = 1
    and li.status = 1
union all
select li.id,  cl.link_id, date(cl.created) dt, 'clicks' as col
    from social_clicks cl inner join links li on cl.link_id = li.id
    inner join campaigns ca on li.campaign_id = cl.id
    where cl.time >= '2020-11-10 00:00:00' 
    and ca.is_active = 1
    and li.status = 1
union all
select li.id, im.link_id, date(im.created) dt, 'impressions' as col
    from impressions im inner join links li on im.link_id = li.id
    inner join campaigns ca on li.campaign_id = im.id
    where im.created >= '2020-11-10 00:00:00' 
    and ca.is_active = 1
    and li.status = 1
) res) res2
group by res2.id, res2.dt
having sum(res2.clicks_count) > 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM