sql - 人口稀少的表上的自联接
问题描述
我在 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
. 我试过做一个自我加入来获得所有的TransactionId
s:
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 仍未填充,而且看起来不完整。我觉得我在这里遗漏了一个基本问题;也许我想要的不是一个连接,而是一个......分叉?
解决方案
我正在尝试获取失败的交易,
如果要返回失败类型列之一不是的会话的所有事务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;
推荐阅读
- django - 使用 Django Manager 删除所有 MpttModel 条目的正确方法
- reactjs - 使用 Passport Google OAuth2 验证私有 React 路由
- angular - Angular 组件视图在刷新 (F5) 之前不会列出完整的 NGRX 状态负载
- python - PATCH (partial=true) 在 Django 中不起作用
- reactjs - 错误导致 webpack 无法构建
- javascript - 消息在它需要所有数据之前被发送,这意味着它在所有消息中发送了几条具有相同数据的消息
- amazon-web-services - 如何将大型黑名单与 AWS WAF 集成
- reactjs - axios 和 fetch 在 React 中返回空响应数据
- c++ - boost::interprocess::interprocess_condition::timed_wait 永远等待
- android - 如何使用 recylerview 和 Fragments 和 viewmodel 显示 cardview 项目