Wednesday, 27 September 2017

Trial balance Detail & Expanded STD Query - Oracle EBS R12

https://generalledger-gl.blogspot.com/2017/09/trial-balance-detail-std-query.html

PLSQL Query to Get Trial balance Detail & Expanded

Trial Balance Detail Query

SELECT

 glcc.SEGMENT1 || '-' || glcc.SEGMENT2 || '-' || glcc.SEGMENT3 || '-' ||
 glcc.SEGMENT4 || '-' || glcc.SEGMENT5 || '-' || glcc.SEGMENT6 || '-' ||
 glcc.SEGMENT7 || '-' || glcc.SEGMENT8 Code_Combination,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(BEGIN_BALANCE_DR, 0),
                 
                   'S',
                   NVL(BEGIN_BALANCE_DR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(BEGIN_BALANCE_DR, 0),
                          NVL(BEGIN_BALANCE_DR_BEQ, 0)),
                 
                   'C',
                   NVL(BEGIN_BALANCE_DR_BEQ, 0))),
       
        'PJTD',
        DECODE(&CURRENCY_TYPE, 'T', 0, 'S', 0, 'E', 0, 'C', 0),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_DR, 0)),
                          0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_DR, 0)),
                          0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          DECODE(GLB.PERIOD_NAME,
                                 &FIRST_PERIOD_NAME,
                                 NVL(BEGIN_BALANCE_DR, 0),
                                 0),
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &FIRST_PERIOD_NAME,
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0),
                                 0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_DR_BEQ, 0)),
                          0)))) BEG_BALANCE_DR,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(BEGIN_BALANCE_CR, 0),
                 
                   'S',
                   NVL(BEGIN_BALANCE_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(BEGIN_BALANCE_CR, 0),
                          NVL(BEGIN_BALANCE_CR_BEQ, 0)),
                 
                   'C',
                   NVL(BEGIN_BALANCE_CR_BEQ, 0))),
       
        'PJTD',
        DECODE(&CURRENCY_TYPE, 'T', 0, 'S', 0, 'E', 0, 'C', 0),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_CR, 0)),
                          0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_CR, 0)),
                          0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          DECODE(GLB.PERIOD_NAME,
                                 &FIRST_PERIOD_NAME,
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0),
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &FIRST_PERIOD_NAME,
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_CR_BEQ, 0)),
                          0)))) BEG_BALANCE_CR,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                 
                   'S',
                   NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                          NVL(BEGIN_BALANCE_CR_BEQ, 0)),
                 
                   'C',
                   NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                   NVL(BEGIN_BALANCE_CR_BEQ, 0))),
       
        'PJTD',
        DECODE(&CURRENCY_TYPE, 'T', 0, 'S', 0, 'E', 0, 'C', 0),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0)),
                          0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                         
                          (NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0)),
                          0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          DECODE(GLB.PERIOD_NAME,
                               
                                 &FIRST_PERIOD_NAME,
                                 NVL(BEGIN_BALANCE_DR, 0) -
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0),
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &FIRST_PERIOD_NAME,
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          (NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                          NVL(BEGIN_BALANCE_CR_BEQ, 0)),
                          0)))) BEG_BAL_H,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PERIOD_NET_DR, 0),
                          NVL(PERIOD_NET_DR_BEQ, 0)),
                 
                   'C',
                   NVL(PERIOD_NET_DR_BEQ, 0))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                         
                          NVL(PROJECT_TO_DATE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0)),
                 
                   'C',
                   NVL(PROJECT_TO_DATE_DR_BEQ, 0) + NVL(PERIOD_NET_DR_BEQ, 0))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(PERIOD_NET_DR, 0) + NVL(BEGIN_BALANCE_DR, 0),
                          0) -
                 
                   DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          NVL(BEGIN_BALANCE_DR, 0),
                          0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR, 0)
                         
                          + NVL(BEGIN_BALANCE_DR, 0),
                          0)
                 
                   - DECODE(GLB.PERIOD_NAME,
                            &FIRST_PERIOD_NAME,
                            NVL(BEGIN_BALANCE_DR, 0),
                            0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR, 0) +
                                 NVL(BEGIN_BALANCE_DR, 0),
                                 0)
                         
                          - DECODE(GLB.PERIOD_NAME,
                                   &FIRST_PERIOD_NAME,
                                   NVL(BEGIN_BALANCE_DR, 0),
                                   0),
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR_BEQ, 0) +
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0),
                                 0)
                         
                          - DECODE(GLB.PERIOD_NAME,
                                   &FIRST_PERIOD_NAME,
                                   NVL(BEGIN_BALANCE_DR_BEQ, 0),
                                   0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR_BEQ, 0) +
                          NVL(BEGIN_BALANCE_DR_BEQ, 0),
                          0) - DECODE(GLB.PERIOD_NAME,
                                      &FIRST_PERIOD_NAME,
                                      NVL(BEGIN_BALANCE_DR_BEQ, 0),
                                      0)))) PERIOD_DR,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PERIOD_NET_CR, 0),
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                 
                   'C',
                   NVL(PERIOD_NET_CR_BEQ, 0))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                         
                          NVL(PROJECT_TO_DATE_CR_BEQ, 0) +
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                 
                   'C',
                   NVL(PROJECT_TO_DATE_CR_BEQ, 0) + NVL(PERIOD_NET_CR_BEQ, 0))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(PERIOD_NET_CR, 0) + NVL(BEGIN_BALANCE_CR, 0),
                          0) -
                 
                   DECODE(GLB.PERIOD_NAME,
                          &FIRST_PERIOD_NAME,
                          NVL(BEGIN_BALANCE_CR, 0),
                          0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_CR, 0)
                         
                          + NVL(BEGIN_BALANCE_CR, 0),
                          0)
                 
                   - DECODE(GLB.PERIOD_NAME,
                            &FIRST_PERIOD_NAME,
                            NVL(BEGIN_BALANCE_CR, 0),
                            0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_CR, 0) +
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0)
                         
                          - DECODE(GLB.PERIOD_NAME,
                                   &FIRST_PERIOD_NAME,
                                   NVL(BEGIN_BALANCE_CR, 0),
                                   0),
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_CR_BEQ, 0) +
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0)
                         
                          - DECODE(GLB.PERIOD_NAME,
                                   &FIRST_PERIOD_NAME,
                                   NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                   0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_CR_BEQ, 0) +
                          NVL(BEGIN_BALANCE_CR_BEQ, 0),
                          0) - DECODE(GLB.PERIOD_NAME,
                                      &FIRST_PERIOD_NAME,
                                      NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                      0)))) PERIOD_CR,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0),
                         
                          NVL(PERIOD_NET_DR_BEQ, 0) -
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                 
                   'C',
                   NVL(PERIOD_NET_DR_BEQ, 0) - NVL(PERIOD_NET_CR_BEQ, 0))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0)
                 
                   - NVL(PROJECT_TO_DATE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0)
                 
                   - NVL(PROJECT_TO_DATE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0)
                         
                          - NVL(PROJECT_TO_DATE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                         
                          NVL(PROJECT_TO_DATE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0)
                         
                          - NVL(PROJECT_TO_DATE_CR_BEQ, 0) -
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                 
                   'C',
                   NVL(PROJECT_TO_DATE_DR_BEQ, 0) + NVL(PERIOD_NET_DR_BEQ, 0) -
                   NVL(PROJECT_TO_DATE_CR_BEQ, 0)
                 
                   - NVL(PERIOD_NET_CR_BEQ, 0))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0) +
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          0)
                 
                   - DECODE(GLB.PERIOD_NAME,
                            &FIRST_PERIOD_NAME,
                            NVL(BEGIN_BALANCE_DR, 0) -
                            NVL(BEGIN_BALANCE_CR, 0),
                            0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0)
                         
                          + NVL(BEGIN_BALANCE_DR, 0) -
                          NVL(BEGIN_BALANCE_CR, 0),
                          0)
                 
                   - DECODE(GLB.PERIOD_NAME,
                            &FIRST_PERIOD_NAME,
                            NVL(BEGIN_BALANCE_DR, 0) -
                            NVL(BEGIN_BALANCE_CR, 0),
                            0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR, 0)
                               
                                 - NVL(PERIOD_NET_CR, 0) +
                                 NVL(BEGIN_BALANCE_DR, 0) -
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0)
                         
                          - DECODE(GLB.PERIOD_NAME,
                                   &FIRST_PERIOD_NAME,
                                   NVL(BEGIN_BALANCE_DR, 0) -
                                   NVL(BEGIN_BALANCE_CR, 0),
                                   0),
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR_BEQ, 0) -
                                 NVL(PERIOD_NET_CR_BEQ, 0)
                               
                                 + NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0) - DECODE(GLB.PERIOD_NAME,
                                             &FIRST_PERIOD_NAME,
                                             NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                                             NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                             0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR_BEQ, 0) -
                          NVL(PERIOD_NET_CR_BEQ, 0) +
                          NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                          NVL(BEGIN_BALANCE_CR_BEQ, 0),
                          0) - DECODE(GLB.PERIOD_NAME,
                                      &FIRST_PERIOD_NAME,
                                      NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                                      NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                      0)))) ACTIVITY_H,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(BEGIN_BALANCE_DR, 0) +
                         
                          NVL(PERIOD_NET_DR, 0),
                          NVL(BEGIN_BALANCE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0)),
                 
                   'C',
                   NVL(BEGIN_BALANCE_DR_BEQ, 0) + NVL(PERIOD_NET_DR_BEQ, 0))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PROJECT_TO_DATE_DR, 0) +
                         
                          NVL(PERIOD_NET_DR, 0),
                          NVL(PROJECT_TO_DATE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0)),
                 
                   'C',
                   NVL(PROJECT_TO_DATE_DR_BEQ, 0) + NVL(PERIOD_NET_DR_BEQ, 0))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR, 0) + NVL(BEGIN_BALANCE_DR, 0),
                          0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR, 0) + NVL(BEGIN_BALANCE_DR, 0),
                          0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR, 0) +
                               
                                 NVL(BEGIN_BALANCE_DR, 0),
                                 0),
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR_BEQ, 0) +
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0),
                                 0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR_BEQ, 0) +
                          NVL(BEGIN_BALANCE_DR_BEQ, 0),
                          0)))) END_BALANCE_DR,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(BEGIN_BALANCE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(BEGIN_BALANCE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(BEGIN_BALANCE_CR, 0) +
                         
                          NVL(PERIOD_NET_CR, 0),
                          NVL(BEGIN_BALANCE_CR_BEQ, 0) +
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                 
                   'C',
                   NVL(BEGIN_BALANCE_CR_BEQ, 0) + NVL(PERIOD_NET_CR_BEQ, 0))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PROJECT_TO_DATE_CR, 0) +
                         
                          NVL(PERIOD_NET_CR, 0),
                          NVL(PROJECT_TO_DATE_CR_BEQ, 0) +
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                 
                   'C',
                   NVL(PROJECT_TO_DATE_CR_BEQ, 0) + NVL(PERIOD_NET_CR_BEQ, 0))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_CR, 0) + NVL(BEGIN_BALANCE_CR, 0),
                          0)),
             
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_CR, 0) + NVL(BEGIN_BALANCE_CR, 0),
                          0)),
             
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                         
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_CR, 0) +
                               
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0),
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_CR_BEQ, 0) +
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0))),
             
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_CR_BEQ, 0) +
                          NVL(BEGIN_BALANCE_CR_BEQ, 0),
                          0)))) END_BALANCE_CR,

 DECODE(&AMOUNT_TYPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0) -
                   NVL(BEGIN_BALANCE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                   'S',
                   NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0) -
                   NVL(BEGIN_BALANCE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0) -
                          NVL(BEGIN_BALANCE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                          NVL(BEGIN_BALANCE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0) -
                          NVL(BEGIN_BALANCE_CR_BEQ, 0) -
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                   'C',
                   NVL(BEGIN_BALANCE_DR_BEQ, 0) + NVL(PERIOD_NET_DR_BEQ, 0) -
                   NVL(BEGIN_BALANCE_CR_BEQ, 0) - NVL(PERIOD_NET_CR_BEQ, 0))),
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0) -
                   NVL(PROJECT_TO_DATE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                   'S',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0) -
                   NVL(PROJECT_TO_DATE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                   'E',
                   DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0) -
                          NVL(PROJECT_TO_DATE_CR, 0) - NVL(PERIOD_NET_CR, 0),
                          NVL(PROJECT_TO_DATE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0) -
                          NVL(PROJECT_TO_DATE_CR_BEQ, 0) -
                          NVL(PERIOD_NET_CR_BEQ, 0)),
                   'C',
                   NVL(PROJECT_TO_DATE_DR_BEQ, 0) + NVL(PERIOD_NET_DR_BEQ, 0) -
                   NVL(PROJECT_TO_DATE_CR_BEQ, 0) - NVL(PERIOD_NET_CR_BEQ, 0))),
        'YTD',
        DECODE(&CURRENCY_TYPE,
               'T',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0) +
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          0)),
               'S',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0) +
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          0)),
               'E',
               SUM(DECODE(GLB.TRANSLATED_FLAG,
                          'R',
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR, 0) - NVL(PERIOD_NET_CR, 0) +
                                 NVL(BEGIN_BALANCE_DR, 0) -
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0),
                          DECODE(GLB.PERIOD_NAME,
                                 &PERIOD_NAME,
                                 NVL(PERIOD_NET_DR_BEQ, 0) -
                                 NVL(PERIOD_NET_CR_BEQ, 0) +
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0))),
               'C',
               SUM(DECODE(GLB.PERIOD_NAME,
                          &PERIOD_NAME,
                          NVL(PERIOD_NET_DR_BEQ, 0) -
                          NVL(PERIOD_NET_CR_BEQ, 0) +
                          NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                          NVL(BEGIN_BALANCE_CR_BEQ, 0),
                          0)))) END_BAL_H

  FROM GL_BALANCES GLB,
     
       GL_CODE_COMBINATIONS GLCC,
     
       GL_LEDGERS GLL,
     
       GL_LEDGER_SET_ASSIGNMENTS ASG,
     
       GL_LEDGER_RELATIONSHIPS LR

 WHERE

 GLB.ACTUAL_FLAG = 'A'

 AND GLB.CURRENCY_CODE = &ENTERED_CURRENCY

 AND GLB.PERIOD_NAME IN (&PERIOD_NAME,
                     DECODE(&AMOUNT_TYPE,
                            'PTD',
                            &PERIOD_NAME,
                           
                            'PJTD',
                            &PERIOD_NAME,
                           
                            'YTD',
                            &FIRST_PERIOD_NAME))

 AND GLB.CODE_COMBINATION_ID = GLCC.CODE_COMBINATION_ID

