I have created a view in Redshift using the following query:
drop view if exists public.vw_some_table cascade;
create or replace view public.vw_some_table as
(
with interim_table as
(select
player_name
, avg(score) as total_score
from public.table_1
group by 1
)
, final_table as
(select
player_name
, total_score + 3 as total_score_adj
from interim_table
)
select *
from final_table);
However, when I check the view’s DDL statement, I see it is stored as follows, using nested subqueries, rather than CTEs:
create view vw_some_table(player_name, total_score_adj) as
SELECT final_table.player_name, final_table.total_score_adj
FROM ( SELECT interim_table.player_name, interim_table.total_score + 3 AS total_score_adj
FROM ( SELECT table_1.player_name, avg(table_1.score) AS total_score
FROM table_1
GROUP BY table_1.player_name) interim_table) final_table;
Is there anyway to prevent this refactoring of the view from taking place? Even if the two output the same results, I would like to retain the business logic as defined using CTEs for easier interpretation/maintenance down the road.