Sql Postgres-检索内部连接最早的记录
因此,在我的工作中,管理层希望评估我们需要多长时间才能联系到潜在客户。在1小时之前、1到3小时之间联系了多少潜在客户,依此类推 通过此查询,我可以接近结果,但如果所述潜在客户联系了多次(这是最常见的情况),则结果会出现偏差,例如: -Lead进入ad 9:00,在9:30获得联系,在1小时前为列计算一次,然后随后获得N次联系,因此事件将在所有不同的列中计算。 我想要的只是第一次接触 有没有办法让内部连接只解释最早的第一次接触,以及与我们相关的信息 ws_ts是lead时间戳,call_start是不言自明的。 leads表和call_日志根据contact_id连接 提前谢谢。我的选择:Sql Postgres-检索内部连接最早的记录,sql,postgresql,Sql,Postgresql,因此,在我的工作中,管理层希望评估我们需要多长时间才能联系到潜在客户。在1小时之前、1到3小时之间联系了多少潜在客户,依此类推 通过此查询,我可以接近结果,但如果所述潜在客户联系了多次(这是最常见的情况),则结果会出现偏差,例如: -Lead进入ad 9:00,在9:30获得联系,在1小时前为列计算一次,然后随后获得N次联系,因此事件将在所有不同的列中计算。 我想要的只是第一次接触 有没有办法让内部连接只解释最早的第一次接触,以及与我们相关的信息 ws_ts是lead时间戳,call_start
select
min(dcl.call_start::date) as Date,
sum(
case
when (((dcl.call_start) - (lsl.ws_ts))::interval) < '01:00:00'
then 1
else 0
end
)as "Lead called before 1 hour",
sum(
case
when (( ((dcl.call_start) - (lsl.ws_ts)))::interval) > '01:00:00' and (( ((dcl.call_start) - (lsl.ws_ts)))::interval) < '03:00:00'
then 1
else 0
end
)as "Lead called between 1 and 3 hours",
sum(
case
when (( ((dcl.call_start) - (lsl.ws_ts)))::interval) > '03:00:00' and (( ((dcl.call_start) - (lsl.ws_ts)))::interval) < '05:00:00'
then 1
else 0
end
)as "Lead called between 3 and 5 hours",
sum(
case
when (( ((dcl.call_start) - (lsl.ws_ts)))::interval) > '05:00:00'
then 1
else 0
end
)as "Lead called after 5 hours"
from public.leads lsl
inner join dialer.dialer_call_logs dcl on (lsl.ws_contact_id::int = dcl.contact_id )
where lsl.ws_source = 'CAMPAIGN' and lsl.ws_ts::date between '2020-03-09' and '2020-03-13' and lsl.ws_type <> 'call' and dcl."source" = 'CAMPAIGN'
group by lsl.ws_creation
一种方法是横向连接:
from public.leads lsl cross join lateral
(select dcl.*
from dialer.dialer_call_logs dcl
where ws_contact_id::int = contact_id
order by dcl.call_start asc
limit 1
) dcl
where ws_source = 'CAMPAIGN' and
ws_ts::date between '2020-03-09' and '2020-03-13' and
ws_type <> 'call' and dcl."source" = 'CAMPAIGN'
您尚未指定列的来源。有些where条件可能需要在子查询中。没错,已经指定了列的原点。我们将尝试实施您的解决方案,并回复您。
from public.leads lsl cross join lateral
(select dcl.*
from dialer.dialer_call_logs dcl
where ws_contact_id::int = contact_id
order by dcl.call_start asc
limit 1
) dcl
where ws_source = 'CAMPAIGN' and
ws_ts::date between '2020-03-09' and '2020-03-13' and
ws_type <> 'call' and dcl."source" = 'CAMPAIGN'