--AND GLCC.CHART_OF_ACCOUNTS_ID = &STRUCT_NUM

 AND GLCC.SUMMARY_FLAG = 'N'

 AND GLCC.TEMPLATE_ID IS NULL

 AND GLL.LEDGER_ID = &LEDGER_ID

 AND ASG.LEDGER_SET_ID(+) = GLL.LEDGER_ID

 AND LR.TARGET_LEDGER_ID = NVL(ASG.LEDGER_ID, GLL.LEDGER_ID)

 AND LR.SOURCE_LEDGER_ID = NVL(ASG.LEDGER_ID, GLL.LEDGER_ID)

 AND LR.TARGET_CURRENCY_CODE = &LEDGER_CURRENCY

 AND LR.SOURCE_LEDGER_ID = GLB.LEDGER_ID

 AND LR.TARGET_LEDGER_ID = GLB.LEDGER_ID

 group by glcc.SEGMENT1,
          glcc.SEGMENT2,
          glcc.SEGMENT3,
          glcc.SEGMENT4,
          glcc.SEGMENT5,
          glcc.SEGMENT6,
          glcc.SEGMENT7,
          glcc.SEGMENT8

 order by glcc.SEGMENT4

#############################################################################

Trial Balance Expanded STD Query

select

/*cc.SEGMENT1, cc.SEGMENT2, cc.SEGMENT3,*/
 cc.SEGMENT4, /*cc.SEGMENT5, cc.SEGMENT6, cc.SEGMENT7, cc.SEGMENT8,*/

 decode(&CURRENCY_TYPE,
       
        'T',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               1,
             
               sum(decode(bl.period_name,
                          &FIRST_PERIOD_NAME,
                          nvl(bl.begin_balance_dr, 0),
                          0))
             
               - sum(decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0)),
       
        'S',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               1,
             
               sum(decode(bl.period_name,
                          &FIRST_PERIOD_NAME,
                          nvl(bl.begin_balance_dr, 0),
                          0))
             
               - sum(decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0)),
       
        'E',
        sum(decode(bl.translated_flag,
                 
                   'R',
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr, 0),
                                        0)),
                         
                          1,
                         
                          decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr, 0),
                                 0)
                         
                          - decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr, 0),
                                   0),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_dr, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_dr, 0),
                            0),
                 
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr_beq, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr_beq, 0),
                                        0)),
                         
                          1,
                         
                          decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr_beq, 0),
                                 0)
                         
                          - decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr_beq, 0),
                                   0),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_dr_beq, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_dr_beq, 0),
                            0))))

 BEGIN_DR,

 decode(&CURRENCY_TYPE,
       
        'T',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               -1,
             
               abs(sum(decode(bl.period_name,
                              &FIRST_PERIOD_NAME,
                              nvl(bl.begin_balance_dr, 0),
                              0))
                 
                   - sum(decode(bl.period_name,
                                &FIRST_PERIOD_NAME,
                                nvl(bl.begin_balance_cr, 0),
                                0))),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0)),
       
        'S',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               -1,
             
               abs(sum(decode(bl.period_name,
                              &FIRST_PERIOD_NAME,
                              nvl(bl.begin_balance_dr, 0),
                              0))
                 
                   - sum(decode(bl.period_name,
                                &FIRST_PERIOD_NAME,
                                nvl(bl.begin_balance_cr, 0),
                                0))),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0)),
       
        'E',
        sum(decode(bl.translated_flag,
                 
                   'R',
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr, 0),
                                        0)),
                         
                          -1,
                         
                          abs(decode(bl.period_name,
                                     &FIRST_PERIOD_NAME,
                                     nvl(bl.begin_balance_dr, 0),
                                     0)
                             
                              - decode(bl.period_name,
                                       &FIRST_PERIOD_NAME,
                                       nvl(bl.begin_balance_cr, 0),
                                       0)),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0),
                 
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr_beq, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr_beq, 0),
                                        0)),
                         
                          -1,
                         
                          abs(decode(bl.period_name,
                                     &FIRST_PERIOD_NAME,
                                     nvl(bl.begin_balance_dr_beq, 0),
                                     0)
                             
                              - decode(bl.period_name,
                                       &FIRST_PERIOD_NAME,
                                       nvl(bl.begin_balance_cr_beq, 0),
                                       0)),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_cr_beq, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr_beq, 0),
                            0))))

 BEGIN_CR,

 decode(&CURRENCY_TYPE,
       
        'T',
        sum(decode(bl.period_name, &PERIOD_NAME, nvl(bl.period_net_dr, 0), 0)),
       
        'S',
        sum(decode(bl.period_name, &PERIOD_NAME, nvl(bl.period_net_dr, 0), 0)),
       
        'E',
        sum(decode(bl.translated_flag,
                 
                   'R',
                   decode(bl.period_name,
                          &PERIOD_NAME,
                          nvl(bl.period_net_dr, 0),
                          0),
                 
                   decode(bl.period_name,
                          &PERIOD_NAME,
                          nvl(bl.period_net_dr_beq, 0),
                          0))))

 PER_DR,

 decode(&CURRENCY_TYPE,
       
        'T',
        sum(decode(bl.period_name, &PERIOD_NAME, nvl(bl.period_net_cr, 0), 0)),
       
        'S',
        sum(decode(bl.period_name, &PERIOD_NAME, nvl(bl.period_net_cr, 0), 0)),
       
        'E',
        sum(decode(bl.translated_flag,
                 
                   'R',
                   decode(bl.period_name,
                          &PERIOD_NAME,
                          nvl(bl.period_net_cr, 0),
                          0),
                 
                   decode(bl.period_name,
                          &PERIOD_NAME,
                          nvl(bl.period_net_cr_beq, 0),
                          0))))

 PER_CR,

 decode(&CURRENCY_TYPE,
       
        'T',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               1,
             
               sum(decode(bl.period_name,
                          &FIRST_PERIOD_NAME,
                          nvl(bl.begin_balance_dr, 0),
                          0))
             
               - sum(decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0)),
       
        'S',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               1,
             
               sum(decode(bl.period_name,
                          &FIRST_PERIOD_NAME,
                          nvl(bl.begin_balance_dr, 0),
                          0))
             
               - sum(decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0)),
       
        'E',
        sum(decode(bl.translated_flag,
                 
                   'R',
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr, 0),
                                        0)),
                         
                          1,
                         
                          decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr, 0),
                                 0)
                         
                          - decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr, 0),
                                   0),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_dr, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_dr, 0),
                            0),
                 
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr_beq, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr_beq, 0),
                                        0)),
                         
                          1,
                         
                          decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr_beq, 0),
                                 0)
                         
                          - decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr_beq, 0),
                                   0),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_dr_beq, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_dr_beq, 0),
                            0))))

 + decode(&CURRENCY_TYPE,
         
          'T',
          sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.period_net_dr, 0),
                     0)),
         
          'S',
          sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.period_net_dr, 0),
                     0)),
         
          'E',
          sum(decode(bl.translated_flag,
                   
                     'R',
                     decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.period_net_dr, 0),
                            0),
                   
                     decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.period_net_dr_beq, 0),
                            0))))

 END_DR,

 decode(&CURRENCY_TYPE,
       
        'T',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               -1,
             
               abs(sum(decode(bl.period_name,
                              &FIRST_PERIOD_NAME,
                              nvl(bl.begin_balance_dr, 0),
                              0))
                 
                   - sum(decode(bl.period_name,
                                &FIRST_PERIOD_NAME,
                                nvl(bl.begin_balance_cr, 0),
                                0))),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0)),
       
        'S',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               -1,
             
               abs(sum(decode(bl.period_name,
                              &FIRST_PERIOD_NAME,
                              nvl(bl.begin_balance_dr, 0),
                              0))
                 
                   - sum(decode(bl.period_name,
                                &FIRST_PERIOD_NAME,
                                nvl(bl.begin_balance_cr, 0),
                                0))),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_cr, 0),
                     0)),
       
        'E',
        sum(decode(bl.translated_flag,
                 
                   'R',
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr, 0),
                                        0)),
                         
                          -1,
                         
                          abs(decode(bl.period_name,
                                     &FIRST_PERIOD_NAME,
                                     nvl(bl.begin_balance_dr, 0),
                                     0)
                             
                              - decode(bl.period_name,
                                       &FIRST_PERIOD_NAME,
                                       nvl(bl.begin_balance_cr, 0),
                                       0)),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0),
                 
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr_beq, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr_beq, 0),
                                        0)),
                         
                          -1,
                         
                          abs(decode(bl.period_name,
                                     &FIRST_PERIOD_NAME,
                                     nvl(bl.begin_balance_dr_beq, 0),
                                     0)
                             
                              - decode(bl.period_name,
                                       &FIRST_PERIOD_NAME,
                                       nvl(bl.begin_balance_cr_beq, 0),
                                       0)),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_cr_beq, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr_beq, 0),
                            0))))

 + decode(&CURRENCY_TYPE,
         
          'T',
          sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.period_net_cr, 0),
                     0)),
         
          'S',
          sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.period_net_cr, 0),
                     0)),
         
          'E',
          sum(decode(bl.translated_flag,
                   
                     'R',
                     decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.period_net_cr, 0),
                            0),
                   
                     decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.period_net_cr_beq, 0),
                            0))))

 END_CR,

 decode(&CURRENCY_TYPE,
       
        'T',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               1,
             
               sum(decode(bl.period_name,
                          &FIRST_PERIOD_NAME,
                          nvl(bl.begin_balance_dr, 0),
                          0))
             
               - sum(decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0)),
       
        'S',
        decode(sign(sum(decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_dr, 0),
                               0))
                   
                    - sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_cr, 0),
                                 0))),
             
               1,
             
               sum(decode(bl.period_name,
                          &FIRST_PERIOD_NAME,
                          nvl(bl.begin_balance_dr, 0),
                          0))
             
               - sum(decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_cr, 0),
                            0)),
               0)
       
        + sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0))
       
        - sum(decode(bl.period_name,
                     &FIRST_PERIOD_NAME,
                     nvl(bl.begin_balance_dr, 0),
                     0)),
       
        'E',
        sum(decode(bl.translated_flag,
                 
                   'R',
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr, 0),
                                        0)),
                         
                          1,
                         
                          decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr, 0),
                                 0)
                         
                          - decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr, 0),
                                   0),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_dr, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_dr, 0),
                            0),
                 
                   decode(sign(decode(bl.period_name,
                                      &FIRST_PERIOD_NAME,
                                      nvl(bl.begin_balance_dr_beq, 0),
                                      0)
                             
                               - decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_cr_beq, 0),
                                        0)),
                         
                          1,
                         
                          decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr_beq, 0),
                                 0)
                         
                          - decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr_beq, 0),
                                   0),
                          0)
                 
                   + decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.begin_balance_dr_beq, 0),
                            0)
                 
                   - decode(bl.period_name,
                            &FIRST_PERIOD_NAME,
                            nvl(bl.begin_balance_dr_beq, 0),
                            0))))

 + decode(&CURRENCY_TYPE,
         
          'T',
          sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.period_net_dr, 0),
                     0)),
         
          'S',
          sum(decode(bl.period_name,
                     &PERIOD_NAME,
                     nvl(bl.period_net_dr, 0),
                     0)),
         
          'E',
          sum(decode(bl.translated_flag,
                   
                     'R',
                     decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.period_net_dr, 0),
                            0),
                   
                     decode(bl.period_name,
                            &PERIOD_NAME,
                            nvl(bl.period_net_dr_beq, 0),
                            0))))

 - (decode(&CURRENCY_TYPE,
         
           'T',
           decode(sign(sum(decode(bl.period_name,
                                  &FIRST_PERIOD_NAME,
                                  nvl(bl.begin_balance_dr, 0),
                                  0))
                     
                       - sum(decode(bl.period_name,
                                    &FIRST_PERIOD_NAME,
                                    nvl(bl.begin_balance_cr, 0),
                                    0))),
                 
                  -1,
                 
                  abs(sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr, 0),
                                 0))
                     
                      - sum(decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr, 0),
                                   0))),
                  0)
         
           + sum(decode(bl.period_name,
                        &PERIOD_NAME,
                        nvl(bl.begin_balance_cr, 0),
                        0))
         
           - sum(decode(bl.period_name,
                        &FIRST_PERIOD_NAME,
                        nvl(bl.begin_balance_cr, 0),
                        0)),
         
           'S',
           decode(sign(sum(decode(bl.period_name,
                                  &FIRST_PERIOD_NAME,
                                  nvl(bl.begin_balance_dr, 0),
                                  0))
                     
                       - sum(decode(bl.period_name,
                                    &FIRST_PERIOD_NAME,
                                    nvl(bl.begin_balance_cr, 0),
                                    0))),
                 
                  -1,
                 
                  abs(sum(decode(bl.period_name,
                                 &FIRST_PERIOD_NAME,
                                 nvl(bl.begin_balance_dr, 0),
                                 0))
                     
                      - sum(decode(bl.period_name,
                                   &FIRST_PERIOD_NAME,
                                   nvl(bl.begin_balance_cr, 0),
                                   0))),
                  0)
         
           + sum(decode(bl.period_name,
                        &PERIOD_NAME,
                        nvl(bl.begin_balance_cr, 0),
                        0))
         
           - sum(decode(bl.period_name,
                        &FIRST_PERIOD_NAME,
                        nvl(bl.begin_balance_cr, 0),
                        0)),
         
           'E',
           sum(decode(bl.translated_flag,
                     
                      'R',
                      decode(sign(decode(bl.period_name,
                                         &FIRST_PERIOD_NAME,
                                         nvl(bl.begin_balance_dr, 0),
                                         0)
                                 
                                  - decode(bl.period_name,
                                           &FIRST_PERIOD_NAME,
                                           nvl(bl.begin_balance_cr, 0),
                                           0)),
                           
                             -1,
                           
                             abs(decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_dr, 0),
                                        0)
                               
                                 - decode(bl.period_name,
                                          &FIRST_PERIOD_NAME,
                                          nvl(bl.begin_balance_cr, 0),
                                          0)),
                             0)
                     
                      + decode(bl.period_name,
                               &PERIOD_NAME,
                               nvl(bl.begin_balance_cr, 0),
                               0)
                     
                      - decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_cr, 0),
                               0),
                     
                      decode(sign(decode(bl.period_name,
                                         &FIRST_PERIOD_NAME,
                                         nvl(bl.begin_balance_dr_beq, 0),
                                         0)
                                 
                                  - decode(bl.period_name,
                                           &FIRST_PERIOD_NAME,
                                           nvl(bl.begin_balance_cr_beq, 0),
                                           0)),
                           
                             -1,
                           
                             abs(decode(bl.period_name,
                                        &FIRST_PERIOD_NAME,
                                        nvl(bl.begin_balance_dr_beq, 0),
                                        0)
                               
                                 - decode(bl.period_name,
                                          &FIRST_PERIOD_NAME,
                                          nvl(bl.begin_balance_cr_beq, 0),
                                          0)),
                             0)
                     
                      + decode(bl.period_name,
                               &PERIOD_NAME,
                               nvl(bl.begin_balance_cr_beq, 0),
                               0)
                     
                      - decode(bl.period_name,
                               &FIRST_PERIOD_NAME,
                               nvl(bl.begin_balance_cr_beq, 0),
                               0))))
   
    + decode(&CURRENCY_TYPE,
           
             'T',
             sum(decode(bl.period_name,
                        &PERIOD_NAME,
                        nvl(bl.period_net_cr, 0),
                        0)),
           
             'S',
             sum(decode(bl.period_name,
                        &PERIOD_NAME,
                        nvl(bl.period_net_cr, 0),
                        0)),
           
             'E',
             sum(decode(bl.translated_flag,
                       
                        'R',
                        decode(bl.period_name,
                               &PERIOD_NAME,
                               nvl(bl.period_net_cr, 0),
                               0),
                       
                        decode(bl.period_name,
                               &PERIOD_NAME,
                               nvl(bl.period_net_cr_beq, 0),
                               0)))))

 NET,

 max(LR.TARGET_LEDGER_name) LEDGER_NAME_H

  FROM gl_balances BL,
     
       gl_code_combinations CC,
     
       GL_LEDGERS GLL,
     
       GL_LEDGER_SET_ASSIGNMENTS ASG,
     
       GL_LEDGER_RELATIONSHIPS LR

 WHERE bl.currency_code = &ENTERED_CURRENCY
     
   AND bl.period_name in (&FIRST_PERIOD_NAME, &PERIOD_NAME)
     
   AND bl.actual_flag = 'A'
     
   AND cc.template_id is null
     
   AND CC.SUMMARY_FLAG = 'N'
     
      --AND cc.chart_of_accounts_id = &STRUCT_NUM
     
   AND cc.code_combination_id = bl.code_combination_id
     
   AND GLL.LEDGER_ID = &LEDGER_ID
     
   AND ASG.LEDGER_SET_ID(+) = GLL.LEDGER_ID
     
   AND LR.TARGET_LEDGER_ID = NVL(ASG.LEDGER_ID, GLL.LEDGER_ID)
     
   AND LR.SOURCE_LEDGER_ID = NVL(ASG.LEDGER_ID, GLL.LEDGER_ID)
     
   AND LR.TARGET_CURRENCY_CODE = &LEDGER_CURRENCY
     
   AND LR.SOURCE_LEDGER_ID = BL.LEDGER_ID
     
   AND LR.TARGET_LEDGER_ID = BL.LEDGER_ID
     
   and cc.SEGMENT4 between NVL(&FROM_ACCOUNT, cc.SEGMENT4) and
       NVL(&TO_ACCOUNT, cc.SEGMENT4)

 group by /*cc.SEGMENT1, cc.SEGMENT2, cc.SEGMENT3,*/ cc.SEGMENT4 /*, cc.SEGMENT5, cc.SEGMENT6, cc.SEGMENT7, cc.SEGMENT8*/

 order by cc.SEGMENT4

