gpt4 book ai didi

SQL Informix 高级查询

转载 作者:行者123 更新时间:2023-12-04 08:40:32 24 4
gpt4 key购买 nike

让我试着解释我想用我的数据做什么。我的数据结构如下:
我有 3 列:date , id , 和 stage .阶段可以是不同的数字,但我有兴趣显示来自 stage 的某些 id 的特定转换。 -1stage 1 .请看下面的例子。

  • 我有一个 id那是在 stage -1 ,然后过渡到阶段 1 ,回到舞台-1之后。每当从 -1 过渡至 1制作完成,我想要id .
  • 在这里,我从 -1 过渡至 3 ,这不是我感兴趣的。
  • 在这里,进行了过渡,而这个 id是我想要展示的。
  • 在这里,我们从 1 过渡至 -1 ,这不是我想要的。

  • 数据:
        #1   date            id                     stage
    2018-12-31 520000000001354292 -1
    2019-09-30 520000000001354292 1
    2019-12-31 520000000001354292 -1


    #2 2018-12-31 520000000001435675 -1
    2019-03-31 520000000001435675 -1
    2019-06-30 520000000001435675 3


    #3 2018-12-31 520000000003156164 -1
    2019-03-31 520000000003156164 -1
    2018-12-31 520000000003161014 -1
    2019-03-31 520000000003161014 1


    #4 2018-12-31 520500000002472437 1
    2019-03-31 520500000002472437 -1
    2019-06-30 520500000002472437 -1
    2019-09-30 520500000002472437 2
    我想要的输出是:
    520000000001354292    
    520000000003156164
    我希望我已经足够清楚地解释了这一点。
    此外,在此之后,我想显示从 -1 的过渡和 1stage 3 .

    最佳答案

    您可以使用 lag() :

    select distinct id
    from (
    select t.*, lag(stage) over(partition by id order by date) lag_stage
    from mytable t
    ) t
    where lag_stage = -1 and stage = 1
    这带来了所有 id至少有一个来自 -1 的转换的 s至 1 .

    Also, after this, i want to show transition from either -1 and 1 to stage 3.


    您也可以轻松调整查询以适应该用例。只需更改最终 where条款:
    where lag_stage in (-1, 1) and stage = 3

    关于SQL Informix 高级查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64589000/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com