database - How to create a temporary / dynamic / virtual table when a SQL runs in Oracle? -
i have data has measured not in table. can not insert table nor can create table , insert these data. used dual following table. used join other tables.
with movie_genre ( select '10' "id", 'action' "genre" dual union select '20' "id", 'horror' "genre" dual union select '30' "id", 'comedy' "genre" dual union select '40' "id", 'adventure' "genre" dual union select '50' "id", 'drama' "genre" dual union select '60' "id", 'mystery' "genre" dual union select '70' "id", 'musical' "genre" dual ) select * movie_genre ;
so result -
id genre 10 action 20 horror 30 comedy 40 adventure 50 drama 60 mystery 70 musical
my question is, there better way this? suggestion life saver.
an example -
lets have table -
create table movies ( id number, name varchar2(50), genre_id number ); insert movies values (1, 'the hulk', 10); insert movies values (2, 'dumb , dumber', 30); insert movies values (3, 'frozen', 70);
and need result -
name genre is_in_genre hulk action yes hulk horror no hulk comedy no hulk adventure no hulk drama no hulk mystery no hulk musical no dumb , dumber action no dumb , dumber horror no dumb , dumber comedy yes dumb , dumber adventure no dumb , dumber drama no dumb , dumber mystery no dumb , dumber musical no frozen action no frozen horror no frozen comedy no frozen adventure no frozen drama no frozen mystery no frozen musical yes
here, not have movie_genre table.
you can pass genre string in order want , use regular expression generate movie_genre table.the sql fiddle here
with movie_genre ( select level * 10 id, regexp_substr(genre,'[^,]+',1,level) genre ( select ('action,horror,comedy,adventure,drama,mystery,musical') genre dual ) connect level <=regexp_count(genre,'[^,]+') ) select * movie_genre;
Comments
Post a Comment