Friday, September 14, 2018

convert rows to columns

Use the below query to convert rows to columns:

select  current_balance, account_no,
         sum( decode( SRLNO, 1, collected_amount ) ) AMOUNT_1,
         sum( decode( SRLNO, 2, collected_amount )  ) AMOUNT_2,
         sum( decode( SRLNO, 3, collected_amount )  ) AMOUNT_3,
         sum( decode( SRLNO, 4, collected_amount )  ) AMOUNT_4,
         sum( decode( SRLNO, 5, collected_amount ) ) AMOUNT_5,      
         sum( decode( SRLNO, 6, collected_amount ) ) AMOUNT_6,
         sum( decode( SRLNO, 7, collected_amount )  ) AMOUNT_7,
         sum( decode( SRLNO, 8, collected_amount )  ) AMOUNT_8,
         sum( decode( SRLNO, 9, collected_amount )  ) AMOUNT_9,
         sum( decode( SRLNO, 10, collected_amount ) ) AMOUNT_10,
       
         sum( decode( SRLNO, 11, collected_amount ) ) AMOUNT_11,
         sum( decode( SRLNO, 12, collected_amount )  ) AMOUNT_12,
         sum( decode( SRLNO, 13, collected_amount )  ) AMOUNT_13,
         sum( decode( SRLNO, 14, collected_amount )  ) AMOUNT_14,
         sum( decode( SRLNO, 15, collected_amount ) ) AMOUNT_15,
           sum( decode( SRLNO, 16, collected_amount )  ) AMOUNT_16,
         sum( decode( SRLNO, 17, collected_amount ) ) AMOUNT_17
     from (
select e.current_balance, a.account_no, a.collected_amount,a.update_ts,
rank( ) OVER (PARTITION BY
a.account_no ORDER BY a.update_ts
NULLS LAST) SRLNO from  return  a , account e
where a.account_no=e.account_no
and e.ag_id=1
--and e.account_no='100086016'
order by a.account_no
)

   group by current_balance, account_no
   order by account_no


No comments:

Post a Comment