SELECT SPRIDEN.SPRIDEN_ID,
       SPRIDEN.SPRIDEN_LAST_NAME,
       SPRIDEN.SPRIDEN_FIRST_NAME,
       TBRACCD.TBRACCD_TERM_CODE,
       TBRACCD.TBRACCD_TRAN_NUMBER,
       TBRACCD.TBRACCD_DETAIL_CODE,
       TBRACCD.TBRACCD_DESC,
       TBRACCD.TBRACCD_SRCE_CODE,
       TBRACCD.TBRACCD_BALANCE
FROM SATURN.SPRIDEN SPRIDEN
      INNER JOIN
      TAISMGR.TBRACCD TBRACCD
      ON SPRIDEN.SPRIDEN_PIDM = TBRACCD.TBRACCD_PIDM
     INNER JOIN TAISMGR.TBBDETC TBBDETC
        ON TBRACCD.TBRACCD_DETAIL_CODE = TBBDETC.TBBDETC_DETAIL_CODE
WHERE     SPRIDEN.SPRIDEN_CHANGE_IND IS NULL
      AND SPRIDEN.SPRIDEN_FIRST_NAME IS NOT NULL
/* Single term or blank for all terms */
      AND TBRACCD.TBRACCD_TERM_CODE LIKE NVL('&TERM_CODE','%')
      AND TBRACCD.TBRACCD_BALANCE <> 0
      AND TBBDETC.TBBDETC_REFUNDABLE_IND IS NULL
      AND TBBDETC.TBBDETC_PAYT_CODE = '&PAYT_CODE'
      AND TBBDETC.TBBDETC_TYPE_IND = 'P'
GROUP BY SPRIDEN.SPRIDEN_ID,
         SPRIDEN.SPRIDEN_FIRST_NAME,
         SPRIDEN.SPRIDEN_LAST_NAME,
         TBRACCD.TBRACCD_TERM_CODE,
         TBRACCD.TBRACCD_TRAN_NUMBER,
         TBRACCD.TBRACCD_DETAIL_CODE,
         TBRACCD.TBRACCD_BALANCE,
         TBRACCD.TBRACCD_DESC,
         TBRACCD.TBRACCD_SRCE_CODE
ORDER BY SPRIDEN.SPRIDEN_LAST_NAME ASC, SPRIDEN.SPRIDEN_FIRST_NAME ASC