创建临时测试表脚本如下:
CREATE MULTISET VOLATILE TABLE temprecommend
(
item_id DECIMAL(18,0),
rankScore DECIMAL(3,2),
sourcingTaskId BYTEINT
)
PRIMARY INDEX ( item_id )
ON COMMIT PRESERVE ROWS;
初始化数据脚本如下:
insert into temprecommend (item_id,rankScore,sourcingTaskId) values(152241683582,7.50,116);
insert into temprecommend (item_id,rankScore,sourcingTaskId) values(152241683581,7.10,116);
insert into temprecommend (item_id,rankScore,sourcingTaskId) values(152055889769,7.30,115);
insert into temprecommend (item_id,rankScore,sourcingTaskId) values(152004277613,7.20,115);
insert into temprecommend (item_id,rankScore,sourcingTaskId) values(152241683585,7.40,116);
SQL 如下:
select * from
(select row_number() over (partition by sourcingtaskid order by rankscore desc) as num,item_id,rankScore,sourcingTaskId
from temprecommend) as data
where data.num = 1
解释:1.下面一句实现按sourcingtaskid分组,并按rankscore 排序
select row_number() over (partition by sourcingtaskid order by rankscore desc) as num,item_id,rankScore,
sourcingTaskId from temprecommend
select * from
(select row_number() over (partition by sourcingtaskid order by rankscore desc) as num,item_id,rankScore,sourcingTaskId
from temprecommend) as data
where data.num = 1