gpt4 book ai didi

sql - SQL Server 中的伪随机可重复排序(不是 NEWID() 也不是 RAND())

转载 作者:行者123 更新时间:2023-12-02 08:36:18 24 4
gpt4 key购买 nike

我想以可重复的方式对结果进行随机排序,以达到分页等目的。为此,NEWID() 过于随机,无法重新获得相同的结果。按兰德(种子)排序将是理想的,因为使用相同的种子会产生相同的随机集合。不幸的是,Rand() 状态每行都会重置,有人有解决方案吗?

declare @seed as int;
set @seed = 1000;

create table temp (
id int,
date datetime)

insert into temp (id, date) values (1,'20090119')
insert into temp (id, date) values (2,'20090118')
insert into temp (id, date) values (3,'20090117')
insert into temp (id, date) values (4,'20090116')
insert into temp (id, date) values (5,'20090115')
insert into temp (id, date) values (6,'20090114')

-- re-seeds for every item
select *, RAND(), RAND(id+@seed) as r from temp order by r
--1 2009-01-19 00:00:00.000 0.277720118060575 0.732224964471124
--2 2009-01-18 00:00:00.000 0.277720118060575 0.732243597442382
--3 2009-01-17 00:00:00.000 0.277720118060575 0.73226223041364
--4 2009-01-16 00:00:00.000 0.277720118060575 0.732280863384898
--5 2009-01-15 00:00:00.000 0.277720118060575 0.732299496356156
--6 2009-01-14 00:00:00.000 0.277720118060575 0.732318129327415
-- Note how the last column is +=~0.00002

drop table temp

-- interestingly this works:
select RAND(@seed), RAND()
--0.732206331499865 0.306382810665955

注意,我尝试了 Rand(ID) 但结果只是排序。显然 Rand(n) < Rand(n+1)

最佳答案

基于 gkrogers 哈希建议构建,效果非常好。关于性能有什么想法吗?

declare @seed as int;
set @seed = 10;

create table temp (
id int,
date datetime)

insert into temp (id, date) values (1,'20090119')
insert into temp (id, date) values (2,'20090118')
insert into temp (id, date) values (3,'20090117')
insert into temp (id, date) values (4,'20090116')
insert into temp (id, date) values (5,'20090115')
insert into temp (id, date) values (6,'20090114')

-- re-seeds for every item
select *, HASHBYTES('md5',cast(id+@seed as varchar)) r
from temp order by r
--1 2009-01-19 00:00:00.000 0x6512BD43D9CAA6E02C990B0A82652DCA
--5 2009-01-15 00:00:00.000 0x9BF31C7FF062936A96D3C8BD1F8F2FF3
--4 2009-01-16 00:00:00.000 0xAAB3238922BCC25A6F606EB525FFDC56
--2 2009-01-18 00:00:00.000 0xC20AD4D76FE97759AA27A0C99BFF6710
--3 2009-01-17 00:00:00.000 0xC51CE410C124A10E0DB5E4B97FC2AF39
--6 2009-01-14 00:00:00.000 0xC74D97B01EAE257E44AA9D5BADE97BAF

drop table temp

编辑:请注意,如果使用动态 SQL,则在查询中使用的 @seed 声明可以替换为参数或常量 int。 (不需要以 TSQL 方式声明 @int)

关于sql - SQL Server 中的伪随机可重复排序(不是 NEWID() 也不是 RAND()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/458175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com