首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用自定义类型进行按id选择和下一行选择?

如何使用自定义类型进行按id选择和下一行选择?
EN

Stack Overflow用户
提问于 2019-03-18 21:45:18
回答 1查看 28关注 0票数 0

我有一个这样的表:https://i.stack.imgur.com/qyyKD.png

代码语言:javascript
复制
id, external_id, type, timestamp
694534804,685112085,FASTLY,2019-03-13 15:51:51.272000
694534790,685112085,FASTLY,2019-03-13 14:14:43.814000
694534789,685112085,FASTLY,2019-03-13 14:11:32.138000
694534788,685112085,FASTLY,2019-03-13 14:10:54.681000
694534787,685112085,FASTLY,2019-03-13 14:10:39.444000
694534786,685112085,FASTLY,2019-03-13 14:10:21.359000
694534785,685112085,FASTLY,2019-03-13 14:10:20.849000
694527409,685112085,FASTLY,2019-03-13 13:22:05.733000
694527408,685112085,FASTLY,2019-03-13 13:20:29.546000
694527407,685112085,FASTLY,2019-03-13 13:20:13.528000
694527406,685112085,SLOWLY,2019-03-13 13:19:44.476000
694515187,685112085,FASTLY,2019-03-11 14:32:04.805000
694515186,685112085,FASTLY,2019-03-11 14:31:41.592000
694303908,685112085,FASTLY,2019-03-01 16:03:19.720000
694303907,685112085,FASTLY,2019-03-01 16:03:19.560000
694295217,685112085,FASTLY,2019-02-28 23:01:29.801000
694295216,685112085,FASTLY,2019-02-28 22:55:06.978000
686123194,685112085,FASTLY,2019-01-21 15:21:08.029000
685653586,685112085,FASTLY,2019-01-18 15:04:54.975000
685653585,685112085,SLOWLY,2019-01-18 15:04:46.627000
685108390,685112085,FASTLY,2019-01-16 00:08:46.439000
685108387,685112085,SLOWLY,2019-01-15 22:11:14.562000
694527391,1846930,FASTLY,2019-03-12 16:37:21.586000
694527390,1846930,FASTLY,2019-03-12 16:36:43.122000
694527389,1846930,FASTLY,2019-03-12 16:36:21.071000
693947087,1846930,SLOWLY,2019-02-05 10:22:21.698000
1846934,1846930,FASTLY,2018-12-05 20:48:29.100000
1846931,1846930,SLOWLY,2018-12-05 20:48:28.961000

我想要获取一对值:在前面输入ID和ID,或者对于相同的external_id,用type = SLOWLY等于他。像这样的Smth:

代码语言:javascript
复制
select * from table where id in (694534804, 694527408, 694527406, 694515187, 685653585, 685108390, 685108387, 694527390, 693947087, 1846934, 1846931)  

id        | prev_or_eq_slowly_id
694534804 | 694527406
694527408 | 694527406
694527406 | 694527406
694515187 | 685653585
685653585 | 685653585
694527390 | 693947087 
693947087 | 693947087
1846934   | 1846931
1846931   | 1846931
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-18 21:47:12

如果你能在lag()中使用filter就好了

代码语言:javascript
复制
select id,
       (case when type = 'SLOWLY' then id
             else lag(id) filter (where type = 'SLOWLY') over (partition by external_id order by timestamp)
        end) as prev_slowly_id
from t;

相反,使用横向联接可能会更容易:

代码语言:javascript
复制
select t.id, t2.prev_slowly_id as 
from t left join lateral
     (select t2.id
      from t t2
      where t2.external_id = t.external_id and
            t2.timestamp <= t.timestamp and
            t2.type = 'SLOWLY'
      order by t2.timestmp desc
     ) t2;

但最好的方法是累积最大值。。。假设ids随着时间戳的增加而增加:

代码语言:javascript
复制
select t.*,
       max(id) filter (where type = 'SLOWLY') over (partition by external_id order by id) as prev_slowly_id
from t;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55222824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档