如何通过联合获取不同的记录postgresql

问题描述 投票:0回答:1

我正在获取一条记录,该记录具有两种状态,即活动状态和非活动状态,我们可以具有许多非活动状态,但是一次只能在一个表中拥有一个活动状态,因此我希望所有具有活动状态的记录以及除活动状态之外的所有非活动记录。

表记录:-

id  |  status  | status_message  | csr_id  
-----+----------+-----------------
  14 | inactive | Hi             |  1
 138 | inactive | Test campaigin |  1
 134 | inactive | Test           |  1
 137 | inactive | Test ctr group |  2
 139 | inactive | test 3         |  2
 140 | inactive | test 3         |  2
 141 | inactive | test 3         |  2
 142 | inactive | test 8         |  2
 144 | inactive | Itf            |  2
 143 | inactive | test 9         |  2
 145 | inactive | Test evry1     |  2
 150 | active   | Test live cmrt1|  1

查询以供参考

 (select * from profile where csr_id IN (2,1) AND status = 'active') 
    UNION 
 (select DISTINCT ON (csr_id) *from profile where csr_id IN (2,1) AND status = 'inactive') order by status;

结果:-

id  |  status  | status_message | csr_id |
-----+----------+---------------+--------
150 | active   | test 9         |  1
134 | inactive | Test cmtt      |  1

预期结果:-

id  |  status  | status_message | csr_id |
-----+----------+---------------+--------
150 | active   | test 9         |  1

任何解决方案?

postgresql postgresql-9.3 postgresql-9.4 postgresql-9.2
1个回答
0
投票
那将是:
© www.soinside.com 2019 - 2024. All rights reserved.