替换Oracle SQL中的函数

时间:2014-12-03 15:26:50

标签: sql oracle replace

我正在使用oracle SQL,我有以下查询:

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'

输出应该是如下的查询:

select id,
       count(distinct name2) over (partition by name3) / count(*) over (),
       count(distinct name3) over (partition by name4) / count(*) over (),
       . . .
from mytable;

我希望得到而不是:

count(distinct name2) over (partition by name3) / count(*) over ()

此查询:

count(distinct name3) over (partition by name2) / count(*) over ()

任何人都可以建议如何替换列值的顺序? (<thiscol><nextcol>)。我尝试将<thiscol>替换为<nextcol>,但它给了我相同的结果。我尝试了许多其他成功的事情。

任何?

1 个答案:

答案 0 :(得分:1)

那是真的奇怪。相反,让我们按相反的顺序排序:

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 desc)
              )
from all_tab_columns atc
where table_name = 'mytable';

请注意排序中的desc