A report which dynamically ignores all zero value columns

A risk reporting table contains exposure values by counterparty for the last 7 days.

 

The structure of the table is as follows:

 

COUNTERPARTY        VARCHAR2(50)

T                    NUMBER

T-1                  NUMBER

T-2                  NUMBER

T-3                  NUMBER

T-4                  NUMBER

T-5                  NUMBER

T-6                  NUMBER

 

Values for Saturdays and Sundays will always be zero.

T to T-6 will be a moving window, therefore we can not hardcode which days are Saturdays or Sundays.

 

The requirement is to load the data into another table, where the final product will be a CSV, but users do not want to include the Saturday/Sunday all zero columns.

 

How do we do this?

 

A quick way would be to write a code like this

 

 

 

 

 

declare
  v_t number;
  v_t1 number;
  v_t2 number;
  v_t3 number;
  v_t4 number;
  v_t5 number;
  v_t6 number;

  v_sql varchar2(2000);

begin
select sum(t), sum(t-1), sum(t-2), sum(t-3), sum(t-4), sum(t-5), sum(t-6)
into v_t, v_t1,v_t2,v_t3,v_t4,v_t5,v_t6
from table1 ;

v_sql := ‘insert into newtable as select ‘
if v_t <> 0 then append to v_sql
if v_t1 <> 0 then append to v_sql
if v_t2 <> 0 then append to v_sql
etc

v_sql := v_sql ||’ from table1 ;’

execute immediate v_sql;
commit;

end;

Thursday, December 18th, 2008 at 18:14
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>