无法在 postgres 中获取想要的查询
unable to fetch wanted querie in postgres
假设我有下一个数据
id cid vid firstname lastname dtype dname createdat
1 32 34 ramesh kjk t check 2021-11-02 10:00:51.66395
1 32 34 ramesh kjk t check 2021-11-01 10:00:51.66395
1 32 4 raj kjk c check 2021-11-04 10:00:51.66395
除外 o/p:
id cid vid firstname lastname dtype dname createdat
1 32 34 ramesh kjk t check 2021-11-02 10:00:51.66395
1 32 4 raj kjk c check 2021-11-04 10:00:51.66395
只需要获取每个用户的唯一一条最新记录(vid),文档类型可以是c或t
使用相关子查询
select t1.* from table_nam t1
where t1.createdat=(select max(createdat) from table_nam t2 where t1.vid=t2.vid)
你也可以使用 distinct on
select distinct on (vid) as vid,createdat,cid
from table_name
order by vid,createdat desc
假设我有下一个数据
id cid vid firstname lastname dtype dname createdat
1 32 34 ramesh kjk t check 2021-11-02 10:00:51.66395
1 32 34 ramesh kjk t check 2021-11-01 10:00:51.66395
1 32 4 raj kjk c check 2021-11-04 10:00:51.66395
除外 o/p:
id cid vid firstname lastname dtype dname createdat
1 32 34 ramesh kjk t check 2021-11-02 10:00:51.66395
1 32 4 raj kjk c check 2021-11-04 10:00:51.66395
只需要获取每个用户的唯一一条最新记录(vid),文档类型可以是c或t
使用相关子查询
select t1.* from table_nam t1
where t1.createdat=(select max(createdat) from table_nam t2 where t1.vid=t2.vid)
你也可以使用 distinct on
select distinct on (vid) as vid,createdat,cid
from table_name
order by vid,createdat desc