1

I'm using Oracle Apex 4,2. I have a table with a column in it called 'versions'. In the 'versions' column for each row there is a list of values that are separated by commas e.g. '1,2,3,4'.
I'm trying to create a Select List whose list of values will be each of the values that are separated by commas for one of the rows. What would the SQL query for this be?

Example:

Table Name: Products

Name     | Versions
--------------------
myProd1  | 1,2,3
myProd2  | a,b,c

Desired output:
Two Select Lists.
The first one is obvious, I just select the name column from the products table. This way the user can select whatever product they want. The second one is the one I'm not sure about. Let's say the user has select 'myProd1' from the first Select List. Then the second select should contain the following list of values for the user to select from: '1.0', '1.1' or '1.2'.

4

1 に答える 1

5

あなたの最新のコメントを読んだ後、あなたが求めているのは LOV ではなくリスト アイテムであることを理解しました。LOVでもかまいませんが。最初のリスト項目/lov には、Prod1、Prod2、Prod3 など、ユーザーが選択したすべての製品のみが含まれます。 . 私の理解では、ユーザーはこのリストから製品ごとに 1 つの値しか選択できないためです。たとえば、Prod1 の値は 1、2、3、4 です。ただし、ユーザーは 1 つだけを選択する必要があります。正しい?これが、コンマ値をテーブルに変換する必要がある理由です。最初のクエリ選択は次のようになります。

SELECT prod_id
  FROM your_prod_table
 /

 id      
 --------
 myProd1 
 myProd2 
 .....

2 番目のクエリは、product_id が your_prod_table にあるすべてのバージョンを選択する必要があります。

 SELECT version FROM your_versions_table
   WHERE prod_id IN (SELECT prod_id FROM your_prod_table)
 /

 Versions
 --------
 1,2,3,4   -- myProd1 values
 a,b,c,d   -- myProd2 values
 .....

上記は、myProd1 などのすべての値など、製品のすべてのバージョンを返します。

コンマ区切りを変換する私の例を使用してください。テーブルへの値。ハードコードされた '1,2,3,4' をテーブルの値列に置き換えます。デュアルをテーブル名に置き換えます

単一のクエリと単一の結果で製品とバージョンが必要な場合は、両方のテーブルを単純に結合/外部結合 (左、右結合) します。

SELECT p.prod_id, v.version 
  FROM your_prod_table     p
     , your_versions_table v
  WHERE p.prod_id = v.prod_id
 /

この場合、出力に次のように表示されます。

 id      |  Values
 ------------------
 myProd1 | 1,2,3,4
 myProd2 | a,b,c,d

上記のクエリでコンマをテーブルに変換すると、次のようになります。すべてが 1 つのリストまたは LOV に含まれています。

 id      |  Values
 ------------------
 myProd1 | 1
 myProd1 | 2
 myProd1 | 3
 myProd1 | 4
 myProd2 | a
 myProd2 | b
 myProd2 | c
 myProd2 | d

これが役立つことを願っています。ここでも、APEX で使用可能な場合は、LOV またはリスト値を使用できます。2 つの個別の値リスト (製品用とバージョン用) の方が理にかなっています。リスト アイテムの場合、上記のように 2 つの個別のクエリが必要になり、値/バージョンのみをカンマからテーブルに変換する方が簡単です。しかし、それはあなた次第です。

コンマからテーブルへの例:

-- Comma to table - regexp_count --
SELECT trim(regexp_substr('1,2,3,4', '[^,]+', 1, LEVEL)) str_2_tab
  FROM dual
 CONNECT BY LEVEL <= regexp_count('1,2,3,4', ',')+1
/

-- Comma to table - Length -
SELECT trim(regexp_substr('1,2,3,4', '[^,]+', 1, LEVEL)) token
  FROM dual
CONNECT BY LEVEL <= length('1,2,3,4') - length(REPLACE('1,2,3,4', ',', ''))+1
/

-- Comma to table - instr --
SELECT trim(regexp_substr('1,2,3,4', '[^,]+', 1, LEVEL)) str_2_tab
  FROM dual
 CONNECT BY LEVEL <= instr('1,2,3,4', ',', 1, LEVEL - 1)
/

上記のすべての出力は同じです。

STR_2_TAB
----------
1
2
3
4

表へのカンマ - PL/SQL-APEXの例。LOV には、PL/SQL ではなく SQL が必要です。

DECLARE
  v_array apex_application_global.vc_arr2;
  v_string varchar2(2000);
BEGIN
-- Convert delimited string to array
   v_array:= apex_util.string_to_table('alpha,beta,gamma,delta', ',');

  FOR i in 1..v_array.count LOOP
    dbms_output.put_line('Array: '||v_array(i));
  END LOOP;

 -- Convert array to delimited string
  v_string:= apex_util.table_to_string(v_array,'|');
  dbms_output.put_line('String: '||v_string);
END;
/
于 2013-02-27T17:26:09.277 に答える