Oracle Analytic功能可能吗?

时间:2014-03-04 15:59:20

标签: sql oracle oracle11g

想知道是否有人建议如何使用任何oracle分析函数重写此sql。 Oracle版本11gR2。提前谢谢。

SELECT SUB_SEGMENT,
SUM(
CASE
  WHEN (TRANSACTION_MONTH = TO_CHAR(SYSDATE, 'MM'))
  AND (TRANSACTION_YEAR   = (TO_CHAR(SYSDATE, 'YYYY')))
  THEN TRAN_AMOUNT
  ELSE 0
END) AS MTD,
SUM(
CASE
  WHEN (TRANSACTION_MONTH = TO_CHAR(ADD_MONTHS(SYSDATE,  -1), 'MM'))
  AND (TRANSACTION_YEAR   = (TO_CHAR(ADD_MONTHS(SYSDATE, -1), 'YYYY')))
  THEN TRAN_AMOUNT
  ELSE 0
END) AS MONTH6,
SUM(
CASE
  WHEN (TRANSACTION_MONTH = TO_CHAR(ADD_MONTHS(SYSDATE,  -2), 'MM'))
  AND (TRANSACTION_YEAR   = (TO_CHAR(ADD_MONTHS(SYSDATE, -2), 'YYYY')))
  THEN TRAN_AMOUNT
  ELSE 0
END) AS MONTH5,
SUM(
CASE
  WHEN (TRANSACTION_MONTH = TO_CHAR(ADD_MONTHS(SYSDATE,  -3), 'MM'))
  AND (TRANSACTION_YEAR   = (TO_CHAR(ADD_MONTHS(SYSDATE, -3), 'YYYY')))
  THEN TRAN_AMOUNT
  ELSE 0
END) AS MONTH4,
SUM(
CASE
  WHEN (TRANSACTION_MONTH = TO_CHAR(ADD_MONTHS(SYSDATE,  -4), 'MM'))
  AND (TRANSACTION_YEAR   = (TO_CHAR(ADD_MONTHS(SYSDATE, -4), 'YYYY')))
  THEN TRAN_AMOUNT
  ELSE 0
END) AS MONTH3,
SUM(
CASE
  WHEN (TRANSACTION_MONTH = TO_CHAR(ADD_MONTHS(SYSDATE,  -5), 'MM'))
  AND (TRANSACTION_YEAR   = (TO_CHAR(ADD_MONTHS(SYSDATE, -5), 'YYYY')))
  THEN TRAN_AMOUNT
  ELSE 0
END) AS MONTH2,
SUM(
CASE
  WHEN (TRANSACTION_MONTH = TO_CHAR(ADD_MONTHS(SYSDATE,  -6), 'MM'))
  AND (TRANSACTION_YEAR   = (TO_CHAR(ADD_MONTHS(SYSDATE, -6), 'YYYY')))
  THEN TRAN_AMOUNT
  ELSE 0
END) AS MONTH1
FROM TRAN_TYPE
WHERE INCOMING_OUTGOING = 'I'
GROUP BY SUB_SEGMENT
ORDER BY DECODE(SUB_SEGMENT, 'Premium Plus', 1, 'Premium', 2, 'Upper Mass', 3);

1 个答案:

答案 0 :(得分:0)

解决方案 - 请尝试以下输出

select * from 

(select count(1) payments, trunc(transaction_date,'MM') month_ from 
(select level * 12 + 11024294 payment_id,
case when round(11 * level * dbms_random.value) < 400 then round(11 * level * dbms_random.value) * 20
     when round(11 * level * dbms_random.value) < 800 then round(11 * level * dbms_random.value) * 3 
     else round(11 * level * dbms_random.value) end amount,
case when mod(level,2) = 0 or mod(level,2) = 3 then 'Premium Plus'
     when mod(level,2) = 5 or mod(level,3) = 1 then 'Premium'
     else 'Upper Mass'
end sub_segment,
to_date(round (dbms_random.value (1, 28))
        || '-'
        || round (dbms_random.value (7, 12))
        || '-'
        || round (dbms_random.value (2014, 2014)),
        'DD-MM-YYYY')
transaction_date
from dual tran_type
connect by level <= 20000) transaction_data
group by trunc(transaction_date,'MM'))

pivot(max(payments) as payments for month_ in 
('01-Jul-14' as jul14, '01-Aug-14' as aug14, '01-Sep-14' as sep14,
 '01-Oct-14' as oct14, '01-Nov-14' as nov14, '01-Dec-14' as dec14));

解决方案扩展 -

select * from (select * from 

(select count(1) payments, trunc(transaction_date,'MM') month_ from 
(select level * 12 + 11024294 payment_id,
case when round(11 * level * dbms_random.value) < 400 then round(11 * level * dbms_random.value) * 20
     when round(11 * level * dbms_random.value) < 800 then round(11 * level * dbms_random.value) * 3 
     else round(11 * level * dbms_random.value) end amount,
case when mod(level,2) = 0 or mod(level,2) = 3 then 'Premium Plus'
     when mod(level,2) = 5 or mod(level,3) = 1 then 'Premium'
     else 'Upper Mass'
end sub_segment,
to_date(round (dbms_random.value (1, 28))
        || '-'
        || round (dbms_random.value (7, 12))
        || '-'
        || round (dbms_random.value (2014, 2014)),
        'DD-MM-YYYY')
transaction_date
from dual tran_type
connect by level <= 20000) transaction_data
group by trunc(transaction_date,'MM'))

pivot(max(payments) as payments for month_ in 
('01-Jul-14' as jul14, '01-Aug-14' as aug14, '01-Sep-14' as sep14,
 '01-Oct-14' as oct14, '01-Nov-14' as nov14, '01-Dec-14' as dec14)))

 unpivot(num_of_pmt for month in ("JUL14_PAYMENTS" as 'Jul-14',
 "AUG14_PAYMENTS" as 'Aug-14',
 "SEP14_PAYMENTS" as 'Sep-14',
 "OCT14_PAYMENTS" as 'Oct-14',
 "NOV14_PAYMENTS" as 'Nov-14',
 "DEC14_PAYMENTS" as 'Dec-14'));