ID Name 1 and 4 apples, bananas, oranges, 2, 1 watermelon, apple 2 minus 2 watermelon, orange, pear 5,1,4,6,2 grapes, apples, oranges, pears, watermelon
To get the following list Id name 1 apple 2 watermelon Three bananas 4 oranges 5 grapes Six pears
For each master, how do you write this SQL statement?
CodePudding user response:
This if can use stored procedures in a temporary table is relatively simple, using a SQL written as if do not have what idea, wait for SQL great god shows the code
CodePudding user response:
with tab1 as ( Select rowidd 'r1', '1' and 4 'id' apples, bananas, oranges' name from dual union all Select 'r2' rowidd, '2 minus 2' id, 'watermelon, orange, pears' name from dual ) Select distinct Regexp_substr (t1. Id, '[^,] +' 1, level) id, Regexp_substr (t1) name, '[^,] +', 1, level) name The from tab1 t1 Connect by the prior t1. Rowidd=t1. Rowidd And the prior sys_guid () is not null And level & lt;=regexp_count (t1. Id ', ') + 1 The order by regexp_substr (t1. Id, '[^,] +' 1, level) ;
CodePudding user response:
Roughly is such, can be concluded that the original poster to effect, can enlarge the level to adapt to a long string of level, SELECT DISTINCT Id, NAME The FROM (SELECT Regexp_Substr (Anderson, d, '[^,] +' 1, LEVEL) Id, Regexp_Substr (a.N ame, '[^,] +' 1, LEVEL) NAME The FROM (SELECT '1 and 4' Id, apples, bananas, oranges' NAME The FROM Dual UNION ALL SELECT '2, 1' Id, watermelon, apple 'NAME The FROM Dual UNION ALL SELECT '2 minus 2' Id, watermelon, orange, pears' NAME The FROM Dual UNION ALL SELECT '5,1,4,6,2' Id, grapes, apples, oranges, pears, watermelon 'NAME The FROM Dual) a CONNECT BY LEVEL & lt; =3)WHERE the Id IS NOT NULL The ORDER BY ID.
The following statements is to forge the data, can be replaced with the actual table (SELECT '1 and 4' Id, apples, bananas, oranges' NAME The FROM Dual UNION ALL SELECT '2, 1' Id, watermelon, apple 'NAME The FROM Dual UNION ALL SELECT '2 minus 2' Id, watermelon, orange, pears' NAME The FROM Dual UNION ALL SELECT '5,1,4,6,2' Id, grapes, apples, oranges, pears, watermelon 'NAME The FROM Dual) a