我的桌子
id updateddate updatedbyuser department
-------------------------------------------------------
40715449759 12/7/2018 12:24 1260 IB
40715449759 12/8/2018 10:40 1203 ESCALATION
40715449759 12/8/2018 10:40 task
40715449759 12/9/2018 9:58 client
40715449759 12/9/2018 12:09 300050282 ESCALATION
42058447691 12/23/2018 16:44 1712 TASK
42058447691 12/26/2018 19:33 4700 ESCALATION
42058447691 12/27/2018 17:26 7357 ESCALATION
输出
id first_action_date
--------------------------------
40715449759 12/8/2018 10:40
42058447691 12/26/2018 19:33
我正在尝试从mytable提取id和first_action_time,其中updatedate的第一个实例是first_action_date,部门是ESCALATION以获取唯一ID
答案 0 :(得分:1)
只需使用min
作为
select id,
min(first_action_date)
as first_action_date
from mytable
where department = 'ESCALATION'
group by id
答案 1 :(得分:1)
Postgres具有一个称为distinct on
的功能,可以在这种情况下使用:
select distinct on (id) id, first_action_date
from t
where department = 'ESCALATION'
order by id, first_action_date asc;
尽管您的特定查询可以直接由group by
处理,但是如果您想要其他列(例如用户),这将很方便。要获取整行:
select distinct on (id) t.*
from t
where department = 'ESCALATION'
order by id, first_action_date asc;
答案 2 :(得分:0)
select id, updateddate as first_action_date from table where department= 'ESCALATION' order by id