按Oracle SQL

时间:2018-04-20 21:12:19

标签: sql oracle group-by

我有一个表,其中有几个条目大致相同,除了它们中的一些标记。这些是从Web服务返回的消息,必须以这种方式存储。

鉴于此示例查询:

SELECT entry_error.error_desc, 
       Count(entry_error.id) 
FROM   entry_message err_entries 
       full outer join entry_error 
                    ON err_entries.id = entry_error.id 
       full outer join error_code 
                    ON entry_error.error_code = error_code.error_code 
WHERE NOT EXISTS(SELECT id_father 
                      FROM   entry_message creator 
                      WHERE  err_entries.id = creator.id_father) 
GROUP  BY entry_error.error_desc;

我得到这样的输出:

entry_error.error_desc                              count(entry_error.id)                
First Sample Text: 321; Second Sample Text: 123;    1
First Sample Text: 456; Second Sample Text: 654;    1 
First Sample Text: 789; Second Sample Text: 987;    1

但我希望它像:

entry_error.error_desc                              count(entry_error.id)                
First Sample Text: {0}; Second Sample Text: {1};    3

是否可以直接在我的查询中执行此操作?

编辑:请注意,这些消息只是一个示例,有几个不同的消息。它们无法在查询中明确写入。 我需要查询来分组类似的通用消息(使用类似UTL_MATCH,idk的东西),它们是X%相似的。

1 个答案:

答案 0 :(得分:1)

  

我认为这与你想要的很接近 -

WITH ERR AS (
    SELECT
        'FIRST SAMPLE TEXT: 321; SECOND SAMPLE TEXT: 123;' AS ERR_DESC,
        1 AS ERR_COUNT
    FROM
        DUAL
    UNION
    SELECT
        'FIRST SAMPLE TEXT: 456; SECOND SAMPLE TEXT: 654;' AS ERR_DESC,
        1 AS ERR_COUNT
    FROM
        DUAL
    UNION
    SELECT
        'FIRST SAMPLE TEXT: 789; SECOND SAMPLE TEXT: 987;' AS ERR_DESC,
        1 AS ERR_COUNT
    FROM
        DUAL
    UNION
    SELECT
        'FIRST ERROR MESSAGE: 333; SECOND ERROR MESSAGE: 432;' AS ERR_DESC,
        1 AS ERR_COUNT
    FROM
        DUAL
) SELECT
    REGEXP_REPLACE(ERR_DESC,': [0-9]*;','{0}') AS MSG,
    SUM(ERR_COUNT) AS ERROR_COUNT
FROM
    ERR
GROUP BY
    REGEXP_REPLACE(ERR_DESC,': [0-9]*;','{0}');
  

输出 -

"MSG","ERROR_COUNT"
"FIRST SAMPLE TEXT{0} SECOND SAMPLE TEXT{0}",3
"FIRST ERROR MESSAGE{0} SECOND ERROR MESSAGE{0}",1