Total Pageviews

2018/07/08

[PostgreSQL] Example for with...as..insert into

Problem
I try to get data from intent and utter_template, and insert data into story when data is not existed.


How-To
Here has the example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
   with source as (
      select t.project_id, t.intent, t.utter_template, t.story_name
      from (
       select t1.project_id, '* _' || t1.value || chr(10) as intent, 
              '   - ' || t2.name || chr(10) utter_template, 
              'story_' || substring(t1.value from 4) story_name 
         from (select * from intent 
               where project_id=:project_id and value like 'qa_%') t1, 
              (select * from utter_template 
               where project_id=:project_id and name like 'utter_qa%') t2 
         where substring(t1.value from 4) =  substring(t2.name from 10)     
      ) t
      where (t.story_name) not in 
            (select name from story where project_id=:project_id)
   )
   insert into story (project_id, name, content) 
   (select project_id, story_name, intent || utter_template from source)


No comments: