我在oracle中有下表。版本9i
Num count
123 1
345 4
455 3
234 2
542 1
我正在尝试编写一个select查询,它给出了以下输出。
123
345
345
345
345
455
455
455
234
234
542
每个号码都应显示其计数次数。如果我使用函数,这可以实现吗?我不允许创建任何临时表。
我知道我必须使用循环序列然后使用UNION ALL但不知道如何实现它。我试过这个
select Num from <table> where count=1
union all
// what fits here?
答案 0 :(得分:3)
这可以使用伪列LEVEL
和Correlated Query
来重复数字计数次数...
尝试以下查询
select num from (select distinct num,level,ROWID as RD
from table a
connect by level<= (select count from table b where a.num=b.num))
order by rd;
答案 1 :(得分:1)
试试这个
SELECT A.NUM
FROM Test A
INNER JOIN (SELECT ROWNUM RN FROM dual connect by level < 100) B
ON B.rn <= A.count
order by A.NUM;
答案 2 :(得分:0)
WITH cte ("Num", "count")
AS
(
SELECT "Num", "count" FROM TableName
UNION ALL
SELECT "Num", "count" - 1 FROM cte WHERE "count" > 1
)
SELECT "Num"
FROM cte
ORDER BY "Num"
答案 3 :(得分:0)
如果您使用11gR2,可以这样做:
with t(num, i) as
(
select num, count from tab1 union all
select num, i-1
from t
where i >1
)
select num from t
order by num;