如何为表中的匹配值分配唯一值?

发布于 2024-11-16 21:17:00 字数 721 浏览 2 评论 0原文

我的问题描述: 如何为表中的匹配值分配唯一值?

我对表中具有相同值的列进行了排序,例如

AMOUNT REF1 REF2 REF3 VALUE_DATE ADDRESS

2000 asdfg hjklq werty 13-JAN-11 A

2500 asdfg hjklq werty 14-JAN-11 B

2500 asdfg hjklq werty 15-JAN-11 C

2500 asdfg hjklq werty 16-JAN-11 D

2000 asdfg hjklq werty 17-JAN-11 E

期望输出:我需要更新并分配匹配值的唯一值,例如

AMOUNT REF1 REF2 REF3 VALUE_DATE ADDRESS match_no

2000 asdfg hjklq werty 2011 年 1 月 13 日 A 1

2500 asdfg hjklq werty 2011 年 1 月 14 日 B 2

2500 asdfg hjklq werty 2011 年 1 月 15 日 C 2

2500 asdfg hjklq werty 2011 年 1 月 16 日 D 2

2000 asdfg hjklq werty 2011 年 1 月 17 日 E 1

应为匹配值分配相同的唯一编号并更新 match_no 行,如上所示。

Description of my Question:
how to assign an unique value for the matching values in a table?

I have sorted the columns which has same values in a table,like

AMOUNT REF1 REF2 REF3 VALUE_DATE ADDRESS

2000 asdfg hjklq werty 13-JAN-11 A

2500 asdfg hjklq werty 14-JAN-11 B

2500 asdfg hjklq werty 15-JAN-11 C

2500 asdfg hjklq werty 16-JAN-11 D

2000 asdfg hjklq werty 17-JAN-11 E

desired out: I need to update and assign an unique valufor matching values, like

AMOUNT REF1 REF2 REF3 VALUE_DATE ADDRESS match_no

2000 asdfg hjklq werty 13-JAN-11 A 1

2500 asdfg hjklq werty 14-JAN-11 B 2

2500 asdfg hjklq werty 15-JAN-11 C 2

2500 asdfg hjklq werty 16-JAN-11 D 2

2000 asdfg hjklq werty 17-JAN-11 E 1

Same unique number should assign for matched value and update the match_no row as shown above..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2024-11-23 21:17:00

您可以使用分析函数对要查找唯一值的字段进行排名,然后使用这些值更新表。对于这种情况,您希望根据匹配的查询设置行,merge 通常比 update 更有用。

MERGE INTO   your_table
     USING   (SELECT   amount,
                       ref1,
                       ref2,
                       ref3,
                       DENSE_RANK()
                          OVER (ORDER BY amount,
                                         ref1,
                                         ref2,
                                         ref3)
                          AS rnk
                FROM   your_table) yt
        ON   (    your_table.amount = yt.amount
              AND your_table.ref1 = yt.ref1
              AND your_table.ref2 = yt.ref2
              AND your_table.ref3 = yt.ref3)
WHEN MATCHED THEN
   UPDATE SET match_no = rnk;

You can use analytic functions to rank the fields you want to find unique values across, then update the table with those values. For this kind of scenario, where you're looking to set rows based on a matching query, merge is often more useful than update.

MERGE INTO   your_table
     USING   (SELECT   amount,
                       ref1,
                       ref2,
                       ref3,
                       DENSE_RANK()
                          OVER (ORDER BY amount,
                                         ref1,
                                         ref2,
                                         ref3)
                          AS rnk
                FROM   your_table) yt
        ON   (    your_table.amount = yt.amount
              AND your_table.ref1 = yt.ref1
              AND your_table.ref2 = yt.ref2
              AND your_table.ref3 = yt.ref3)
WHEN MATCHED THEN
   UPDATE SET match_no = rnk;
凶凌 2024-11-23 21:17:00
create table temp_lookup as select x.*, rownum as match_no
  from (select amount, ref1, ref2, ref3 from yourtable
          group by amount, ref1, ref2, ref3) x;

update yourtable a set match_no = 
 (select match_no from temp_lookup b
    where a.amount = b.amount
      and a.ref1 = b.ref1
      and a.ref2 = b.ref2
      and a.ref3 = b.ref3)

drop table temp_lookup;
create table temp_lookup as select x.*, rownum as match_no
  from (select amount, ref1, ref2, ref3 from yourtable
          group by amount, ref1, ref2, ref3) x;

update yourtable a set match_no = 
 (select match_no from temp_lookup b
    where a.amount = b.amount
      and a.ref1 = b.ref1
      and a.ref2 = b.ref2
      and a.ref3 = b.ref3)

drop table temp_lookup;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文