5

実際のテーブルをクエリせずに (Oracle) SQL を実行する可能性を探しています。ここで、DUAL テーブルに関するヒントを見つけました。それはかなり良いです。しかし、「どこからも」複数の行を選択するための短い解決策を探しています。それは可能ですか?これが私が考えることができる最短のクエリです。これは私が必要とすることを行います:

Select 1, 'foo' FROM DUAL union 
Select 2, 'bar' FROM DUAL union  
Select 3, 'more stuff' FROM DUAL

しかし、結果にもっと多くの行が必要な場合は、かなり不便です。もっと短い方法はありますか?何かのようなもの

Select 1, 'foo'; 2, 'bar'; 3, 'more stuff' from dual 
or 
Select * from (1, 'foo') union (2, 'bar') union (3, 'more stuff') 

うまくいかないのはわかっていますが、アイデアはありますか?クエリの結果を転置する簡単な方法はありますか? 何かのようなもの:

Select transposed (1, 'foo', 2, 'bar', 3, 'more stuff') from dual 

「オーバーヘッド文字」の量を最小限に抑えたいです。

4

2 に答える 2

10

connect by level次のような、より多くのレコードを生成するために使用できます。

select level, decode(level, 1,'foo', 2,'bar', 3,'more stuff')
from dual connect by level <= 3
于 2012-02-27T12:28:05.827 に答える
2

In case there are multiple Columns - then this works as well -

select
  REGEXP_SUBSTR (jango,'^[^#]*') as f1,
  REGEXP_SUBSTR(REGEXP_REPLACE(jango,'^([^#]*#){1}', ''),'^[^#]*') as f2,
  REGEXP_REPLACE(jango,'^([^#]*#){2}', '') as f3
from
  (
  Select decode(level,
    1, 'foo#koo#joo',
    2, 'bar#loo#too' ,
    3, 'more stuff#doo#dingo') as jango
  from dual connect by level <= 20
  )

Here is how it works - The inner query is same as above I have appended multiple Columns using # - need to take care that it is not a part in the regex family else we need to escape it.

  Select decode(level,
    1, 'foo#koo#joo',
    2, 'bar#loo#too' ,
    3, 'more stuff#doo#dingo') as jango
  from dual connect by level <= 20

Gives the following -

             Jango
-------------------
foo#koo#joo
bar#loo#too
more stuff#doo#dingo

Now the following piece selects from the output column - 'jango', anything upto #

  REGEXP_SUBSTR (jango,'^[^#]*') as f1,

  O/p --> foo

for the Second column we remove the contents of the 1st column followed by #

  REGEXP_REPLACE(jango,'^([^#]*#){1}', '')

  we get --> koo#joo

Now the 1st step - get the first field.

for more fields {1} can be increased.

于 2013-09-26T05:48:04.850 に答える