Oracle SQL从表中获取唯一符号

时间:2017-04-18 22:19:50

标签: sql oracle

我有表格描述了smth。例如:

My_Table
id   description
================
1    ABC
2    ABB
3    OPAC
4    APEЧ

我需要从所有“描述”列中获取所有唯一符号。 结果应如下所示:

symbol
================
A
B
C
O
P
E
Ч

它适用于所有语言,因此,正如我所见,正则表达式无法提供帮助。 请帮我。感谢。

3 个答案:

答案 0 :(得分:3)

with    cte (c,description_suffix) as
        (
            select  substr(description,1,1)
                   ,substr(description,2)
            from    mytable
            where   description is not null

            union all

            select  substr(description_suffix,1,1)
                   ,substr(description_suffix,2)
            from    cte
            where   description_suffix is not null
        )
select    c
         ,count(*) as cnt
from      cte
group by  c
order by  c 

with    cte(n) as
        (
            select      level 
            from        dual 
            connect by  level <= (select max(length(description)) from mytable)
        )
select  substr(t.description,c.n,1) as c
       ,count(*)                    as cnt
from            mytable t
        join    cte c
        on      c.n <= length(description)
group by substr(t.description,c.n,1)
order by c
+---+-----+
| C | CNT |
+---+-----+
| A |   4 |
| B |   3 |
| C |   2 |
| E |   1 |
| O |   1 |
| P |   2 |
| Ч |   1 |
+---+-----+

答案 1 :(得分:0)

创建一个数字表并使用您需要的所有相关ID填充它(在这种情况下为1..maxlength的字符串)

SELECT DISTINCT
  locate(your_table.description, numbers.id)   AS symbol
FROM
  your_table
INNER JOIN
  numbers
    ON  numbers.id >= 1
    AND numbers.id <= CHAR_LENGTH(your_table.description)

答案 2 :(得分:0)

SELECT DISTINCT(SUBSTR(ll,LEVEL,1)) OP --Here DISTINCT(SUBSTR(ll,LEVEL,1)) is used to get all distinct character/numeric in vertical as per requirment
FROM
( 
    SELECT LISTAGG(DES,'') 
    WITHIN GROUP (ORDER BY ID) ll 
    FROM My_Table  --Here listagg is used to convert all values under description(des) column into a single value and there is no space in between
)
CONNECT BY LEVEL <= LENGTH(ll);