GL_CODE_COMBINATIONS Query - Oracle EBS R12

https://generalledger-gl.blogspot.com/2017/09/glcodecombinations-query.htm

PLSQL Query to Get GL_CODE_COMBINATIONS

select

 gcc.SEGMENT1 || '-' || gcc.SEGMENT2 || '-' || gcc.SEGMENT3 || '-' ||
 gcc.SEGMENT4 || '-' || gcc.SEGMENT5 || '-' || gcc.SEGMENT6 || '-' ||
 gcc.SEGMENT7 || '-' || gcc.SEGMENT8 Code_Combination,

 decode(gcc.detail_posting_allowed_flag, 'Y', '*', '') POSTING_ALLOWED,

 decode(gcc.detail_budgeting_allowed_flag,
       
        'Y',
        '*',
        '') BUDGET_ALLOWED,

 decode(gcc.summary_flag,
       
        'Y',
        ' ',
       
        gl1.description) ACCOUNT_TYPE,

 gcc.summary_flag SUMMARY_FLAG,

 decode(gcc.summary_flag, 'Y', '*', '') SUMMARY,

 gcc.enabled_flag ENABLED_FLAG,

 decode(gcc.enabled_flag, 'Y', '*', '') ENABLED,

 gcc.start_date_active START_DATE,

 gcc.end_date_active END_DATE

  from gl_lookups gl1,
     
       gl_code_combinations gcc

 where

