表的自动增量日期字段和版本oracle sql [英] Auto incremental date field and version oracle sql for a table

查看:60
本文介绍了表的自动增量日期字段和版本oracle sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,并且在这个表中我没有正确加载数据完整性问题,因为这是一个维表,我们需要正确地维护有效数据表和示例数据,有效数据表和示例数据

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);

数据完整性检查查询

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)

结果

CITY       POSTAL_CD   COUNT(*)

chicago    N/A         150

N/A        N/A          400

现有数据集

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

预期数据集

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

修复后,运行数据完整性检查查询,我应该得到0行

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

如果只有很少的记录,我应该如何手动调整并更新 EFFECTIVE_DT_FROM EFFECTIVE_DT_TO VERSION ,但是我有很多行,如何解决呢?

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;

我注意到我有一个情况

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

我有31种这样的情况,当我尝试您的合并查询不起作用时,我得到了以下结果集

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

推荐答案

我认为您使用 merge row_number()的逻辑是正确的(尽管,大概不需要 partition by 子句,因为您已经在城市中进行过滤了).我用其他逻辑扩展了日期处理功能:

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:

  • 最初的 effective_dt_from 和最终的 effective_dt_to 应该保持不变

之间,您要从'2017-01-01'开始逐日递增日期.

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

查询:

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

这篇关于表的自动增量日期字段和版本oracle sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