1

我正在尝试将某种唯一标识符 (uid) 添加到由成对行组成的分区中,即为大小 = 2 行的窗口分区中的 (identifier1,identifier2) 的每两行生成一些 uid/tag。

因此,例如,ID X 的前 2 行将获得 uid A,相同 ID 的接下来两行将获得 uid B,如果在 ID X 的分区中只剩下一行,它将获得 id C。

这是我想要完成的,图片说明了表的结构,我手动添加了 expectedIdentifier 来说明目标:

在此处输入图像描述

这是我当前的 SQL,ntile 没有解决它,因为分区大小不同:

select
rowId
, ntile(2) over (partition by firstIdentifier, secondIdentifier order by timestamp asc) as ntile
, *
from log;

已经尝试过 ntile( (count(*) over partition...) / 2),但这不起作用。

可以使用 md5() 或类似方法生成 UID,但我无法如上所示标记行(因此我可以 md5 生成的标记/uid)

4

2 回答 2

1

虽然 Snowflake 窗口函数中不支持 count(*),但支持 count(1) 并可用于创建唯一标识符。下面是一个整数唯一 ID 匹配行对并处理“奇数”行组的示例:

select 
ntile(2) over (partition by firstIdentifier, secondIdentifier order by timestamp asc) as ntile
,ceil(count(1) over( partition by firstIdentifier, secondIdentifier order by timestamp asc) / 2) as id
, *
from log;
于 2018-05-10T18:22:23.627 回答
0
select *, char(65 + (row_number() over(partition by 
firstidentifier,secondidentifier order by timestamp)-1)/2) 
expectedidentifier from log 
order by firstidentifier, timestamp

这是 Sql 服务器版本

with log (firstidentifier,secondidentifier, timestamp)
as (
select 15396, 14460, 1 union all
select 15396, 14460, 1 union all
select 19744, 14451, 1 union all
select 19744, 14451, 1 union all
select 19744, 14451, 1 union all
select 15590, 12404, 1 union all
select 15590, 12404, 1 union all
select 15590, 12404, 1 union all
select 15590, 12404, 1 union all
select 15590, 12404, 1 
)
select *, char(65 + (row_number() over(partition by 
firstidentifier,secondidentifier order by timestamp)-1)/2) 
expectedidentifier from log 
order by firstidentifier,secondidentifier,timestamp
于 2018-05-10T18:40:08.417 回答