找出不同类型交易的两个总金额之比

时间:2019-03-27 17:25:47

标签: sql oracle

下面的代码给我以下结果

ACCOUNT_KEY EXECUTION_LOCAL_DATE_TIME   ACCT_CURR_AMOUNT    EXECUTION_LOCAL_DATE_TIME_1 ACCT_CURR_AMOUNT_1  SUM_AMOUNT  GROUP_TRANSACTION_TYPE_CD
49EBE0BE    00:00.0 100000  00:00.0 100000  200000  ALL-OUT
49EBE0BE    00:00.0 100000  00:00.0 50000   150000  ALL-INN

SELECT  T1.ACCOUNT_KEY, T1.EXECUTION_LOCAL_DATE_TIME,  
                         T1.ACCT_CURR_AMOUNT,T2.EXECUTION_LOCAL_DATE_TIME,T2.ACCT_CURR_AMOUNT,(T1.ACCT_CURR_AMOUNT + T2.ACCT_CURR_AMOUNT) AS SUM_AMOUNT,TG.Group_Transaction_Type_CD

   FROM CDS_SCHEMA_2.V_TRANSACTIONS T1 
         JOIN CDS_SCHEMA_2.V_TRANSACTIONS T2
            ON T1.ACCOUNT_KEY = T2.ACCOUNT_KEY
        JOIN CDS_SCHEMA_2.SAM_TRANS_CODE_TYPE_RELATION TC
            ON T2.TRANSACTION_CODE_CD = TC.TRANSACTION_CODE_CD
        JOIN CDS_SCHEMA_2.SAM_TRANS_TYPE_GROUP_RELATION TG
            ON  TG.TRANSACTION_TYPE_CD=TC.TRANSACTION_TYPE_CD
   WHERE  T1.execution_local_date_time between to_date('20170807', 'YYYYMMDD') and to_date('20170808', 'YYYYMMDD')
   AND    T2.execution_local_date_time between to_date('20170807', 'YYYYMMDD') and to_date('20170808', 'YYYYMMDD')
   AND    TRUNC(T2.EXECUTION_LOCAL_DATE_TIME) = TRUNC(T1.EXECUTION_LOCAL_DATE_TIME) + 1 
   AND T1.account_key = '49EBE0BE71C97B385960D17C8A85AE0B7B64382F'                                                      
   AND TG.Group_Transaction_Type_CD IN ('ALL-INN','ALL-OUT')

我需要使用上述查询作为子查询或联接来查找ALL-INN的SUM_AMOUNT和ALL-OUT的SUM_AMOUNT(即ALL-INN的SUM_AMOUNT / ALL-OUT的SUM_AMOUNT)之间的比率

0 个答案:

没有答案