Oracle 11g SQL将列数据拆分为多列数据
我有一个数据如下的要求Oracle 11g SQL将列数据拆分为多列数据,sql,oracle,Sql,Oracle,我有一个数据如下的要求 ID Date Comments 101 23/Jun/2018 RR: Insufficient resource 101 23/Jun/2018 Next week apply for leave 102 22/Jun/2018 NULL 103 21/Jun/2018 Go ahead 我需要一个Oracl
ID Date Comments
101 23/Jun/2018 RR: Insufficient resource
101 23/Jun/2018 Next week apply for leave
102 22/Jun/2018 NULL
103 21/Jun/2018 Go ahead
我需要一个Oracle sql来获取上述数据
ID DATE A_comments D_comments
101 23/Jun/2018 Next week apply for leave RR: Insufficient resource
102 22/Jun/2018
103 21/Jun/2018 Go ahead
当注释带有前缀RR时:对于特定ID,它应该转到D_comments
,如果没有前缀RR,它应该转到A_comments
请帮助我实现这一点。此查询适用于您的示例数据。但是,如果有Alex提到的任何其他未知场景,请编辑您的问题以添加它
SELECT
id,
date_t,
MAX(
CASE
WHEN comments LIKE 'RR:%' THEN NULL
ELSE comments
END
) a_comments,
MAX(
CASE
WHEN comments LIKE 'RR:%' THEN comments
END
) d_comments
FROM
t
GROUP BY
id,
date_t;
您可以使用CASE
语句根据值“分布”列:
SELECT id,
date,
case when comments not like 'RR:%' then comments end as A_comment,
case when comments like 'RR:%' then comments end as D_comment
FROM my_table;
输出将是
ID DATE A_comments D_comments
101 23/Jun/2018 Next week apply for leave
101 23/Jun/2018 RR: Insufficient resource
102 22/Jun/2018
103 21/Jun/2018 Go ahead
因此,您需要将列“折叠”成一行,例如使用MAX
:
SELECT id,
date,
MAX(case when comments not like 'RR:%' then comments end) as A_comment,
MAX(case when comments like 'RR:%' then comments end) as D_comment
FROM my_table
GROUP BY id, date;
SELECT id,
date,
LISTAGG(case when comments not like 'RR:%' then comments end, '-'
WITHIN GROUP (ORDER BY comments)) as A_comment,
MAX(case when comments like 'RR:%' then comments end) as D_comment
FROM my_table
GROUP BY id, date;
这将产生
ID DATE A_comments D_comments
101 23/Jun/2018 Next week apply for leave RR: Insufficient resource
102 22/Jun/2018
103 21/Jun/2018 Go ahead
编辑:
为了反映您的评论,要附加所有评论,您可以使用listag
而不是MAX
:
SELECT id,
date,
MAX(case when comments not like 'RR:%' then comments end) as A_comment,
MAX(case when comments like 'RR:%' then comments end) as D_comment
FROM my_table
GROUP BY id, date;
SELECT id,
date,
LISTAGG(case when comments not like 'RR:%' then comments end, '-'
WITHIN GROUP (ORDER BY comments)) as A_comment,
MAX(case when comments like 'RR:%' then comments end) as D_comment
FROM my_table
GROUP BY id, date;
试试下面的方法
WITH t
AS (SELECT 101 id_col,
'23/Jun/2018' date_col,
'RR: Insufficient resource' comments
FROM DUAL
UNION
SELECT 101 id_col,
'23/Jun/2018' date_col,
'Next week apply for leave' comments
FROM DUAL
UNION
SELECT 102 id_col, '22/Jun/2018' date_col, 'NULL' comments FROM DUAL
UNION
SELECT 103 id_col, '21/Jun/2018' date_col, 'Go ahead' comments
FROM DUAL)
( SELECT id_col,
date_col,
MAX (CASE WHEN comments NOT LIKE 'RR:%' THEN comments END)
a_comment,
MAX (CASE WHEN comments LIKE 'RR:%' THEN comments END) d_comment
FROM t
GROUP BY id_col, date_col)
ORDER BY id_col
如果同一ID有多行带有(或不带有)RR前缀,该怎么办?或者每个ID只有一条注释,但它们在不同的日期?对于ID,我们只能有一条带有RR前缀的注释。但是如果没有RR前缀,我们可以有多条注释。在这种情况下,我们可以附加所有评论感谢您的解决方案欢迎!请查看请检查我的答案,如果它对您有帮助,请接受它,或者对给定的解决方案给出相关反馈(评论)。请阅读:了解为什么它很重要。