Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. START WITH and CONNECT BY in Oracle SQL
  2. CUSTOMER_ID   PAYING_ACCOUNT_ID   PARENT_ACCOUNT_ID     ACCOUNT_ID   COMPANY_ID
  3. 24669         24669               24669                 24669        0
  4. 24671         24671               24669                 24671        0  
  5. 24670         24670               24669                 24670        0
  6. 3385217       3385217             24670                 3385217      0
  7. 158           158                 158                   158          0
  8. 159           159                 158                   159          0
  9. 160           160                 159                   160          0
  10. 161           161                 160                   161          0
  11. 162           162                 160                   162          0
  12. 180           180                 180                   180          0
  13.        
  14. CREATE TABLE "SYSTEM"."ACCOUNT"
  15. ("CUSTOMER_ID"       NUMBER(20,0) NOT NULL ENABLE,
  16. "PAYING_ACCOUNT_ID" NUMBER(20,0),
  17. "PARENT_ACCOUNT_ID" NUMBER(20,0),
  18. "ACCOUNT_ID"        NUMBER,
  19. "COMPANY_ID"        NUMBER)
  20.        
  21. select  lpad(' ', 2*level) || A.ACCOUNT_ID AS LEVEL_LABEL,
  22.            LEVEL,
  23.           A.*
  24.       from ACCOUNT A
  25. start with PARENT_ACCOUNT_ID IN
  26.                        (select PARENT_ACCOUNT_ID
  27.                           from ACCOUNT
  28.                          where ACCOUNT_ID IN
  29.                                         (select PARENT_ACCOUNT_ID
  30.                                            from ACCOUNT
  31.                                           where parent_account_id != account_id)
  32.                                             and ACCOUNT_ID = PARENT_ACCOUNT_ID)
  33.    CONNECT BY NOCYCLE  PRIOR A.ACCOUNT_ID = A.PARENT_ACCOUNT_ID;
  34.        
  35. SELECT level, * FROM accounts
  36.  START WITH parent_account_id = account_id
  37.  CONNECT BY PRIOR account_id = parent_account_id
  38.          AND account_id <> parent_account_id