如何在postgresql中的条件上复制行字段

时间:2013-10-04 10:05:01

标签: sql postgresql

我暴露了我的问题: 这是我的表:

id      ticket_id   Severity    Changed_field    Last_modified_date
1       ticket1     Minor                        01/01/2013
2       ticket1                 Category         02/01/2013
3       ticket1                 Status           03/01/2013
4       ticket1     Major       Severity         04/01/2013
5       ticket1                 Assigned_to      05/01/2013

我希望:

id      ticket_id   Severity    Changed_field    Last_modified_date
1       ticket1     Minor                        01/01/2013
2       ticket1     Minor       Category         02/01/2013
3       ticket1     Minor       Status           03/01/2013
4       ticket1     Major       Severity         04/01/2013
5       ticket1     Major       Assigned_to      05/01/2013

或:

id      ticket_id   Severity    Changed_field    Last_modified_date
1       ticket1     Major                        01/01/2013
2       ticket1     Major       Category         02/01/2013
3       ticket1     Major       Status           03/01/2013
4       ticket1     Major       Severity         04/01/2013
5       ticket1     Major       Assigned_to      05/01/2013

1 个答案:

答案 0 :(得分:1)

怎么样

SELECT 
     id
    ,ticket_id
    ,CASE WHEN COALESCE(Severity, '') = ''
             THEN 'Minor'
             ELSE Severity
     END AS Severity
    ,Changed_field
    ,Last_modified_date
FROM YOUR_Table