我有一个SQL查询(在这个例子中修改得更小),我想让结果随机化,但仍然使用特定的Order By子句。
with STAGE2 as ( select /*+ FULL(ip) FULL(sx) MATERIALIZE PARALLEL(4) */ distinct
sx.claim_nbr,
sx.member_nbr
from
mem_process ip
left outer join visit_x sx on ip.claim_nbr = sx.claim_nbr
where
( (ip.business_unit = 'NC' and ip.carrier = 'MD' and ip.bank = '01'))
and ip.paid = 'Y'
and sx.paid = 'Y'
and substr(sx.claim_nbr,7,1) not in ('5', '6', '7', '8')
and substr(sx.serv_nbr,15,2) = '00'
)
select distinct
STAGE1.member_nbr,
STAGE1.claim_nbr, STAGE1.serv_nbr, STAGE1.ymdeff, STAGE1.ymdend, STAGE1.ymdpaid,
STAGE1.carrier, STAGE1.bu, STAGE1.bank, STAGE1.PROG_NBR, STAGE1.REGION
from (
select
sx.member_nbr,
sx.claim_nbr, sx.serv_nbr, sx.ymdeff, sx.ymdend, sx.ymdpaid,
ip.carrier, ip.business_unit as bu, ip.bank, ip.prog_nbr, ip.region
from
STAGE2
left outer join mem_process ip on STAGE2.claim_nbr = ip.claim_nbr
left outer join visit_x sx on ip.claim_nbr = sx.claim_nbr
where
ip.paid = 'Y'
and sx.paid = 'Y'
and length(sx.ymdpaid) = 8
) STAGE1
order by
STAGE1.member_nbr,
STAGE1.serv_nbr
;
是否可以以随机顺序返回行,但仍然保留“member_nbr”和“serv_nbr”顺序?我想试试兰德(),但我不能让它工作。
以下是我目前收到的订单:
member-a, serv-1-1
member-a, serv-1-2
member-a, serv-2-1
member-a, serv-2-2
member-a, serv-3-1
member-b, serv-4-1
member-b, serv-4-2
member-b, serv-5-1
member-c, serv-6-1
member-c, serv-7-1
member-h, serv-8-1
member-h, serv-8-2
member-z, serv-10-1
member-z, serv-10-2
member-z, serv-10-3
member-z, serv-11-1
这就是我想要的随机顺序
member-c, serv-6-1
member-c, serv-7-1
member-b, serv-4-1
member-b, serv-4-2
member-b, serv-5-1
member-z, serv-10-1
member-z, serv-10-2
member-z, serv-10-3
member-z, serv-11-1
member-a, serv-1-1
member-a, serv-1-2
member-a, serv-2-1
member-a, serv-2-2
member-a, serv-3-1
member-h, serv-8-1
member-h, serv-8-2
2条答案
按热度按时间5uzkadbs1#
您已按member_nbr、serv_nbr对行进行了排序。相反,您希望这些member_nbr块的顺序是随机的。
为此,使用一个窗口函数,为每个成员提供一个随机数_nbr:
演示:https://dbfiddle.uk/jD5vIKT5
ar7v8xwq2#
尝试使用Floor(dbms_random.value(1,n))
结果取决于随机数跨度(1,n)和数据中的差异。你也可以通过将随机数关联到你的某些列来调整它。