我需要添加一列,为每条记录分配行号

I need to add a column that assigns row numbers to each record

我正在尝试添加一个为每条记录分配行号的列[​​=14=]

我更改了代码以添加所需的列

但是我得到了重复的行号。如何获得唯一的行号? .

**Note: This code can be executed in the SQL editor like that. It needs no sample data**

select distinct trunc(GenerateTimeBy1Day,'day') as claim_eff_date, trunc(GenerateTimeBy1DayPlus20,'day') as bwe_to_completeby from
(
    select from_dt + (level - 1)/1 as GenerateTimeBy1Day, (from_dt + (level - 1)/1) + 20 as GenerateTimeBy1DayPlus20 
    from (select from_dt
        ,to_dt
        ,to_dt - from_dt + 1 as days_between    
    from (select to_date('03-Jan-2021') as from_dt
               , to_date('30-Jan-2021') as to_dt 
          from dual))
    connect by (level - 1) <= days_between  
)
order by claim_eff_date

我对代码进行了这些更改以添加所需的列

**Note: This code can be executed in the SQL editor like that. It needs no sample data**

select distinct trunc(GenerateTimeBy1Day,'day') as claim_eff_date, trunc(GenerateTimeBy1DayPlus20,'day') as bwe_to_completeby, row_number() 
over (PARTITION BY trunc(GenerateTimeBy1Day,'day'), trunc(GenerateTimeBy1DayPlus20,'day') ORDER BY trunc(GenerateTimeBy1Day,'day')) as row_number from
(
    select from_dt + (level - 1)/1 as GenerateTimeBy1Day, (from_dt + (level - 1)/1) + 20 as GenerateTimeBy1DayPlus20 
    from (select from_dt
        ,to_dt
        ,to_dt - from_dt + 1 as days_between    
    from (select to_date('03-Jan-2021') as from_dt
               , to_date('30-Jan-2021') as to_dt 
          from dual))
    connect by (level - 1) <= days_between  
)
order by claim_eff_date

但我得到了

Row_Number
----------
1
1
2
3
4
5
6
1
1
2
3

如何获得唯一的行号?

Row_Number
----------
1
2
3
4
5
6
7
8
9
10

我不确定这些日期代表什么,但是 - 如果您最初编写的查询完成了这项工作,那么将它用作计算行号的最终查询的 source

SQL> alter session set nls_date_format = 'dd.mm.yyyy';

Session altered.

SQL> with your_query as (
  2  select distinct trunc(GenerateTimeBy1Day,'day') as claim_eff_date,
  3                  trunc(GenerateTimeBy1DayPlus20,'day') as bwe_to_completeby
  4  from
  5  (
  6      select from_dt + (level - 1)/1 as GenerateTimeBy1Day, (from_dt + (level - 1)/1) + 20 as GenerateTimeBy1DayPlus20
  7      from (select from_dt
  8          ,to_dt
  9          ,to_dt - from_dt + 1 as days_between
 10      from (select date '2021-01-03' as from_dt
 11                 , date '2021-01-30' as to_dt
 12            from dual))
 13      connect by (level - 1) <= days_between
 14  ))
 15  select claim_eff_date,
 16         bwe_to_completeby,
 17         row_number() over (order by claim_eff_date) rn
 18  from your_query
 19  order by claim_eff_date;

CLAIM_EFF_ BWE_TO_COM         RN
---------- ---------- ----------
28.12.2020 18.01.2021          1
04.01.2021 25.01.2021          2
04.01.2021 18.01.2021          3
11.01.2021 01.02.2021          4
11.01.2021 25.01.2021          5
18.01.2021 08.02.2021          6
18.01.2021 01.02.2021          7
25.01.2021 08.02.2021          8
25.01.2021 15.02.2021          9

9 rows selected.

SQL>

顺便说一句,这是你写的:to_date('03-Jan-2021')。那是错误的。 TO_DATE 应用于不带日期格式掩码的字符串依赖于 Oracle 的功能猜测 您的意思。此外,这甚至在我的数据库中都不起作用,尽管你说

This code can be executed in the SQL editor

我的数据库使用 克罗地亚语,没有月份像 Jan

更安全的选择是

  • 应用格式掩码:to_date('03-Jan-2021', 'dd-mon-yyyy', 'nls_date_language = english')
  • 或者,使用日期字面量(就像我所做的那样):date '2021-01-03' 始终具有 date 关键字和 yyyy-mm-dd 格式的日期,因此没有歧义