1

我有一个很大的查询,我在下面粘贴了部分内容。我想在我的第二次加入中使用第一次加入的结果。

我想要做的是得到最后session一个,lead_conversion然后我sessions在 then 和当前行之间得到所有

这是我正在努力的部分

left join (
      select ss.id, ss.session_start, ss.lead_id
      from sessions ss
      inner join lead_conversions inner_lc on inner_lc.session_id = ss.id   
    ) prev_lc 
on prev_lc.lead_id = lc.lead_id 
and prev_lc.session_start::TIMESTAMP < s.session_start::TIMESTAMP

left join cte_sessions reset_prev_sess 
on reset_prev_sess.lead_id = lc.lead_id 
and reset_prev_sess.session_start::TIMESTAMP <= s.session_start::TIMESTAMP 
and (
  prev_lc.session_start::TIMESTAMP IS NULL 
  OR 
  reset_prev_sess.session_start::TIMESTAMP > prev_lc.session_start::TIMESTAMP
)

我的问题是我不能只取最后一个prev_lc而且我似乎无法使用max(prev_lc.session_start)

我曾尝试在第一次选择和使用中进行分组,max但这不起作用,因为我相信这是在 on 之前运行的

left join (
      select max(ss.session_start) as session_start, max(ss.lead_id) as lead_id
      from sessions ss
      inner join lead_conversions inner_lc on inner_lc.session_id = ss.id   
      group by inner_lc.id
    ) prev_lc on prev_lc.lead_id = lc.lead_id 

我也尝试过max在第二次加入中使用,但这给出了错误 SQL compilation error: Invalid aggregate function in ON clause [MAX(CAST(PREV_LC.SESSION_START AS TIMESTAMP_NTZ(9)))]

    left join cte_sessions reset_prev_sess 
        on reset_prev_sess.lead_id = lc.lead_id 
        and reset_prev_sess.session_start::TIMESTAMP <= s.session_start::TIMESTAMP 
        and (
          prev_lc.session_start::TIMESTAMP IS NULL 
          OR 
          reset_prev_sess.session_start::TIMESTAMP > max(prev_lc.session_start::TIMESTAMP)
        )

对此的任何帮助将不胜感激

谢谢

4

2 回答 2

0

因此,如果有人遇到此问题,我已经找到了解决方案。我最终只是重新考虑了如何去做。

我最终为每次转换添加了一个行号

with cte_sessions as (
  select
  s.id
  ,s.lead_id
  ,s.session_start::TIMESTAMP as session_start
  ,CASE WHEN MAX(lc.id) IS NOT NULL
             then ROW_NUMBER() over (partition by  s.lead_id,  (CASE WHEN 
             MAX(lc.id) IS NOT NULL then 1 else 0 end)
                order by s.session_start
             )
  END as conversion_row
  from sessions s
  left join lead_conversions lc on lc.session_id = s.id
  group by s.id, s.session_start, s.lead_id, s.project_id, s.crawler_id
  order by s.session_start
)

我刚刚在加入中做了这个

left join cte_sessions prev_lc on prev_lc.lead_id = lc.lead_id and prev_lc.conversion_row =  s.conversion_row - 1
于 2021-08-09T10:53:57.557 回答
0

如果我理解正确你正在寻找加入最后一个会话开始,那么你可以做的是在你的子查询中按 startsession 排序并限制为 1 条记录:

left join (
      select ss.id, ss.session_start, ss.lead_id
      from sessions ss
      inner join lead_conversions inner_lc on inner_lc.session_id = ss.id 
      order by ss.session_start desc
      limit 1  
    ) prev_lc 

其余的查询保持不变。

于 2021-08-05T17:48:16.627 回答