--gcc.chart_of_accounts_id = &P_STRUCT_NUM

 gcc.account_type = gl1.lookup_code

 and gl1.lookup_type = 'ACCOUNT TYPE'

 order by gcc.SEGMENT4

Trial Balance Query - Oracle EBS R12

https://generalledger-gl.blogspot.com/2017/09/trial-balance-query-std.html

PLSQL Query to Get Trial Balance

select

 cc.SEGMENT4 ACCOUNTS,

 decode(&AMOUNT_YPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                 
                   'S',
                   NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                         
                          NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                          NVL(BEGIN_BALANCE_CR_BEQ, 0)))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   0,
                 
                   'S',
                   0,
                 
                   'E',
                   0)),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
             
               'T',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &FIRST_PERIOD,
                         
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          0)),
             
               'S',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &FIRST_PERIOD,
                         
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          0)),
             
               'E',
               SUM(DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          DECODE(BAL.PERIOD_NAME,
                                 &FIRST_PERIOD,
                               
                                 NVL(BEGIN_BALANCE_DR, 0) -
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0),
                         
                          DECODE(BAL.PERIOD_NAME,
                                 &FIRST_PERIOD,
                               
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0)))))

 BEGIN_BALANCE,

 DECODE(&AMOUNT_YPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PERIOD_NET_DR, 0),
                         
                          NVL(PERIOD_NET_DR_BEQ, 0)))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                         
                          NVL(PROJECT_TO_DATE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0)))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
             
               'T',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_DR, 0),
                            0)),
             
               'S',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_DR, 0),
                            0)),
             
               'E',
               SUM(DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_DR, 0) +
                                 NVL(PERIOD_NET_DR, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_DR, 0),
                                   0),
                         
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0) +
                                 NVL(PERIOD_NET_DR_BEQ, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_DR_BEQ, 0),
                                   0)))))

 PERIOD_DR,

 DECODE(&AMOUNT_YPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PERIOD_NET_CR, 0),
                         
                          NVL(PERIOD_NET_CR_BEQ, 0)))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                         
                          NVL(PROJECT_TO_DATE_CR_BEQ, 0) +
                          NVL(PERIOD_NET_CR_BEQ, 0)))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
             
               'T',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_CR, 0),
                            0)),
             
               'S',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_CR, 0),
                            0)),
             
               'E',
               SUM(DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_CR, 0) +
                                 NVL(PERIOD_NET_CR, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_CR, 0),
                                   0),
                         
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0) +
                                 NVL(PERIOD_NET_CR_BEQ, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                   0)))))

 PERIOD_CR

