oracle如何在单行中返回值

时间:2012-09-20 05:39:45

标签: oracle

SELECT 
     YEAR
    ,MONTH
    ,WEEK
    ,A.customer_id      
    ,C.CP_PK
    ,C.DEPT_PK                                  ,decode (e.EN_TYPE,'11', e.EN_PK ) as COL1
    ,decode (e.EN_TYPE,'11', d2.ATTR_PK) as COL2
    ,decode (e.EN_TYPE,'10', e.EN_PK ) as COL3
    ,decode (e.EN_TYPE,'13', e.EN_PK ) as COL4
    ,DECODE (e.EN_TYPE,'13', d2.ATTR_PK) as COL5
    ,COUNT(*)       TXN_COUNT
    ,SUM(A_AMOUNT) TOTAL_AMT
FROM TXN_HEADER  T
     ,CUSTOMER A 
     ,TXN_DETAIL D1
     ,TXN_DETAIL D2
     ,CP_ATTRIBUTE C
     ,GEN_LOOKUP e
WHERE   A.TYPE  =  0            
AND     T.CUSTOMER_ID = A.customer_id
AND     T.TXN_PK= D1.TXN_PK
AND     T.TXN_PK= D2.TXN_PK
AND     D1.CP_PK = C.CP_PK
AND     D1.OPERATION = 1 
and     e.en_code = d2.new_code
AND     T.TXN_PK= 12313
GROUP BY 
     YEAR
    ,MONTH
    ,WEEK
    ,A.customer_id      
    ,C.CP_PK
    ,C.DEPT_PK          
    ,decode (e.EN_TYPE,'11', e.EN_PK ) 
    ,decode (e.EN_TYPE,'11', d2.ATTR_PK)
    ,decode (e.EN_TYPE,'10', e.EN_PK) 
    ,decode (e.EN_TYPE,'13', e.EN_PK) 
    ,DECODE (e.EN_TYPE,'13', d2.ATTR_PK) 

生成的输出如下

YEAR    MONTH   WEEK    CUSTOMER ID CP_PK   DEPT_PK COL1    COL2    COL3    COL4    COL5    TXN_COUNT   TOTAL_AMT
2012    8   32  119 127 -1              74  61  1   
2012    8   32  119 127 -1  73  63              1   
2012    8   32  119 127 -1          67          1   
2012    8   32  119 128 -1  73  63              1   
2012    8   32  119 128 -1              74  61  1   
2012    8   32  119 128 -1          67          1   

这里col1到col5是分散的。我想要连续的所有值。

像这样

YEAR    MONTH   WEEK    CUSTOMER ID CP_PK   DEPT_PK COL1    COL2    COL3    COL4    COL5    TXN_COUNT   TOTAL_AMT


    2012    8   32  119 127 -1  73  63  67  74  61  1   
    2012    8   32  119 128 -1  73  63  67  74  61  1

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT 
     YEAR
    ,MONTH
    ,WEEK
    ,A.customer_id      
    ,C.CP_PK                                                            
    ,C.DEPT_PK
    ,min(decode (e.EN_TYPE,'11', e.EN_PK )) as COL1
    ,min(decode (e.EN_TYPE,'11', d2.ATTR_PK)) as COL2
    ,min(decode (e.EN_TYPE,'10', e.EN_PK )) as COL3
    ,min(decode (e.EN_TYPE,'13', e.EN_PK )) as COL4
    ,min(DECODE (e.EN_TYPE,'13', d2.ATTR_PK)) as COL5
    ,COUNT(*)       TXN_COUNT
    ,SUM(A_AMOUNT) TOTAL_AMT
FROM TXN_HEADER  T
     ,CUSTOMER A 
     ,TXN_DETAIL D1
     ,TXN_DETAIL D2
     ,CP_ATTRIBUTE C
     ,GEN_LOOKUP e
WHERE   A.TYPE  =  0            
AND     T.CUSTOMER_ID = A.customer_id
AND     T.TXN_PK= D1.TXN_PK
AND     T.TXN_PK= D2.TXN_PK
AND     D1.CP_PK = C.CP_PK
AND     D1.OPERATION = 1 
and     e.en_code = d2.new_code
AND     T.TXN_PK= 12313
GROUP BY 
     YEAR
    ,MONTH
    ,WEEK
    ,A.customer_id      
    ,C.CP_PK
    ,C.DEPT_PK