Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7085705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:27:27+00:00 2026-05-28T07:27:27+00:00

I have a complex sql need to solve, the idea is to determine the

  • 0

I have a complex sql need to solve, the idea is to determine the ADMIN_ID for a particular CUSTOMER_ID. The rule to determine the ADMIN_ID is as below:

  1. To determine the ADMIN_ID for a particular account, the system should query the
    account from bottom up to the parent, and the first account with TXT01 is not null
    it the parent.

  2. The ADMIN_ID is only applicable to CUSTOMER Group E.

  3. In case Top account have an absence of a TXT01, it should give a warning message

  4. Only CUSTOMER_ID = PAYING_ACCOUNT_ID need to analyze

Account Table

CUSTOMER_ID       PAYING_ACCOUNT_ID         PARENT_ACCOUNT_ID
3271516           3271516                   719216
1819276           1819276                   810546
719216            719216                    719216
810546            810546                    810547
810547            810547                    810547
999999            111111                    111111
111111            111111                    111111
123456            123456                    231
231               231                       231    

Customer Table

CUSTOMER_ID         TXT01
719216              TOM
810546              NULL
810547              JIM
3271516             NULL
1819276             NULL
999999              NULL
111111              BEN
123456              NULL
231                 NULL     

Customer Group

CUSTOMER_ID         GROUP
719216              E
810546              E
810547              E
3271516             E
1819276             E
999999              E
111111              E
123456              E
231                 E
888                 A

Output

CUSTOMER_ID       PAYING_ACCOUNT_ID        PARENT_ACCOUNT_ID        ADMIN_ID
3271516           3271516                    719216                  TOM
1819276           1819276                    810546                  JIM
719216            719216                     719216                  TOM
810546            810546                     810547                  JIM
810547            810547                     810547                  JIM
111111            111111                     111111                  BEN
123456            123456                     231                     Warning!!
231               231                        532                     Warning!!   

DDL

CREATE TABLE ACCOUNT (CUSTOMER_ID NUMBER(20) NOT NULL,
PAYING_ACCOUNT_ID NUMBER(20),
PARENT_ACCOUNT_ID NUMBER(20));

CREATE TABLE CUSTOMER (CUSTOMER_ID NUMBER(20) NOT NULL,
TXT01 VARCHAR2(20));

CREATE TABLE CUSTOMER_GROUP (CUSTOMER_ID NUMBER(20) NOT NULL,
GROUP VARCHAR2(20));

INSERT INTO ACCOUNT VALUES (3271516,3271516,719216);
INSERT INTO ACCOUNT VALUES (1819276,1819276,810546);
INSERT INTO ACCOUNT VALUES (719216,719216,719216);
INSERT INTO ACCOUNT VALUES (810546,810546,810547);
INSERT INTO ACCOUNT VALUES (810547,810547,810547);
INSERT INTO ACCOUNT VALUES (999999,111111,111111);
INSERT INTO ACCOUNT VALUES (111111,111111,111111);
INSERT INTO ACCOUNT VALUES (123456,123456,231);
INSERT INTO ACCOUNT VALUES (231,231,231);
INSERT INTO CUSTOMER VALUES (719216,'TOM');
INSERT INTO CUSTOMER VALUES (810546,NULL);
INSERT INTO CUSTOMER VALUES (810547,'JIM');
INSERT INTO CUSTOMER VALUES (3271516,NULL);
INSERT INTO CUSTOMER VALUES (1819276,NULL);
INSERT INTO CUSTOMER VALUES (999999,NULL);
INSERT INTO CUSTOMER VALUES (111111,'BEN');
INSERT INTO CUSTOMER VALUES (123456,NULL);
INSERT INTO CUSTOMER VALUES (231,NULL);
INSERT INTO CUSTOMER_GROUP VALUES (719216,'E');
INSERT INTO CUSTOMER_GROUP VALUES (810546,E);
INSERT INTO CUSTOMER_GROUP VALUES (810547,'E');
INSERT INTO CUSTOMER_GROUP VALUES (3271516,'E');
INSERT INTO CUSTOMER_GROUP VALUES (1819276,'E');
INSERT INTO CUSTOMER_GROUP VALUES (999999,'E');
INSERT INTO CUSTOMER_GROUP VALUES (111111,'E');
INSERT INTO CUSTOMER_GROUP VALUES (123456,'E');
INSERT INTO CUSTOMER_GROUP VALUES (231,'E');
INSERT INTO CUSTOMER_GROUP VALUES (888,'A');    