,

 decode(&AMOUNT_YPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                 
                   'S',
                   NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                         
                          NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                          NVL(BEGIN_BALANCE_CR_BEQ, 0)))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                   'T',
                   0,
                 
                   'S',
                   0,
                 
                   'E',
                   0)),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
             
               'T',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &FIRST_PERIOD,
                         
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          0)),
             
               'S',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &FIRST_PERIOD,
                         
                          NVL(BEGIN_BALANCE_DR, 0) - NVL(BEGIN_BALANCE_CR, 0),
                          0)),
             
               'E',
               SUM(DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          DECODE(BAL.PERIOD_NAME,
                                 &FIRST_PERIOD,
                               
                                 NVL(BEGIN_BALANCE_DR, 0) -
                                 NVL(BEGIN_BALANCE_CR, 0),
                                 0),
                         
                          DECODE(BAL.PERIOD_NAME,
                                 &FIRST_PERIOD,
                               
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0) -
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                 0)))))

 +

 DECODE(&AMOUNT_YPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PERIOD_NET_DR, 0),
                         
                          NVL(PERIOD_NET_DR_BEQ, 0)))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PROJECT_TO_DATE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                         
                          NVL(PROJECT_TO_DATE_DR_BEQ, 0) +
                          NVL(PERIOD_NET_DR_BEQ, 0)))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
             
               'T',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_DR, 0),
                            0)),
             
               'S',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_DR, 0) + NVL(PERIOD_NET_DR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_DR, 0),
                            0)),
             
               'E',
               SUM(DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_DR, 0) +
                                 NVL(PERIOD_NET_DR, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_DR, 0),
                                   0),
                         
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_DR_BEQ, 0) +
                                 NVL(PERIOD_NET_DR_BEQ, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_DR_BEQ, 0),
                                   0)))))

 -

 DECODE(&AMOUNT_YPE,
        'PTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PERIOD_NET_CR, 0),
                         
                          NVL(PERIOD_NET_CR_BEQ, 0)))),
       
        'PJTD',
        SUM(DECODE(&CURRENCY_TYPE,
                 
                   'T',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'S',
                   NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                 
                   'E',
                   DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          NVL(PROJECT_TO_DATE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                         
                          NVL(PROJECT_TO_DATE_CR_BEQ, 0) +
                          NVL(PERIOD_NET_CR_BEQ, 0)))),
       
        'YTD',
        DECODE(&CURRENCY_TYPE,
             
               'T',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_CR, 0),
                            0)),
             
               'S',
               SUM(DECODE(BAL.PERIOD_NAME,
                          &PERIOD_NAME,
                         
                          NVL(BEGIN_BALANCE_CR, 0) + NVL(PERIOD_NET_CR, 0),
                          0)
                 
                   - DECODE(BAL.PERIOD_NAME,
                            &FIRST_PERIOD,
                            NVL(BEGIN_BALANCE_CR, 0),
                            0)),
             
               'E',
               SUM(DECODE(BAL.TRANSLATED_FLAG,
                         
                          'R',
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_CR, 0) +
                                 NVL(PERIOD_NET_CR, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_CR, 0),
                                   0),
                         
                          DECODE(BAL.PERIOD_NAME,
                                 &PERIOD_NAME,
                               
                                 NVL(BEGIN_BALANCE_CR_BEQ, 0) +
                                 NVL(PERIOD_NET_CR_BEQ, 0),
                                 0)
                         
                          - DECODE(BAL.PERIOD_NAME,
                                   &FIRST_PERIOD,
                                 
                                   NVL(BEGIN_BALANCE_CR_BEQ, 0),
                                   0)))))

 ENDING_BALANCE

  FROM GL_BALANCES BAL,
     
       GL_CODE_COMBINATIONS CC,
     
       GL_LEDGERS L,
     
       GL_LEDGER_SET_ASSIGNMENTS ASG,
     
       GL_LEDGER_RELATIONSHIPS LR

 WHERE

 BAL.ACTUAL_FLAG = 'A'

 AND BAL.CURRENCY_CODE = &ENTERED_CURRENCY

 AND BAL.PERIOD_NAME IN (&PERIOD_NAME,
                   
                     DECODE(&AMOUNT_YPE,
                            'PTD',
                            &PERIOD_NAME,
                           
                            'PJTD',
                            &PERIOD_NAME,
                           
                            'YTD',
                            &FIRST_PERIOD))

 AND BAL.CODE_COMBINATION_ID = CC.CODE_COMBINATION_ID

