我正在努力为搜索结果页面构建一个复杂的排序算法。
我想通过评分(评分计数,平均评分)来订购我的商品,但我只希望评级在结果页面的60-80%之间。一页有12个项目。它们应该在页面上随机分发。
我想将简单排序应用为辅助条件,例如created_at
字段。
有人知道该怎么做吗?
答案 0 :(得分:0)
我对您的要求的解释是:
为了实现这一目标,我尝试了以下方法:
select *
from
(
select a.*,
sum(
/* We want the total number of items that meet both criteria */
/* For every one of these items, we want to include an extra row */
case when created_row_num <= 4 and rating_row_num <= 8
then 1
else 0
end ) over() as num_duplicates
from (
select ratings.*,
row_number() over( order by created_at desc ) as created_row_num,
row_number() over( order by avg_rating desc ) as rating_row_num
from ratings
) as a
) as b
where created_row_num <= 4
/* Get top 8 by rating, plus 1 for every record that has already been selected by creation date */
or rating_row_num <= 8 + num_duplicates
答案 1 :(得分:0)
我最终使用了一种解决方案,其中包括未评级商品最终会在评级商品中间结束的可能性。该算法的思想如下:
ORDER BY
CASE WHEN rating IS NOT NULL OR RANDOM() < 0.0x THEN 1 + RANDOM()ELSE RANDOM() END
DESC NULLS LAST