简体   繁体   中英

Update table procedure through pl sql using double loop

Assuming that I have only one table : members_tbl which columns are :SN, FN, DB, IDDBL,FLAG, DBLCRIT I need to update the table if some conditions are OK with this algorithm :

   id_dup := 1;
FOR (i in 1 to Nrow(members_tbl)) THEN
{
FOR (j in (i+1) to Nrow(members_tbl)) THEN
{
IF(members_tbl(i).iddbl IS NULL) THEN
members_tbl(i).iddbl := id_dup
IF (((members_tbl(i).DBIRTH ==members_tbl(j).DBIRTH) AND
    (UTL_MATCH.jaro_winkler_similarity(members_tbl(i).SNAME,members_tbl(j).SNAME) > 80) AND
  (UTL_MATCH.jaro_winkler_similarity(members_tbl(i).FNAME,members_tbl(j).FNAME) > 80)) 
AND (members_tbl(j).iddbl IS NULL)) THEN
{
   members_tbl(j).iddbl := id_dup;
   members_tbl(i).flag := 1;
   members_tbl(j).flag := 1;
   members_tbl(i).dblcrit:= 1;
   members_tbl(j).dblcrit:= 1;
}

}
   id_dup := id_dup + 1;
}

Here is a test case :

CREATE TABLE MEMBERS_TBL
  (
    IDM     NUMBER(9) NOT NULL ,
    SNAME   VARCHAR2(20) ,
    FNAME   VARCHAR2(20) ,
    DBIRTH  VARCHAR2(15) ,
    IDDBL   NUMBER(10) ,
    FLAG    SMALLINT ,
    DBLCRIT SMALLINT
  );
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (125,'BOLOREY','JEANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (126,'BOLLOREY','JEANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (153,'BALORE','GIANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (223,'ABOLLOREYY','JEANNE','11/05/1955',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (225,'FIABLE','MARINE','25/04/1963',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (228,'FAIBLE','MARYANE','25/04/1963',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (230,'FAIBLE','MARINE','25/04/1963',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (235,'LIPARK','JACQLINE','20/12/1939',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (236,'ILIPARC','JACQUELYNE','20/12/1939',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (237,'LIPARC','JACQUELHINE','20/12/1939',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (240,'RINTET','MALIKA','07/08/1954',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (241,'GRISION','RAUGER','26/10/1931',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (242,'GRISION','ROGER','26/10/1931',NULL,NULL,NULL);
Insert into members_tbl (IDM,SNAME,FNAME,DBIRTH,IDDBL,FLAG,DBLCRIT) values (245,'GRESION','RAUJER','26/10/1931',NULL,NULL,NULL);

to be more comprensive, i do change columns SN in SNAME, FN in FNAME and DB in DBIRTH

Is there a way to write this algorithm in a pl/sql procedure ? I'm relatively new to PL/SQL and i've never written a stored procedure.

I think this would work. You'll need to figure out how it determines which row is "higher" and thus the duplicate.

update members_tbl
set iddbl = 1
where iddbl is null and exists (
    select 1
    from members_tbl m2
    where
            m2.DB = members_tbl.DB
        and UTL_MATCH.jaro_winkler_similarity(members_tbl.FN, m2.FN) > 80
        and UTL_MATCH.jaro_winkler_similarity(members_tbl.SN, m2.SN) > 80
        and NumberOfRow(m2) < NumberOfRow(members_tbl) /* ??? */

);

update members_tbl
set flag = 1, dblcrit = 1;

Effectively, as suggested by @shawnt00, it's a duplicate detection based on the approximation of character strings through Jaro Winkler's functio; Consequently, there is no need of count (no need of flag, dblcrit, iddbl), but simply a left outer join :

SELECT * FROM (
SELECT m.IDM,
       m2.IDM AS dups_key
FROM members_tbl m
LEFT OUTER JOIN members_tbl m2
ON ( m.IDM != m2.IDM
      AND m.DBIRTH = m2.DBIRTH
      AND utl_match.jaro_winkler_similarity(m.SNAME,m2.SNAME) > 80
      AND utl_match.jaro_winkler_similarity(m.FNAME,m2.FNAME) > 80 )
    )
Where dups_key IS NOT NULL;

And from that, I've extracted other informations that I needed if I'd have used the counters previously mentioned; so at the following link, here are others data extracted from the first query above: this link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM