oracle - Removing duplicated subquery in pl/sql procedure -


how simplify following procedure?

i want remove duplication of clause.

if p_yn = 'y'     open p_cursor         view_a         (             [very long select statements]         )         select col_a, col_b, sum(col_c), sum(col_d) view_a         group col_a, col_b; else     open p_cursor         view_a         (             [very long select statements]         )         select col_1, col_2, sum(col_3), sum(col_4) view_a         group col_1, col_2; end if; 

i considered unon all.

with view_a (     [very long select statements] ) open p_cursor  select col_a, col_b, sum(col_c), sum(col_d) view_a group col_a, col_b p_yn = 'y'  union  select col_1, col_2, sum(col_3), sum(col_4) view_a group col_1, col_2 p_yn <> 'y';       

but doesn't work because 2 statements have different columns.

if union all can need, maybe can add same "fake" columns:

with view_a (     [very long select statements] ) open p_cursor  select col_a, col_b, sum(col_c), sum(col_d), null col_1, null col_2, null col_3, null col_4 view_a group col_a, col_b p_yn = 'y'  union  select null col_a, null col_b, null col_c, null col_d, col_1, col_2, sum(col_3), sum(col_4)  view_a group col_1, col_2 p_yn <> 'y';    

null should changed in else, depending on types of columns; besides, may work or not, depending on need opened cursor.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -