首页 > 解决方案 > 人口稀少的表上的自联接

问题描述

我在 S3 中有一张桌子(好吧,如果它改变的话,它是镶木地板文件)供 Presto/AWS Athena 查询。

里面有很多废话,但基本上结构是:

SessionId, TransactionId, SessionFailureType1, SessionFailureType2
123456   , 123-c-456    ,                     , 
999999   ,              , 1                   ,
123456   , 090-2-999    ,                     , 
999999   , 111-1-111    ,                     , 
123456   ,              ,                     , 1

一些会话没有事务,许多会话没有失败。每个事务和会话开始获取一行。

这种情况很像两张表合而为一,外键为 SessionId。

我正在尝试获取失败的事务,我认为这意味着我需要自行加入表,然后按SessionId. 我试过做一个自我加入来获得所有的TransactionIds:

SELECT TransactionId as trid, SessionId as ssid, SessionFailureType1 as f1, SessionFailureType2 as f2 
FROM MyTable a join MyTable b on a.SessionId = b.SessionId
where b.SessionFailureType1 = 1 or b.SessionFailureType2 = 1
and a.TransactionId <> '';

但这给了我一堆结果,其中交易 ID 仍未填充,而且看起来不完整。我觉得我在这里遗漏了一个基本问题;也许我想要的不是一个连接,而是一个......分叉?

标签: sqlamazon-web-servicesparquetprestoamazon-athena

解决方案


我正在尝试获取失败的交易,

如果要返回失败类型列之一不是的会话的所有事务null,则使用窗口函数:

select t.*
from (select t.*,
             count(SessionFailureType1) over (partition by sessionid) as cnt1,
             count(SessionFailureType2) over (partition by sessionid) as cnt2
      from t
     ) t
where cnt1 > 0 or cnt2 > 0;

推荐阅读