title: Stuck in recursive query with postgresql
body: <pre><code>INSERT INTO complaintstatus(complaintid,currentstatus,reason,photo,datetime,open_reopen_status,actiontaken,parent_id)
  select complaint_id,status,(CASE WHEN (rejectionreason IS NULL OR rejectionreason = '') THEN reopen_reason ELSE rejectionreason END),photo_name,server_time,completion_date,reopen,actiontaken,parent_complaint_id
  from complaint_details_v2 
  where status !=0 
  order by parent_complaint_id
  </code></pre>
  
  <p>I want to add data as per the status fetch by select query if status
   =3 then insert into complaintstatus with date selected as server_time and insert into only one record but if status is 5 then insert
   multiple insertion on behalf of completion ans server_time because
   same data is repeated only status, reopen, server_time and
   completion_date is vary according to the status.</p>
  
  <pre><code>select complaint_id,status,
         (CASE WHEN (rejectionreason IS NULL OR &gt; rejectionreason = '') 
                 THEN reopen_reason ELSE rejectionreason
          END),
          photo_name,
          (CASE WHEN (status='3') THEN server_time 
                ELSE completion_date 
           END),
           reopen,
           actiontaken,
           parent_complaint_id 
  from
  complaint_details_v2 
  where status !=0 
  order by parent_complaint_id
  </code></pre>
  
  <p>output of this query:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>ComplaintId Status rejectionreason    Photoname server_time completiondate reopen actiontaken parent_complaint_id
  194          3      sitce picture      abc.jpg    2017-09-15                   0                109
                      is not available
  195          5                          xyz.jpg   2017-09-15 2017-09-15        0                 195
                                                    08:22:44    00:00:00
  214          5                          asd.jpg   2017-09-19                   1   repair done   214
                                                    11:23:11
  1599         5                          abc.jpg   2017-09-25 2017-10-18        0   complete      214</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://i.stack.imgur.com/MXdIT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MXdIT.png" alt="enter image description here"></a></p>
  
  <p>In the <code>complaintstatus</code> table I have to insert data as per the
   above 0utput on the basis of status and reopen data and select
   servertime and completion date as per the status</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> Status rejectionreason    Photoname datetime      reopen actiontaken parent_complaint_id
    3     site picture       xyz.jpg    2017-09-15     0                 194
          are not available             07:59:59.582                  
    5                        abc.jpg    2017=09-15     0     repair done  195
                                        08:22:44.247
    5                        asd.jpg    2017-09-19     0     repairdone   214
                                        11:23:11.274
    0                                   2017-09-25     0                  214
                                        18:23:07.181
    5                        asd.jpg    2017-10-18     0     complete     214</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://i.stack.imgur.com/gbqNg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gbqNg.png" alt="enter image description here"></a></p>