0

enter image description hereenter image description hereUnable to migrate the merge code that contains level,connect by prior,start with (in postgresql from oracle).

Code:

MERGE INTO ABC.CUSTOMER CT
 USING (SELECT CUST_NO,
               CUST_HIGH_PARENT_NO,
               CUST_CORP_POS,
               CUST_CORP_SUBS,
               TIMESTAMP,
               LEVEL LVL,
               (SELECT MAX(LEVEL)
                  FROM ABC.CUSTOMER
                 START WITH CUST_NO = CUST_NO
                CONNECT BY PRIOR CUST_PARENT_NO = CUST_NO) MXLVL,
               (SELECT CUST_NO
                  FROM ABC.CUSTOMER
                 WHERE CONNECT_BY_ISLEAF = 1
                 START WITH CUST_NO = CUST_NO
                CONNECT BY PRIOR CUST_PARENT_NO = CUST_NO) HIGHPARENT
          FROM ABC.CUSTOMER
         START WITH CUST_NO = CUST_NO
        CONNECT BY PRIOR CUST_PARENT_NO = CUST_NO) A
 ON (UPC.CUST_NO = A.CUST_NO)
 WHEN MATCHED THEN
   UPDATE
      SET 
              CUST_CORP_SUBS = (SELECT COUNT(*) - 1
                                      FROM ABC.CUSTOMER
                                     START WITH CUST_NO =
                                                CT.CUST_NO
                                    CONNECT BY PRIOR CUST_NO =
                                                CUST_PARENT_NO),
          CUST_CORP_POS    = A.MXLVL - A.LVL,
          CUST_HIGH_PARENT_NO= HIGHPARENT,
          TIMESTAMP              = SYSDATE; 

I've

Tried below but unable to it fully. I need help.

Use recursive cte ,tried to use update ,subquery ,level.

Postgresql Code: The oracle query merging two rows in a database.

UPDATE ABC.CUSTOMER CT
      SET  CUST_PAYOR_SUBS = (SELECT COUNT(*) 
                                 FROM ABC.CUSTOMER
                                    (WITH RECURSIVE CUST AS
      (SELECT COUNT(*) FROM ABC.CUSTOMER WHERE CUST_NO = CT.CUST_NO
         UNION ALL 
         SELECT COUNT(*) FROM ABC.CUSTOMER C ,ABC.CUSTOMER CUST
             WHERE C.CUST_NO = CUST.CUST_PAYOR_NO)
             SELECT COUNT(*)
             FROM ABC.CUSTOMER CUST),
      CUST_PAYOR_POS    = A.MXLVL - A.LVL,
      CUST_HIGH_PAYOR_NO = A.HIGHPAYOR,
      TIMESTAMP             = CURRENT_TIMESTAMP::TIMESTAMP(0)
      FROM 
          (WITH RECURSIVE CTE AS
      (SELECT CUST_NO,
           CUST_HIGH_PAYOR_NO,
           CUST_PAYOR_POS,
           CUST_PAYOR_SUBS, 1 as level
       FROM ABC.CUSTOMER 
       WHERE CUST_NO = '00009'---UPC.CUST_NO
         UNION ALL 
         SELECT C.CUST_NO,
           C.CUST_HIGH_PAYOR_NO,
           C.CUST_PAYOR_POS,
           C.CUST_PAYOR_SUBS,
           C.level+1
       FROM CTE C
       JOIN ABC.CUSTOMER CUST ON C.CUST_NO = CUST.CUST_PAYOR_NO)
        SELECT * FROM CTE) MXLVL,
      WHERE UPC.CUST_NO = A.CUST_NO
Hoper
  • 13
  • 3
  • For the "CONNECT BY..." conversion you need to use a recursive CTE - take a look at https://www.2ndquadrant.com/en/blog/oracle-to-postgresql-start-with-connect-by/ and https://stackoverflow.com/questions/22296248/connect-by-root-equivalent-in-postgres . For the MERGE conversion, have a look at https://stackoverflow.com/questions/19395690/migrating-an-oracle-merge-statement-to-a-postgresql-upsert-statement . – Bob Jarvis - Слава Україні Feb 22 '23 at 17:45
  • @BobJarvis-СлаваУкраїні I tried but not sure about level & connectbyisleaf implementation in postgresql. – Hoper Feb 22 '23 at 18:37
  • Welcome to the SO community. The community will help with your issues, but there are certain expectations on you. Please take a few minuets to take the [Tour](https://stackoverflow.com/tour) and review [ask]. Then update your question to include sample data, table definition (ddl scripts), the expected results of that data, all as text - **no images**. Even better create a [fiddle](https://dbfiddle.uk/). – Belayer Feb 22 '23 at 19:06
  • Converting an Oracle Hierarchical to Postgres is basically *a fools errand*. For the most part you will discover that it is not a conversion so much as a complete rewrite. Oracle `level` indicates how deep you are in the hierarchy, and `connect_by_isleaf` indicates the bottom of the hierarchy. In Postgres you can get `level` by introducing a column in the *non-recursive term* then incrementing that column on each `recursive term`. Connect_by_isleaf much more difficult. Build a *path* column then once the recursive query is done pass the result and *lead()* to see if it is the same path. – Belayer Feb 22 '23 at 19:32
  • @Belayer I need help.I've added postgre code but that's not good. – Hoper Feb 22 '23 at 20:19
  • No need to use an UPDATE, Postgres supports MERGE [as well](https://www.postgresql.org/docs/current/sql-merge.html) –  Feb 22 '23 at 21:14
  • https://stackoverflow.com/questions/42022545 –  Feb 22 '23 at 21:16
  • That will take time to upgrade. Currently its in preview mode.So update is the only choice. @a_horse_with_no_name – Hoper Feb 22 '23 at 21:53
  • No, Postgres 15 is not "in preview" mode. It has been released and already received two regular minor upgrades. If you migrate to Postgres today, there is absolutely not reason to use anything less than Postgres 15 –  Feb 22 '23 at 21:55
  • @a_horse_with_no_name That upgrade must be supported by AWS RDS. As of now (its in preview mode only).Supports upto 14.5 Can you help with the code. – Hoper Feb 22 '23 at 22:03
  • I repeat *update your question to include sample data, table definition (ddl scripts), the expected results of that data*. Without that there is not much help to be given. Posting the attempted Postgres is good. The results of that query might also be helpful. – Belayer Feb 22 '23 at 22:29
  • @Belayer Hi, I've added two images. From the subquery 2 rows were fetched & that 2 rows merging/updating. – Hoper Feb 23 '23 at 13:00

0 Answers0