使用connect by生成的数字序列中的数字

时间:2017-04-19 09:33:38

标签: sql oracle

Select Rownum From dual Connect By Rownum <= '4' ;

使用此SQL列出1到4之间的数字。

我想列出1到4个跳过3.以便结果看起来像

1
2
4

请帮助

1 个答案:

答案 0 :(得分:1)

SELECT CASE WHEN LEVEL < 3 THEN LEVEL ELSE LEVEL + 1 END AS value
FROM   DUAL
CONNECT BY LEVEL < 4;

SELECT *
FROM   (
  SELECT LEVEL AS value
  FROM   DUAL
  CONNECT BY LEVEL <= 4
)
WHERE  value <> 3;

或者,由于目前还不清楚你的序列应该如何超越4,你可以这样做:

SELECT POWER( 2, LEVEL - 1 ) AS value
FROM   DUAL
CONNECT BY LEVEL <= 3;