如何为重复添加值?

xmjla07d  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(359)

如果标题不合适,请原谅
我有一张table如下

Dev     FCNo    DateTime
dev1    1   12-06-2020
dev1    2   12-06-2020
dev1    3   13-06-2020
dev1    4   14-06-2020
dev1    5   15-06-2020
dev1    6   15-06-2020
dev1    7   16-06-2020
dev1    1   16-06-2020
dev1    2   16-06-2020
dev1    3   17-06-2020
dev1    4   17-06-2020
dev2    1   12-06-2020
dev2    2   12-06-2020
dev2    3   13-06-2020
dev2    1   13-06-2020
dev2    2   13-06-2020
dev2    3   14-06-2020

我想要一个专栏

Dev     FCNo    DateTime    Occurance
dev1    1   12-06-2020      1
dev1    2   12-06-2020      1
dev1    3   13-06-2020      1
dev1    4   14-06-2020      1
dev1    5   15-06-2020      1
dev1    6   15-06-2020      1
dev1    7   16-06-2020      1
dev1    1   16-06-2020      2
dev1    2   16-06-2020      2
dev1    3   17-06-2020      2
dev1    4   17-06-2020      2
dev2    1   12-06-2020      1
dev2    2   12-06-2020      1
dev2    3   13-06-2020      1
dev2    1   13-06-2020      2
dev2    2   13-06-2020      2
dev2    3   14-06-2020      2

对于特定设备,每当fcno重复为1时,发生率应增加。如何在sql中实现这一点
谢谢

tvmytwxo

tvmytwxo1#

请使用下面的查询,

select dev, fcno, datetime, rnk
from
(select dev, fcno, datetime, row_number() over(partition by dev, fcno) as rnk
from table_name) qry order by dev, fcno;
pbossiut

pbossiut2#

我想你想要 row_number() :

select t.*,
       row_number() over (partition by fcno, dev order by datetime) as occurrence
from t;

相关问题