2012-01-20 11 views
0

私は複雑なSQLを解決する必要がある、アイデアは特定のCUSTOMER_IDのADMIN_IDを判断することです。 ADMIN_IDを決定するルールは以下の通りです:複雑なSQLの問題複数の検索で

  1. 、特定のアカウントのADMIN_IDを決定するには、システムが親まで下から アカウントを照会しなければならない、とTXT01との最初のアカウントが
    nullではありません それは親です。

  2. ADMIN_IDは、顧客グループにのみ適用されるE.

  3. ケーストップアカウントでTXT01の不在を持って、それが警告メッセージ

  4. のみCUSTOMER_ID = PAYING_ACCOUNT_IDを与える必要があります

    を分析する必要があります

アカウント表

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_ID   TXT01 
719216    TOM 
810546    NULL 
810547    JIM 
3271516    NULL 
1819276    NULL 
999999    NULL 
111111    BEN 
123456    NULL 
231     NULL  

顧客グループ

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

出力

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');  

これはやって私のcode..stillですが、いくつかの点で立ち往生。ここでは、この

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 
; 

を試してみてください私の手をGVでき

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

古い質問を確認してください。それはあなたに言われたように、あなたは答えられて明らかにあなたを助けてくれた質問の束を持っていますが、あなたはアンヴェールを受け入れていません。それ以外の人は、あなたを助けてくれる人がいないかもしれません。 –

答えて

0

にここに教祖に感謝することは私の結果は以下のとおりです。

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

質問は私のためにかなり複雑です。どこでも私はそれを理解しようとするでしょう。 – user871695

0
SELECT 
C.CUSTOMER_ID, nvl(a.TXT01, 'warning!') 
FROM CUSTOMER c 
    JOIN CUSTOMER_GROUP cg ON (cg.customer_id = c.customer_id and cg.group = 'E') 
    JOIN ACCOUNT a ON (a.customer_id = c.customer_id) 
WHERE c.CUSTOMER_ID = a.PAYING_ACCOUNT_ID