sql - Better way to write a query -
i'm creating view after joining table , view. view contains actual columns in join , of them calculated columns(lets call them type1). of them calculations of calculated columns (type2). there way can reference type1 columns calculate type2 names instead of writing entire code again? ( don't want create subquery either), checking if there efficient way write code. not posting actual code here ,this short form of i'm trying do:
create view abcd col1,col2 , calc_col1, calc_col2 , calc_col3 select a.col1 col1 , a.col2 col2, case when some_calulations 2 else 0 end calc_col1, ( case when some_calulations 2 else 0 end) - a.somecol calc_col2 , ( ( case when some_calulations 2 else 0 end) - a.somecol ) + 5 )as calc_col3 table1 left join view2 b on join_condition
select a.col1 col1, a.col2 col2, c.calc_col1, d.calc_col2, d.calc_col2 + 5 calc_col3 table1 left join view2 b on join_condition cross join lateral (select case when some_calulations 2 else 0 end calc_col1) c cross join lateral (select calc_col1 - a.somecol calc_col2) d
Comments
Post a Comment