2012-10-20 60 views
1

我是Oracle Analytics的新手。任何人都可以幫我解決以下問題。Oracle分析函數用法

SELECT year, 
      month, 
      week, 
      C.cpg_pk              CPG, 
      C.dep_pk              DEPT, 
      T.cust_id              CUST_ID, 
      D1.r_id               R_ID, 
      Decode(d2.at_code, '3', func1.Get_att(d2.at_code, D2.val_code)) AS P1, 
      Decode(d2.at_code, '2', func1.Get_att(d2.at_code, D2.val_code)) AS IC, 
      Decode(d2.at_code, '1', func1.Get_att(d2.at_code, D2.val_code)) AS B1, 
      Decode(func1.Get_att(d2.at_code, D2.val_code), 2, d2.at_code) AS P2, 
      Decode(func1.Get_att(d2.at_code, D2.val_code), 5, d2.at_code) AS B2, 
      Count(DISTINCT A.cust_id)          TOTAL_ACC 
      , 
      Count(DISTINCT T.txn_pk) 
      TOTAL_TXN, 
      SUM(am_amount)             TOTAL_AMT 
    FROM t_header T, 
      cust_master A, 
      tx_details1 D1, 
      tx_details2 D2, 
      cpg_master C 
    WHERE A.TYPE = 0 
      AND T.cust_id = A.cust_id 
      AND T.txn_pk = 5001 
      AND T.txn_pk = D1.txn_pk 
      AND T.txn_pk = D2.txn_pk 
      AND D1.cpg_pk = C.cpg_pk 
      AND D1.op = 1 
    GROUP BY year, 
       month, 
       week, 
       C.cpg_pk, 
       C.dep_pk, 
       t.cust_id, 
       D1.r_id, 
       Decode(d2.at_code, '3', func1.Get_att(d2.at_code, D2.val_code)), 
       Decode(d2.at_code, '2', func1.Get_att(d2.at_code, D2.val_code)), 
       Decode(d2.at_code, '1', func1.Get_att(d2.at_code, D2.val_code)), 
       Decode(func1.Get_att(d2.at_code, D2.val_code), 2, d2.at_code), 
       Decode(func1.Get_att(d2.at_code, D2.val_code), 5, d2.at_code) 

其產生的輸出如下所示:

YEAR MONTH WEEK CPG DEPT CUST_ID R_ID P1 IC B1 P2 B2 TOTAL 
2012 08  32  127 -1  10019 3665      134 23100.09 
2012 08  32  127 -1  10019 3665      135 23100.09 
2012 08  32  127 -1  10019 3665    723   23100.09 
2012 08  32  127 -1  10019 3665  714    23100.09 
2012 08  32  127 -1  10019 3665     41  23100.09 
2012 08  32  127 -1  10019 3665     42  23100.09 
2012 08  32  127 -1  10019 3665  21     23100.09 
2012 08  32  128 -1  10019 3665      134 23100.09 
2012 08  32  128 -1  10019 3665      135 23100.09 
2012 08  32  128 -1  10019 3665    723   23100.09 
2012 08  32  128 -1  10019 3665  714    23100.09 
2012 08  32  128 -1  10019 3665     41  23100.09 
2012 08  32  128 -1  10019 3665     42  23100.09 
2012 08  32  128 -1  10019 3665  21     23100.09 

這裏的值被重複。我試圖用group by消除重複,但沒有成功。你可以幫幫我嗎?

所需的輸出是這樣的:

YEAR MONTH WEEK CPG DEPT CUST_ID R_ID P1 IC B1 P2 B2 TOTAL_AMT 
--------------------------------------------------------------------------------- 
2012 08  32  127 -1  10019 3665 21 714 723 41 134 23100.09 
2012 08  32  127 -1  10019 3665 21 714 723 41 135 23100.09 
2012 08  32  127 -1  10019 3665 21 714 723 42 134 23100.09 
2012 08  32  127 -1  10019 3665 21 714 723 42 135 23100.09 
2012 08  32  128 -1  10019 3665 21 714 723 41 134 23100.09 
2012 08  32  128 -1  10019 3665 21 714 723 41 135 23100.09 
2012 08  32  128 -1  10019 3665 21 714 723 42 134 23100.09 
2012 08  32  128 -1  10019 3665 21 714 723 42 135 23100.09 

最主要的是yearmonthweekcpgdeptcust_idr_idp1icb1p2b2它應該是唯一的行。使用分析函數可以實現,還是需要編寫PL/SQL?我的問題很敏感嗎?

+1

