我怎样才能在oracle中实现以下目标?

How can i achieve the following in oracle?

我正在尝试将 table 以下转换为所需的输出,如下所示。 我怎样才能达到下面想要的输出 table

数据集:

question_id    element_id
1              7
1              8
1              z
2              x
2              9
2              10
3              10
3              11
3              12
3              y

期望的结果:

question_id    element_id       element
1              7                   z
1              8                   z
2              9                   x
2              10                  x
3              10                  y
3              11                  y
3              12                  y

question_id    element_id       element
1              7                   z
1              8                   null
2              9                   x
2              10                  null
3              10                  y
3              11                  null
3              12                  null

您似乎希望每个问题的每一行都有一个 non-numeric 值,然后过滤掉结果中只有数字元素。如果是:

select t.*
from (select t.*,
             max(case when regexp_like(element_id, '[^0-9]') then element_id end) over (partition by question_id) as element
      from t
     ) t
where regexp_like(element_id, '^[0-9]+$');

您可以使用条件聚合在分析函数的帮助下为每个 question_id 组查找非数字值:

WITH cte AS (
    SELECT t.*, ROW_NUMBER() OVER (PARTITION BY question_id
                                   ORDER BY element_id) rn
    FROM yourTable t
),
cte2 AS (
    SELECT
        question_id,
        element_id,
        CASE WHEN rn = 1
             THEN MAX(CASE WHEN REGEXP_LIKE(element_id, '^[A-Za-z]+$')
                           THEN element_id END)
                  OVER (PARTITION BY question_id) END AS element
    FROM cte
)

SELECT
    question_id,
    element_id,
    element
FROM cte2
WHERE
    REGEXP_LIKE(element_id, '^[0-9]+$')
ORDER BY
    question_id,
    element_id;

Demo