首页 > 解决方案 > 如何仅显示列 postgres 中的一个值

问题描述

我有一个具有三种状态的日期:打开、关闭、修复。但是,我试图仅显示 Open 状态值。我尝试使用 where 子句,但它不起作用。(注意:当使用 where 子句时,会有多个我不想显示的打开状态)。以下是我查询后的当前数据:

在此处输入图像描述

这是来自以下答案的查询

在此处输入图像描述

 select distinct on("id")*,
 status
 from ticket
 ORDER BY "id",CREATED DESC NULLS LAST

标签: sqlpostgresql

解决方案


我推测您想获得id最新状态所在的票(s)open- 并且只有那些票。如果是这样,distinct on请在子查询中使用并随后过滤:

select t.*
from (select distinct on (id) t.*,
      from ticket t
      order by id, created desc nulls last
     ) t
where t.status = 'open'

推荐阅读