3

Note: I would like to do this in a single SQL statement. not pl/sql, cursor loop, 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 pivots the values of the String column into distinct rows such as:

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

How do I go about doing this? It's like a pivot table but I am trying to pivot the data in a column, rather than pivoting the columns in the table.

Note that in real application, I do not actually know what the values of the String column are; I only know that the separation between values is '~'

Patrick Bateman
  • 271
  • 2
  • 5
  • 14
  • possible duplicate of [Split column to multiple rows](http://stackoverflow.com/questions/3713107/split-column-to-multiple-rows) – Tony Andrews Jun 16 '11 at 15:45

1 Answers1

2

Given this 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 query:

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> 

You can adjust the maximum number of items in your search string by adjusting the "LEVEL < 20" to "LEVEL < your_max_items".

DCookie
  • 42,630
  • 11
  • 83
  • 92