问题
I've got this query:
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)
Its generated output is as follows:
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 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 21 23100.09
Here the values are repeating. I tried to eliminate the repeating with a group by
, but didn't succeed. Could you help me?
The required output is this:
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 134 23100.09
2012 08 32 127 -1 10019 3665 21 714 723 135 23100.09
2012 08 32 128 -1 10019 3665 21 714 723 134 23100.09
2012 08 32 128 -1 10019 3665 21 714 723 135 23100.09
The main thing is year
, month
, week
, cpg
, dept
, cust_id
, r_id
, p1
, ic
, b1
, p2
, b2
it should be unique row. Is it achievable using analytical functions or do I need to write PL/SQL?
回答1:
Sure, you can use analytics, something like this:
SELECT DISTINCT YEAR, MONTH, WEEK, CPG, DEPT, CUST_ID, R_ID,
SUM(P1) OVER (PARTITION BY YEAR,MONTH,WEEK, CPG, DEPT, CUST_ID, R_ID) P1,
SUM(IC) OVER (PARTITION BY YEAR,MONTH,WEEK, CPG, DEPT, CUST_ID, R_ID) IC,
SUM(B1) OVER (PARTITION BY YEAR,MONTH,WEEK, CPG, DEPT, CUST_ID, R_ID) B1,
P2, B2, TOT, TOTAL_TXN, TOTAL_AMT
FROM (your_query)
来源:https://stackoverflow.com/questions/12614209/can-i-use-oracle-analytical-function-here