卡在了Postgresql的递归查询中

时间:2018-11-30 09:36:35

标签: postgresql subquery

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

我想根据状态获取通过选择查询来添加数据(如果状态为  = 3然后插入日期为server_time的投诉状态,仅插入一条记录,但是如果状态为5,则插入  代表完成多次插入ans server_time,因为  相同的数据仅重复状态,重新打开,server_time和  完成日期因状态而异。

select complaint_id,status,
       (CASE WHEN (rejectionreason IS NULL OR > 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

此查询的输出:

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

enter image description here

complaintstatus表中,我必须按照  根据状态并在0utput之上重新打开数据并选择  服务器状态时间和完成日期

 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

enter image description here

0 个答案:

没有答案