-- AND CC.CHART_OF_ACCOUNTS_ID = &STRUCT_NUM

 AND CC.TEMPLATE_ID IS NULL

 AND CC.SUMMARY_FLAG = 'N'

 AND L.LEDGER_ID = &LEDGER_ID

 AND ASG.LEDGER_SET_ID(+) = L.LEDGER_ID

 AND LR.TARGET_LEDGER_ID = NVL(ASG.LEDGER_ID, L.LEDGER_ID)

 AND LR.SOURCE_LEDGER_ID = NVL(ASG.LEDGER_ID, L.LEDGER_ID)

 AND LR.TARGET_CURRENCY_CODE = &LEDGER_CURRENCY

 AND LR.SOURCE_LEDGER_ID = BAL.LEDGER_ID

 AND LR.TARGET_LEDGER_ID = BAL.LEDGER_ID

 group by cc.SEGMENT4

 order by cc.SEGMENT4

Wednesday, 13 September 2017

How Revaluation is Calculated and Created - Oracle EBS R12

https://generalledger-gl.blogspot.com/2017/09/how-revaluation-is-calculated-and.html

Revaluation:

Revaluation is a Process of Revaluing Balances that have Transactions dominated in Foreign Currency. It reflects change in Conversion Rate between the Date of Transaction and the Current Market rate of each Currency.

