I have the following table
SELECT * FROM TABLE1
Date ID Completed_Task
01/01/2019 X300 SA, MA, TY
01/01/2019 X400 SA, GT, JY
01/01/2019 X500 SA, GT, TY
I need the table to have one Completed_Task per row and count of that completed_task(it will be 1 if it happened once)
Desired Output:
Date ID Completed_Task Count
01/01/2019 X300 SA 1
01/01/2019 X300 MA 1
01/01/2019 X300 TY 1
01/01/2019 X400 SA 1
01/01/2019 X400 GT 1
01/01/2019 X400 JY 1
01/01/2019 X500 SA 1
01/01/2019 X500 GT 1
01/01/2019 X500 TY 1
Let me know how I can achieve this in Oracle Sql. Your help is appreciated.
How about a JSON approach?
select t.dt, t.id, x.task
from table1 t
cross apply json_table(
'["' || replace(completed_task, ', ', '", "') || '"]',
'$[*]' columns (task varchar2(10) path '$')
) x
The idea is to treat the CSV list as a JSON array. For this, we user string functions, to turn something like 'SA, MA, TY'
to ["SA", "MA", "TY"]
. Then, we can use set-returning function json_table()
to unnest the array to rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.