我写了两个查询联合。我得到的结果是逗号分隔在两列中。输出显示为2条记录。
如何将它们分成4项记录,如预期结果所示?
select emp_id,dept_name,location from department where dept_id = 1
union
select emp_id,dept_name,location from sales_dept where dept_id = 1;输出
emp_id ----- dept_name------ location
r1-----------Retail,IT----- US, UK
k2-----------Sales,Chemical- NZ, SA
j3-----------Biotech(Chemistry,Tech)- JA我需要预期的输出如下所示:
emp_id ----- dept_name-----location
r1-----------Retail--------US
r1-----------IT----------- UK
k2-----------Sales---------NZ
k2-----------Chemical------SA
j3---------Biotech(Chemistry,Tech)--JAdept_name的最后一个记录是“生物技术(化学,技术)”,应该显示为单一记录,而不是分割。请让我知道怎么做。
吉姆提供的查询很好,除非在这个场景中,dept_name是生物技术(化学,技术),因为现在给出了需求。
发布于 2020-06-01 18:54:53
请使用以下查询,
select emp_id, dept_name, location from
(select distinct emp_id, trim(regexp_substr(dept_name,'[^,]+', 1, level) ) dept_name,
trim(regexp_substr(location,'[^,]+', 1, level) ) location, level
from pivot_comma
connect by regexp_substr(dept_name, '[^,]+', 1, level) is not null
order by emp_id, level);https://stackoverflow.com/questions/62138108
复制相似问题