我有一个场景:
这是我的查询:
select id, empid, dt,ROW_NUMBER() OVER(PARTITION BY id ORDER BY dt) from
(select 21919 id,219 empid,'2018-06-01 08:41:59' dt
union all
select 21919,219,'2018-06-01 08:43:29'
union all
select 21919,219,'2018-06-01 08:50:02'
union all
select 21919,88,'2018-06-01 08:56:45'
union all
select 21919,20,'2018-06-01 11:25:55'
union all
select 21919,20,'2018-06-01 12:04:36'
union all
select 21919,88,'2018-06-01 12:05:17'
union all
select 21919,132,'2018-06-01 12:18:27'
union all
select 21919,132,'2018-06-04 13:34:53') as a
和我的结果:
但是我需要这样的结果:
在Postgres中可以做到吗?
任何建议都值得赞赏。
答案 0 :(得分:0)
如果我正确地阅读了您的任务,它会像添加用于分组的属性一样简单?..
select id, empid, dt,ROW_NUMBER() OVER(PARTITION BY id ORDER BY dt,empid) from
(select 21919 id,219 empid,'2018-06-01 08:41:59' dt
union all
select 21919,219,'2018-06-01 08:43:29'
union all
select 21919,219,'2018-06-01 08:50:02'
union all
select 21919,88,'2018-06-01 08:56:45'
union all
select 21919,20,'2018-06-01 11:25:55'
union all
select 21919,20,'2018-06-01 12:04:36'
union all
select 21919,88,'2018-06-01 12:05:17'
union all
select 21919,132,'2018-06-01 12:18:27'
union all
select 21919,132,'2018-06-04 13:34:53') as a