可能的重複[我可以在這裏使用Oracle分析函數嗎?](http://stackoverflow.com/questions/12614209/can-i-use-oracle-analytical-function-here) –

+0

是的,但輸出不是給我期望的結果 – user1760970

+1

因此,回答託尼的回答說爲什麼,並改進原來的問題 - 不要問一個相同的問題。請閱讀[FAQ](http://stackoverflow.com/faq)。 –

回答

1

與T1 如( 選擇2012 YEAR1,08 MONTH1,32周,127 CPG,-1 DEPT,10019 CUST_ID,3665 R_ID,空P1,空IC,723 B1,空P2,空B2,23100.09 TOTAL來自雙聯合所有 - 從雙聯合中選擇2012年,08年,32年,127年,-1年,10019年,3665年,空,空,空,空,135,23100.09全部 - select 2012,08,32,127, -1,10019,3665,null,null,null,null,134,23100.09 from double union全部 select 2012,08,32,127,-1,10019,3665,null,714,null,null,null,23100.09來自雙重聯合的所有 - 從雙重聯合中選擇2012,08,32,127,-1,10019,3665,null,null,null,41,null,23100.09全部 - select 2012,08,32,127, -1,10019,3665,null,n來自雙聯盟的所有 - 從雙聯盟中選擇2012,08,32,127,-1,10019,3665,21,null,null,null,null,23100.09全部 - 從雙重聯合中選擇2012,08,32,128,-1,10019,3665,null,null,null,134,23100.09全部 - 選擇2012,08,32,128,-1,10019,3665, null,null,null,null,135,23100.09 from double union全部 select 2012,08,32,128,-1,10019,3665,null,null,723,null,null,23100.09 from double union全部 select 2012 ,08,32,128,-1,10019,3665,null,714,null,null,null,來自雙聯合的所有的的23100.09 - 選擇2012,08,32,128,-1,10019,3665,null, null,null,41,null,23100.09 from double union全部 - 從d中選擇2012,08,32,128,-1,10019,3665,null,null,null,42,null,23100.09聯盟全部 - 從雙 )中選擇2012,08,32,128,-1,10019,3665,21,null,null,null,null,23100.09)選擇t1p1.YEAR1,t1p1.month1, ,選擇YEAR1,month1,week,t1p1.cpg,t1p1.dept,t1p1.cust_id,t1p1.r_id,t1p1.p1,t1ic.ic,t1b1.b1,t1p2.p2,t1b2.b2,t1p1.total cpg,dept,cust_id,r_id,p1,total from t1其中p1不爲空)t1p1, (從t1選擇YEAR1,month1,week,cpg,dept,cust_id,r_id,ic,其中ic不爲null)t1ic, (從t1選擇YEAR1,month1,week,cpg,dept,cust_id,r_id,b1,其中b1不爲空)t1b1, (從t1選擇YEAR1,month1,week,cpg,dept,cust_id,r_id,p2其中p2是不是null)t1p2, (從t1選擇YEAR1,month1,week,cpg,dept,cust_id,r_id,b2,其中b2不爲空)t1b2 (+)和t1p1.year1 = t1b1.year1(+)和t1p1.year1 = t1p2.year1(+)和t1p1.year1 = t1b2.year1(+) 和t1p1.month1 = t1ic.month1(+)和t1p1.month1 = t1b1.month1(+)和t1p1.month1 = t1p2.month1(+)和t1p1。month1 = t1b2.month1(+) and t1p1.week = t1ic.week(+)and t1p1.week = t1b1.week(+)and t1p1.week = t1p2.week(+)and t1p1.week = t1b2.week (+) 和t1p1.cpg = t1c.cpg(+)和t1p1.cpg = t1b1.cpg(+)和t1p1.cpg = t1p2.cpg(+)和t1p1.cpg = t1b2.cpg(+) 和t1p1.dept = t1ic.dept(+)和t1p1.dept = t1b1.dept(+)和t1p1.dept = t1p2.dept(+)和t1p1.dept = t1b2.dept(+) 和t1p1.cust_id = t1ic .cust_id(+)和t1p1.cust_id = t1b1.cust_id(+)和t1p1.cust_id = t1p2.cust_id(+)和t1p1.cust_id = t1b2.cust_id(+) 和t1p1.r_id = t1ic.r_id(+)和t1p1.r_id = t1b1.r_id(+)和t1p1.r_id = t1p2.r_id(+)和t1p1.r_id = t1b2.r_id(+) order by t1p1.YEAR1,t1p1.month1,t1p1.week,t1p1。 cpg,t1p1.dept,t1p1.cust_id,t1p1.r_id,t1p1.p1,t1ic.ic,t1b1.b1,t1p2.p2,t1b2.b2 /

+0

添加外連接後,此功能正常工作並將所有屬性拆分爲單個查詢 – user1760970