自己加入table

Self join a table

我正在尝试将 table 连接到自身,以从在某个(稍后)点具有 goal_completion 的 clientids 获取所有 session_ids。

Table:

<table class="tableizer-table">
<thead><tr class="tableizer-firstrow"><th>clientid</th><th>sessionid</th><th>goalcompletion</th></tr></thead><tbody>
 <tr><td>1</td><td>a</td><td>0</td></tr>
 <tr><td>1</td><td>b</td><td>0</td></tr>
 <tr><td>1</td><td>c</td><td>1</td></tr>
 <tr><td>2</td><td>x</td><td>0</td></tr>
 <tr><td>2</td><td>y</td><td>0</td></tr>
 <tr><td>2</td><td>z</td><td>0</td></tr>
</tbody></table>

预期输出:

<table class="tableizer-table">
<thead><tr class="tableizer-firstrow"><th>clientid</th><th>sessionid</th></tr></thead><tbody>
 <tr><td>1</td><td>a</td></tr>
 <tr><td>1</td><td>b</td></tr>
 <tr><td>1</td><td>c</td></tr>
</tbody></table>

我尝试了几个版本,但似乎无法弄清楚它是如何工作的。这是我的最新版本:

SELECT a.clientid,
       a.session_id,
       a.goal1completions_funnel,
       a.goal2completions_funnel,
       a.goal3completions_funnel
FROM _demo.ga_conversions_test a
left JOIN _demo.ga_conversions_test b
  ON a.session_id = b.session_id
  AND (b.goal3completions_funnel = 1
     OR b.goal1completions_funnel = 1
     OR b.goal2completions_funnel = 1)

你能带我走正确的路吗?

我认为您不需要为此任务使用联接。请尝试:

select session_id
from _demo.ga_conversions_test 
where goal1completions_funnel=1 OR goal2completions_funnel=1 OR goal3completions_funnel=1
group by session_id

编辑:根据提供的数据,如果我是你,我会发现 clientids 有两种以上的目标完成数,并使用如下的内部连接:

with cte as (
   select clientid, count(distinct goalcompletion) as CountDifferentGoalCompletion
   from _demo.ga_conversions_test
   group by clientid
   having count(distinct goalcompletion) > 1
)
select a.clientid, a.sessionid
from _demo.ga_conversions_test a
inner join cte on cte.clientid = _demo.ga_conversions_test.clientid

EDIT2:如果 cte 结构不起作用(如果这不在 SQL 服务器中则不起作用),则:

select a.clientid, a.sessionid
from _demo.ga_conversions_test a
inner join (select clientid, count(distinct goalcompletion) as CountDifferentGoalCompletion
            from _demo.ga_conversions_test
            group by clientid
            having count(distinct goalcompletion) > 1) x 
on x.clientid = a.clientid

您无法获得所需结果的原因似乎是您正在加入具有相同 session_id 的表。试试这个:

Select ....
From _demo.ga_conversions_test a
left join _demo.ga_conversions_test a on a.session_id < b.session_id (and other criteria)

看看是否有效。如果没有,张贴您的数据结构示例将有助于我们更好地理解。如果给了你一个解决方案,请将我的答案标记为已接受的解决方案。

这是你想要的吗?

select ct.*
from _demo.ga_conversions_test ct
where exists (select 1
              from _demo.ga_conversions_test ct2
              where ct2.session_id = ct.session_id and
                    1 in (goal1completions_funnel, goal2completions_funnel, goal3completions_funnel) and
                    ct2.<timecol> > ct.<ctimecol>
             );