I'm using oracle SQL, and i have the following query:
select replace(replace('count(distinct <thiscol>) over (partition by <nextcol>) / count(*) over () as <thiscol>_<nextcol>,',
'<thiscol>', column_name
), '<nextcol>', lead(column_name) over (order by column_id)
)
from all_tab_columns atc
where table_name = 'mytable'
The output supposed to be queries such as follow:
select id,
count(distinct name2) over (partition by name3) / count(*) over (),
count(distinct name3) over (partition by name4) / count(*) over (),
. . .
from mytable;
I'm expecting to get instead of:
count(distinct name2) over (partition by name3) / count(*) over ()
this query:
count(distinct name3) over (partition by name2) / count(*) over ()
Anyone can advise how to replace the order of the column values? (<thiscol> and <nextcol>). I tried to replace <thiscol> with <nextcol> but it gave me the same result. I tried many other things with of success.
Anyone?
LAGinstead ofLEAD?