oracle 使用特定Order By子句随机化SQL输出

ajsxfq5m  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(101)

我有一个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
5uzkadbs

5uzkadbs1#

您已按member_nbr、serv_nbr对行进行了排序。相反,您希望这些member_nbr块的顺序是随机的。
为此,使用一个窗口函数,为每个成员提供一个随机数_nbr:

order by
  max(dbms_random.random) over (partition by member_nbr),
  serv_nbr;

演示:https://dbfiddle.uk/jD5vIKT5

ar7v8xwq

ar7v8xwq2#

尝试使用Floor(dbms_random.value(1,n))

with 
    tbl AS
        (   Select  SYSDATE - Floor(LEVEL / 4) "A_DATE", LEVEL "A_NUMBER",
                    SubStr('ABCDEFGHIJKLMNOP', LEVEL, 1) "A_LETTER",
                    Floor(dbms_random.value(1, 3)) "A_RAND"
            From Dual
            Connect By LEVEL <= 6
        )
Select  *
From    tbl
Order By A_DATE,  A_RAND, A_NUMBER, A_LETTER

/*
1st run
A_DATE      A_NUMBER A_LE     A_RAND
--------- ---------- ---- ----------
01-OCT-23          4 D             2
01-OCT-23          5 E             2
01-OCT-23          6 F             2
02-OCT-23          2 B             1
02-OCT-23          1 A             2
02-OCT-23          3 C             2

6 rows selected. 

2nd run
A_DATE      A_NUMBER A_LE     A_RAND
--------- ---------- ---- ----------
01-OCT-23          4 D             1
01-OCT-23          5 E             1
01-OCT-23          6 F             2
02-OCT-23          1 A             1
02-OCT-23          2 B             2
02-OCT-23          3 C             2

6 rows selected. 
*/

结果取决于随机数跨度(1,n)和数据中的差异。你也可以通过将随机数关联到你的某些列来调整它。

相关问题