§Income Statement Accounts:
§Are revalued on the basis of the PTD or YTD balances, in accordance with the Income Statement Rule profile option.

§Balance Sheet Accounts:
§Are always revalued on the basis of their YTD balances.

REVALUATION FORMULA


YTD: ACCOUNT AMOUNT = ((begin_balance_dr + period_net_dr - begin_balance_cr - period_net_cr) * revaluation_rate)) LESS (begin_balance_dr_beq + period_net_dr_beq - begin_balance_cr_beq - period_net_cr_beq)

PTD: ACCOUNT AMOUNT = ((period_net_dr - period_net_cr) * revaluation_rate)) LESS (period_net_dr_beq - period_net_cr_beq)

--------------------------------------------------------------------------------------------------------------------

Lets do Revaluation on only one Account


04-001-000-114121100-000-00-0000-0000














--------------------------------------------------------------------------------------------------------------------------

The easy way of Calculating Revaluation on Particular Account is, take YTD entered and YTD Accounted Balances for the Month on which Revaluation is to be generated. The Exchange Rate which we give on Revaluation screen, System simply Multiply the ENTERED balance of that Account for the Month with Exchange Rate and subtract it from Accounted Balance of that Month.

The Account on which Revaluation is generated 

For Liability Accounts:


If Liability Account is Debit then Unrealized gain. (Here Revalue Amount is Positive)
Liability Account
DR

