1

我是 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

这里的值是重复的。我试图用 a 消除重复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

主要的是year, month, week, cpg, dept, cust_id, r_id, p1, ic, b1, p2,b2应该是唯一的行。使用分析函数可以实现还是我需要编写 PL/SQL?我的问题合理吗?

4

1 回答 1

1

with t1 as( select 2012 YEAR1, 08 MONTH1, 32 WEEK, 127 CPG, -1 DEPT, 10019 CUST_ID, 3665 R_ID, null P1, null IC, 723 B1,null P2, null B2, 23100.09 TOTAL from dual union all -- select 2012, 08, 32, 127, -1, 10019, 3665, null, null, null, null, 135 , 23100.09 from dual union all -- 选择 2012, 08, 32, 127, -1, 10019, 3665, null , null, null , null, 134, 23100.09 from dual union all 选择 2012, 08, 32, 127, -1, 10019, 3665, null, 714 , null, null, null, 23100.09 from dual union all -- 选择 2012, 08, 32, 127, -1, 10019, 3665, null, null ,null, 41, null, 23100.09 from dual union all -- 选择 2012, 08, 32, 127, -1, 10019, 3665, null, null, null, 42, null, 23100.09 from dual union all -- 选择 2012, 08, 32, 127, -1, 10019, 3665, 21 , null, null, null, null, 23100。09 from dual union all -- 选择 2012, 08, 32, 128, -1, 10019, 3665, null, null, null, null, 134 , 23100.09 from dual union all -- 选择 2012, 08, 32, 128, - 1, 10019, 3665, null, null, null, null, 135 , 23100.09 from dual union all select 2012, 08, 32, 128, -1, 10019, 3665, null, null, 723 , null, null, 23100.09 from dual union all select 2012, 08, 32, 128, -1, 10019, 3665, null, 714 , null, null, null, 23100.09 from dual union all -- 选择 2012, 08, 32, 128, -1, 10019, 3665 , null, null ,null, 41, null, 23100.09 from dual union all -- 选择 2012, 08, 32, 128, -1, 10019, 3665, null, null, null, 42, null, 23100.09 from dual union all - - 选择 2012, 08, 32, 128, -1, 10019, 3665, 21 , null, null, null, null, 23100.09 from dual ) 选择 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, t1p1.total from (选择 YEAR1, month1, week, cpg, dept, cust_id, r_id, p1 ,从 t1 开始的总计,其中 p1 不为空) t1p1,(选择 YEAR1,month1,week,cpg,dept,cust_id,r_id,ic from t1,其中 ic 不为空)t1ic,(选择YEAR1, month1, week, cpg, dept, cust_id, r_id, b1 from t1 where b1 is not null) t1b1, (选择 YEAR1, month1, week, cpg, dept, cust_id, r_id, p2 from t1 where p2 is not null) t1p2,(从 t1 中选择 YEAR1、month1、week、cpg、dept、cust_id、r_id、b2,其中 b2 不为空) t1b2 其中 t1p1.year1 = t1ic.year1(+) 和 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 = t1ic.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(+) and t1p1.r_id = t1p2.r_id (+) and 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 /week(+) 和 t1p1.week = t1b2.week(+) 和 t1p1.cpg = t1ic.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(+) and t1p1.r_id = t1b1.r_id(+) and t1p1.r_id = t1p2.r_id (+) and 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 /week(+) 和 t1p1.week = t1b2.week(+) 和 t1p1.cpg = t1ic.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(+) and t1p1.r_id = t1b1.r_id(+) and t1p1.r_id = t1p2.r_id (+) and 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 /部门 = t1b1.dept(+) 和 t1p1.dept = t1p2.dept(+) 和 t1p1.dept = t1b2.dept(+) 和 t1p1.cust_id = t1ic.cust_id(+) 和 t1p1.cust_id = t1b1.cust_id( +) and t1p1.cust_id = t1p2.cust_id (+) and t1p1.cust_id = t1b2.cust_id(+) and t1p1.r_id = t1ic.r_id(+) and t1p1.r_id = t1b1.r_id(+) and t1p1.r_id = t1p2.r_id (+) 和 t1p1.r_id = t1b2.r_id(+) 按 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 /部门 = t1b1.dept(+) 和 t1p1.dept = t1p2.dept(+) 和 t1p1.dept = t1b2.dept(+) 和 t1p1.cust_id = t1ic.cust_id(+) 和 t1p1.cust_id = t1b1.cust_id( +) and t1p1.cust_id = t1p2.cust_id (+) and t1p1.cust_id = t1b2.cust_id(+) and t1p1.r_id = t1ic.r_id(+) and t1p1.r_id = t1b1.r_id(+) and t1p1.r_id = t1p2.r_id (+) 和 t1p1.r_id = t1b2.r_id(+) 按 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 /t1p1.dept, t1p1.cust_id, t1p1.r_id, t1p1.p1,t1ic.ic, t1b1.b1, t1p2.p2, t1b2.b2 /t1p1.dept, t1p1.cust_id, t1p1.r_id, t1p1.p1,t1ic.ic, t1b1.b1, t1p2.p2, t1b2.b2 /

于 2012-10-27T04:08:39.657 回答