Column Summary Values ​​Based on Search String

Note. I would like to do this in a single SQL statement. not pl / sql, cursor, etc.

I have data that looks like this:

ID    String
--    ------
01    2~3~1~4
02    0~3~4~6
03    1~4~5~1

I want to provide a report that somehow rotates the values ​​of a String column into various rows, such as:

Value    "Total number in table"
-----    -----------------------
1        3
2        1
3        2
4        3
5        1
6        1

How should I do it? This looks like a pivot table, but I'm trying to rotate the data in a column, rather than rotate the columns in the table.

Note that in a real application, I really don't know what the values ​​of the String column are; I only know that the separation between the values ​​is equal to '~'

+3
source share
1 answer

Given these test data:

CREATE TABLE tt (ID INTEGER, VALUE VARCHAR2(100));
INSERT INTO tt VALUES (1,'2~3~1~4');
INSERT INTO tt VALUES (2,'0~3~4~6');
INSERT INTO tt VALUES (3,'1~4~5~1');

This request:

SELECT VALUE, COUNT(*) "Total number in table"
  FROM (SELECT tt.ID, SUBSTR(qq.value, sp, ep-sp) VALUE
          FROM (SELECT id, value
                     , INSTR('~'||value, '~', 1, L) sp  -- 1st posn of substr at this level
                     , INSTR(value||'~', '~', 1, L) ep  -- posn of delimiter at this level
                  FROM tt JOIN (SELECT LEVEL L FROM dual CONNECT BY LEVEL < 20) q -- 20 is max #substrings
                            ON LENGTH(value)-LENGTH(REPLACE(value,'~'))+1 >= L 
               ) qq JOIN tt on qq.id = tt.id)
 GROUP BY VALUE
 ORDER BY VALUE;

Results in:

VALUE      Total number in table
---------- ---------------------
0                              1
1                              3
2                              1
3                              2
4                              3
5                              1
6                              1

7 rows selected

SQL> 

, "LEVEL < 20" "LEVEL < your_max_items".

+2

All Articles