Auto incremental date field and version oracle sql for a table

扶醉桌前 提交于 2021-02-11 07:08:19

问题


I have a table and in this table i have data is not properly loaded data integrity issue ,since this is a dimension table we need to maintain the effective_dt_from and effective_dt_to and version correctly ,below is the table and sample data

create table TEST
(
LOC_SID NUMBER(38,0),
CITY VARCHAR2(180 BYTE),
POSTAL_CD VARCHAR2(15 BYTE),
EFFECTIVE_DT_FROM DATE,
EFFECTIVE_DT_TO DATE,
VERSION NUMBER(38,0)
);


Insert into TEST values (81910,'chicago',null,to_date('01.01.00 00:00:00','DD.MM.YY HH24:MI:SS'),to_date('31.12.99 23:59:59','DD.MM.YY HH24:MI:SS'),1);
Insert into TEST values (81911,'chicago',null,to_date('01.01.00 00:00:00','DD.MM.YY HH24:MI:SS'),to_date('31.12.99 23:59:59','DD.MM.YY HH24:MI:SS'),1);
Insert into TEST values (81912,'chicago',null,to_date('01.01.00 00:00:00','DD.MM.YY HH24:MI:SS'),to_date('31.12.99 23:59:59','DD.MM.YY HH24:MI:SS'),1);
Insert into TEST values (81913,'chicago',null,to_date('01.01.00 00:00:00','DD.MM.YY HH24:MI:SS'),to_date('31.12.99 23:59:59','DD.MM.YY HH24:MI:SS'),1);
Insert into TEST values (81914,'chicago',null,to_date('01.01.00 00:00:00','DD.MM.YY HH24:MI:SS'),to_date('31.12.99 23:59:59','DD.MM.YY HH24:MI:SS'),1);

Data integrity check query

SELECT count(*) AS RowAffected
FROM
  (SELECT LOC_SID,
          VERSION,
          EFFECTIVE_DT_FROM,
          EFFECTIVE_DT_TO,
          CITY,
          POSTAL_CD
   FROM
     (SELECT t.*,
             LEAD(EFFECTIVE_DT_FROM, 1) OVER(PARTITION BY CITY, POSTAL_CD
                                             ORDER BY EFFECTIVE_DT_FROM) AS next_date,
             LEAD(VERSION, 1) OVER(PARTITION BY CTY, POSTAL_CD
                                   ORDER BY EFFECTIVE_DT_FROM) AS next_version
      FROM TEST t)
   WHERE valid_to != next_date
     OR VERSION = next_version)

Results

CITY       POSTAL_CD   COUNT(*)

chicago    N/A         150

N/A        N/A          400

existing data set

LOC_SID CITY    POSTAL_CD   EFFECTIVE_DT_FROM   EFFECTIVE_DT_TO   VERSION
81910   CHICAGO    N/A        01.01.1900          31.12.2199    1
81911   CHICAGO    N/A        01.01.1900          31.12.2199    1
81912   CHICAGO    N/A        01.01.1900          31.12.2199    1
81913   CHICAGO    N/A        01.01.1900          31.12.2199    1
81914   CHICAGO    N/A        01.01.1900          31.12.2199    1

expected data set

LOC_SID CITY    POSTAL_CD   EFFECTIVE_DT_FROM   EFFECTIVE_DT_TO   VERSION
81910   CHICAGO    N/A        01.01.1900          01.01.2017       1
81911   CHICAGO    N/A        01.01.2017          02.01.2017       2
81912   CHICAGO    N/A        02.01.2017          03.01.2017       3
81913   CHICAGO    N/A        03.01.1900          04.01.2017       4
81914   CHICAGO    N/A        04.01.2017          31.12.2199       5

after the fix and run the Data integrity check query i should get 0 rows

how do i make the EFFECTIVE_DT_FROM, EFFECTIVE_DT_TO and VERSION incremental if it would have been few records i would have manually adjusted and update it ,but i have many rows , how to fix this ?

i tried the below query ,but this will only update the only version ,but doesn't fix the issue

merge into TEST a
using
(
select LOC_SID,CITY,POSTAL_CD,EFFECTIVE_DT_FROM,EFFECTIVE_DT_TO,VERSION
,rank() over(partition by CITY,POSTAL_CD order by LOC_SID ) rnk
from TEST
where CITY='CHICAGO'
)b
on (a.LOC_SID = b.LOC_SID)
when matched then update set 
a.VERSION=b.rnk;

I have noticed i have a scenario

LOC_SID CITY    POSTAL_CD   EFFECTIVE_DT_FROM   EFFECTIVE_DT_TO  VERSION
25101   ASSAM   1153            01.01.1900          31.12.2199     1
25102   ASSAM   1153            01.01.1900          31.12.2199     1
25103   ASSAM   1290            01.01.1900          31.12.2199     1
25104   ASSAM   1290            01.01.1900          31.12.2199     1
25105   ASSAM   1310            01.01.1900          31.12.2199     1
25106   ASSAM   1310            01.01.1900          31.12.2199     1
25107   ASSAM   1781            01.01.1900          31.12.2199     1
25108   ASSAM   1781            01.01.1900          31.12.2199     1
25109   ASSAM   1982            01.01.1900          31.12.2199     1
25110   ASSAM   1982            01.01.1900          31.12.2199     1

I have 31 cases like this ,when i tried your merge query it is not working , i have the below result set

LOC_SID  CITY    POSTAL_CD       EFFECTIVE_DT_FROM   EFFECTIVE_DT_TO  VERSION
    25101   ASSAM   1153            01.01.1900          01.01.2017          1
    25102   ASSAM   1153            01.01.1900          31.12.2199          1
    25103   ASSAM   1290            01.01.1900          01.01.2017          1
    25104   ASSAM   1290            01.01.1900          31.12.2199          1
    25105   ASSAM   1310            01.01.1900          01.01.2017          1
    25106   ASSAM   1310            01.01.1900          31.12.2199          1
    25107   ASSAM   1781            01.01.1900          01.01.2017          1
    25108   ASSAM   1781            01.01.1900          31.12.2199          1
    25109   ASSAM   1982            01.01.1900          01.01.2017          1
    25110   ASSAM   1982            01.01.1900          31.12.2199          1

回答1:


I think that your logic using merge and row_number() is on the right track (although, presumably, the partition by clause is not needed, since you are filtering on the city` already). I extended it with additional logic to handle the dates:

  • the initial effective_dt_from and final effective_dt_to should be left untouched

  • in between, you want to increment dates day by day starting on '2017-01-01'.

Query:

merge into test t
using (
    select
        t.*,
        row_number() over(order by loc_sid asc) rn,
        count(*) over() cnt
    from test t
    where city = 'Chicago'
) t1
on (t1.loc_sid = t.loc_id)
when matched the update set
    t.version = t1.rn,
    t.effective_dt_from = 
        case 
            when rn = 1 then t.effective_dt_from
            else date '2017-01-01' + rn - 2
        end,
    t.effective_dt_to = 
        case 
            when rn = cnt then t.effective_dt_to
            else date '2017-01-01' + rn - 1
        end


来源:https://stackoverflow.com/questions/61578278/auto-incremental-date-field-and-version-oracle-sql-for-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!