我在oracle数据库中有这样一个频率表,其中n是每次出现的次数:
weight | N 12 | 3 34 | 2 56 | 1
是否可以将每行的数目乘以出现的数目,得到如下表格:
weight | N 12 | 1 12 | 1 12 | 1 34 | 1 34 | 1 56 | 1
谢谢您,
mu0hgdu01#
可以使用数字生成器、递归查询、xmltable。有很多选择。例如:
with t as (select weight, '1 to '||n list from frequency) select weight, 1 from t, xmltable(list)
或
with r(weight, n) as (select weight, n from frequency union all select weight, n-1 from r where n-1 > 0) select weight, 1 from r order by weight
D小提琴
r6hnlfcb2#
一种简单的方法是递归cte:
with recursive cte as ( select weight, n from t union all select weight, n - 1 from t where n > 1 ) select weight, 1 as n from cte;
请注意,尽管递归cte是标准的一部分,但是语法中有一些变化,因此您的数据库可能有点不同。编辑:在旧版本的oracle中,您可以生成所需的数字,然后生成数据:
with n as (select level as n from (select max(n) as n from t) n connect by level <= n ) select t.weight, 1 as n from t join n on n.n <= t.n;
新版本的oracle支持递归cte。
2条答案
按热度按时间mu0hgdu01#
可以使用数字生成器、递归查询、xmltable。有很多选择。例如:
或
D小提琴
r6hnlfcb2#
一种简单的方法是递归cte:
请注意,尽管递归cte是标准的一部分,但是语法中有一些变化,因此您的数据库可能有点不同。
编辑:
在旧版本的oracle中,您可以生成所需的数字,然后生成数据:
新版本的oracle支持递归cte。