分组连续问题

现在有一个ORACEL 数据表,表中有 id 和 num 两个字段

id是一个自增列

num 存放了 0 和1组成的随机数

id    num 

1      1

2      0

3      0

4      0

5      1

6      1

7      1

8      0

9      1

10     1

11     0

12     1

13     1

14     1

15     1

16     1

17     1

18     0

19     0

现在想要实现一下效果

id    num 

1      1

2      0

3      0

4      0

5      1

6      2

7      3

8      0

9      1

10     2

11     0

12     1

13     2

14     3

15     4

16     5

17     6

18     0

19     0

遇到连续的1时,就依次累加,遇到0,就重新从1开始累加。各位大佬,应该怎么用SQL实现呢

窗口函数实现效果如下,出现1时不能从1重新开始累计

image.png

回复牛~大哥的方法,我的id列是日期字段,这样写出来效果如下:

image.png

SQL 阿洋在燃烧 发布于 2023-9-20 13:35 (编辑于 2023-9-20 15:28)
1min目标场景问卷 立即参与
回答问题
悬赏:10 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共5回答
最佳回答
0
牛~~~Lv6中级互助
发布于2023-9-20 14:42(编辑于 2023-9-20 16:05)

不就是简单的套一层么。

with t0 as(

    select '202201' as id,1 as num

    union all

    select '202202' as id,1 as num

    union all

    select '202203' as id,1 as num

    union all

    select '202204' as id,1 as num

    union all

    select '202205' as id,0 as num

    union all

    select '202206' as id,1 as num

    union all

    select '202207' as id,0 as num

    union all

    select '202208' as id,1 as num

    union all

    select '202209' as id,1 as num

    union all

    select '202301' as id,1 as num

    union all

    select '202302' as id,1 as num

    union all

    select '202303' as id,0 as num

    union all

    select '202304' as id,0 as num

    union all

    select '202305' as id,1 as num

    union all

    select '202306' as id,1 as num

)

,t1 as(

    select

        id,

        row_number() over(order by id) as id2,

        num

    from t0

)

select

    id,

    id2,

    num,

    rn,

    type,

    case 

        when rn is null then 0

        else row_number() OVER (partition by type order by id)

    end AS new_num

from(

    select     

        id,    

        id2,

        num,

        rn,    

        id2 - rn as type

    from(

        SELECT

            x.id,

            x.id2,

            rn,

            num

        FROM

            t1 x

        left join(

            select id2,row_number() over(order by id2) as rn from t1 where num = 1

        ) y

        on x.id2 = y.id2

    ) x

) y

order by id

image.png

  • 用户k6280494 用户k6280494 牛啊,铁子
    2023-09-20 14:45 
  • 阿洋在燃烧 阿洋在燃烧(提问者) 请问id -rn as type,type这个字段是干啥用的呢,倘若我的id字段是个月份日期,那应该怎么办呢
    2023-09-20 15:24 
  • 阿洋在燃烧 阿洋在燃烧(提问者) 我把效果贴在问题中了,您看一下
    2023-09-20 15:29 
  • 牛~~~ 牛~~~ 回复 阿洋在燃烧(提问者) type有两个作用。首先就是将连续的和非连续的区分开;其次就是这个减法是为了将连续的分组。id是月份,那你无非是多了一个 row_number 造个id2 而已,问题不大
    2023-09-20 15:30 
  • 牛~~~ 牛~~~ 回复 阿洋在燃烧(提问者) 理解我后面给你说的多一个rownumberl吗?
    2023-09-20 15:55 
最佳回答
0
用户k6280494Lv6资深互助
发布于2023-9-20 13:47(编辑于 2023-9-20 14:04)

存储过程-游标

SELECT id,  

       SUM(num) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS num  

FROM (  

  SELECT id,  

         CASE   

           WHEN num = 0 THEN 0   

           WHEN num = 1 THEN 1   

         END AS num  

  FROM your_table  

) t

换一个

SELECT id,  

       SUM(CASE WHEN num = 1 THEN 1 ELSE 0 END) OVER (ORDER BY id) AS cumulative_sum  

FROM your_table;

在换一个

SELECT id,  

       CASE   

         WHEN num = 1 THEN SUM(num) OVER (ORDER BY id)  

         ELSE 0  

       END AS num  

FROM your_table;

最佳回答
0
Z4u3z1Lv6专家互助
发布于2023-9-20 13:48

SqlServer?oracle?mysql?

最佳回答
0
梦似幻亦真Lv3见习互助
发布于2023-9-20 18:11(编辑于 2023-9-21 10:11)
最佳回答
0
johnsengLv6见习互助
发布于2023-9-21 13:45

image.png

  • 5关注人数
  • 501浏览人数
  • 最后回答于:2023-9-21 13:45
    请选择关闭问题的原因
    确定 取消
    返回顶部