Unrealized Gain

CR

If Liability Account is Credit then Unrealized Loss. (Here Revalue Amount is Negative)
Unrealized Loss
DR

Liability Account

CR

For Receivables Accounts:

If Receivables Account is Credit then Unrealized Loss. (Here Revalue Amount is Positive)
Unrealized Loss
DR

Receivables Account

CR

If Receivables Account is Debit then Unrealized Gain. (Here Revalue Amount is Negative)
Receivables Account
DR

Unrealized Gain

CR

Unrealized Gain/Loss Account we provide on Revaluation Screen.

Tuesday, 8 August 2017

PLSQL Qery to Check wrong Account Natures - Oracle EBS R12

https://generalledger-gl.blogspot.com/2017/08/plsql-query-to-check-wrong-account.html

PLSQL Query to Check wrong Account Natures in Oracle EBS

select a.FLEX_VALUE,
     
       a.DESCRIPTION,
     
       a.ENABLED_FLAG,
     
       a.END_DATE_ACTIVE,
     
       decode(substr(a.COMPILED_VALUE_ATTRIBUTES, 5, 1),
              'E',
              'EXPENSE',
              'L',
              'LIABILITY',
              'O',
              'Ownership/Stockholder''s Equity',
              'R',
              'REVENUE',
              'A',
              'ASSET',
              a.COMPILED_VALUE_ATTRIBUTES) NATURE

  from FND_FLEX_VALUES_VL a

 where a.FLEX_VALUE_SET_ID = 1013510
     
   and a.FLEX_VALUE like '1%'
     
   and a.COMPILED_VALUE_ATTRIBUTES not like '%A%'

union

select a.FLEX_VALUE,
     
       a.DESCRIPTION,
     
       a.ENABLED_FLAG,
     
       a.END_DATE_ACTIVE,
     
       decode(substr(a.COMPILED_VALUE_ATTRIBUTES, 5, 1),
              'E',
              'EXPENSE',
              'L',
              'LIABILITY',
              'O',
              'Ownership/Stockholder''s Equity',
              'R',
              'REVENUE',
              'A',
              'ASSET',
              a.COMPILED_VALUE_ATTRIBUTES) NATURE

  from FND_FLEX_VALUES_VL a

 where a.FLEX_VALUE_SET_ID = 1013510
     
   and a.FLEX_VALUE like '2%'
     
   and a.COMPILED_VALUE_ATTRIBUTES not like '%L%'

union

select a.FLEX_VALUE,
     
       a.DESCRIPTION,
     
       a.ENABLED_FLAG,
     
       a.END_DATE_ACTIVE,
     
       decode(substr(a.COMPILED_VALUE_ATTRIBUTES, 5, 1),
              'E',
              'EXPENSE',
              'L',
              'LIABILITY',
              'O',
              'Ownership/Stockholder''s Equity',
              'R',
              'REVENUE',
              'A',
              'ASSET',
              a.COMPILED_VALUE_ATTRIBUTES) NATURE

  from FND_FLEX_VALUES_VL a

 where a.FLEX_VALUE_SET_ID = 1013510
     
   and a.FLEX_VALUE like '3%'
     
   and a.COMPILED_VALUE_ATTRIBUTES not like '%O%'

union

select a.FLEX_VALUE,
     
       a.DESCRIPTION,
     
       a.ENABLED_FLAG,
     
       a.END_DATE_ACTIVE,
     
       decode(substr(a.COMPILED_VALUE_ATTRIBUTES, 5, 1),
              'E',
              'EXPENSE',
              'L',
              'LIABILITY',
              'O',
              'Ownership/Stockholder''s Equity',
              'R',
              'REVENUE',
              'A',
              'ASSET',
              a.COMPILED_VALUE_ATTRIBUTES) NATURE

  from FND_FLEX_VALUES_VL a

 where a.FLEX_VALUE_SET_ID = 1013510
     
   and a.FLEX_VALUE like '4%'
     
   and a.COMPILED_VALUE_ATTRIBUTES not like '%R%'

union

select a.FLEX_VALUE,
     
       a.DESCRIPTION,
     
       a.ENABLED_FLAG,
     
       a.END_DATE_ACTIVE,
     
       decode(substr(a.COMPILED_VALUE_ATTRIBUTES, 5, 1),
              'E',
              'EXPENSE',
              'L',
              'LIABILITY',
              'O',
              'Ownership/Stockholder''s Equity',
              'R',
              'REVENUE',
              'A',
              'ASSET',
              a.COMPILED_VALUE_ATTRIBUTES) NATURE

  from FND_FLEX_VALUES_VL a

 where a.FLEX_VALUE_SET_ID = 1013510
     
   and a.FLEX_VALUE like '5%'
     
   and a.COMPILED_VALUE_ATTRIBUTES not like '%E%'

---------------------------

  select f.FLEX_VALUE,
       
         f.DESCRIPTION,
       
         f.ENABLED_FLAG,
       
         f.END_DATE_ACTIVE,
       
         decode(v.ACCOUNT_TYPE,
                'E',
                'EXPENSE',
                'L',
                'LIABILITY',
                'O',
                'Ownership/Stockholder''s Equity',
                'R',
                'REVENUE',
                'A',
                'ASSET',
                v.ACCOUNT_TYPE) GL_CODE_COMB
       
        ,
         decode(substr(f.COMPILED_VALUE_ATTRIBUTES, 5, 1),
                'E',
                'EXPENSE',
                'L',
                'LIABILITY',
                'O',
                'Ownership/Stockholder''s Equity',
                'R',
                'REVENUE',
                'A',
                'ASSET',
                f.COMPILED_VALUE_ATTRIBUTES) VALUE_SET
       
          from FND_FLEX_VALUES_VL f, gl_code_combinations v
       
         where v.SEGMENT4 = f.FLEX_VALUE
             
           and f.FLEX_VALUE_SET_ID = 1013510
             
           and v.ACCOUNT_TYPE != substr(f.COMPILED_VALUE_ATTRIBUTES, 5, 1)