我正在为 DWH 目的编写此查询。
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_ACC TOTAL_TXN TOTAL_AMT
2012 08 32 127 -1 10019 3665 134 1 1
2012 08 32 127 -1 10019 3665 135 1 1
2012 08 32 127 -1 10019 3665 723 1 1
2012 08 32 127 -1 10019 3665 714 1 1
2012 08 32 127 -1 10019 3665 21 1 1
2012 08 32 128 -1 10019 3665 134 1 1
2012 08 32 128 -1 10019 3665 135 1 1
2012 08 32 128 -1 10019 3665 723 1 1
2012 08 32 128 -1 10019 3665 714 1 1
2012 08 32 128 -1 10019 3665 21 1 1
这里的值是重复的,我尝试了可能的 group by。
所需的输出是
YEAR MONTH WEEK CPG DEPT CUST_ID R_ID P1 IC B1 P2 B2 TOTAL_ACC TOTAL_TXN TOTAL_AMT
2012 08 32 127 -1 10019 3665 21 714 723 134 1 1
2012 08 32 127 -1 10019 3665 21 714 723 135 1 1
2012 08 32 128 -1 10019 3665 21 714 723 134 1 1
2012 08 32 128 -1 10019 3665 21 714 723 135 1 1
主要是年,月,周,cpg,部门,cust_id,r_id,p1,ic,b1,p2,b2 它应该是唯一的行。使用分析函数可以实现还是我需要编写pl/sql