This is my code..still doing but stuck in some point. Appreciate guru here can gv me a hand

WITH myData AS (
SELECT CUSTOMER_ID, TXT01 FROM CUSTOMER WHERE CUSTOMER_ID IN 
(SELECT CUSTOMER_ID from CUSTOMER_GROUP WHERE GROUP = 'E')
)   
SELECT v.*
FROM
(SELECT m.* ,
CASE WHEN TXT01 IS NOT NULL THEN TXT01
ELSE ( *STUCK HERE*
END ADMIN_ID
FROM myData m) v
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:27:28+00:00Added an answer on May 28, 2026 at 7:27 am

    Try this

    with acc_hierarchy as
     (select level lev, ac.*, connect_by_root(ac.paying_account_id) account_id
        from account ac
      connect by prior ac.parent_account_id = ac.paying_account_id
             and prior ac.paying_account_id <> prior ac.parent_account_id),
    acc_customers as
     (select ah.account_id, min(c.txt01) keep(dense_rank first order by ah.lev) admin_id
        from acc_hierarchy ah
        left join (select cus.customer_id, cus.txt01
                    from customer cus
                    join customer_group cg
                      on cg.customer_id = cus.customer_id
                     and cg."GROUP" = 'E') c
          on c.customer_id = ah.customer_id
       where c.txt01 is not null
       group by ah.account_id)
    select a.customer_id, a.paying_account_id, a.parent_account_id, NVL(ac.admin_id, 'Warning!!') admin_id
      from account a
      left join acc_customers ac
        on ac.account_id = a.paying_account_id
     where a.customer_id = a.paying_account_id
    ;
    

    Here are my results:

    SQL> with acc_hierarchy as
      2   (select level lev, ac.*, connect_by_root(ac.paying_account_id) account_id
      3      from account ac
      4    connect by prior ac.parent_account_id = ac.paying_account_id
      5           and prior ac.paying_account_id <> prior ac.parent_account_id),
      6  acc_customers as
      7   (select ah.account_id, min(c.txt01) keep(dense_rank first order by ah.lev) admin_id
      8      from acc_hierarchy ah
      9      left join (select cus.customer_id, cus.txt01
     10                  from customer cus
     11                  join customer_group cg
     12                    on cg.customer_id = cus.customer_id
     13                   and cg."GROUP" = 'E') c
     14        on c.customer_id = ah.customer_id
     15     where c.txt01 is not null
     16     group by ah.account_id)
     17  select a.customer_id, a.paying_account_id, a.parent_account_id, NVL(ac.admin_id, 'Warning!!') admin_id
     18    from account a
     19    left join acc_customers ac
     20      on ac.account_id = a.paying_account_id
     21   where a.customer_id = a.paying_account_id;
    
    CUSTOMER_ID PAYING_ACCOUNT_ID PARENT_ACCOUNT_ID ADMIN_ID
    ----------- ----------------- ----------------- --------------------
        3271516           3271516            719216 TOM
        1819276           1819276            810546 JIM
         719216            719216            719216 TOM
         810546            810546            810547 JIM
         810547            810547            810547 JIM
         111111            111111            111111 BEN
         123456            123456               231 Warning!!
            231               231               231 Warning!!
    
    8 rows selected.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have couple of complex SQL and need to convert it into an ActiveRecord
I have a fairly complex SQL query, where we'll need to return a number
I have this one gigantic complex LINQ to SQL query that I need to
I have a complex sql query that I have in a stored procedure and
I have a complex SQL problem in MS SQL Server, and in drawing on
I have a complex sorting problem with my SQL statement. I have a table
I have a complex Powershell script that gets run as part of a SQL
On SQL Server 2005, I have a complex multi-level allocation process which looks like
I have a complex SQL Query, that needs to be filtered further. Part of
I have some extremely complex queries that I need to use to generate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.