假设我有一张桌子:
HH SLOT RN
--------------
1 1 null
1 2 null
1 3 null
--------------
2 1 null
2 2 null
2 3 null
我想把RN设为介于1到10之间的随机数。这个数字可以在整个表中重复,但是在任何给定的HH中重复这个数字是不好的。例如:
HH SLOT RN_GOOD RN_BAD
--------------------------
1 1 9 3
1 2 4 8
1 3 7 3 <--!!!
--------------------------
2 1 2 1
2 2 4 6
2 3 9 4
这是在Netezza,如果它有任何不同。这对我来说是个大麻烦。提前感谢!
发布于 2013-08-05 20:02:05
嗯,我找不到一个好办法,所以我做了一个黑客:
rand_inst
的新整数字段。rand_inst
更新为这个家庭中该随机数的实例号。例如,如果我得到两个3,那么第二个3将rand_inst
设置为2。rand_inst>1
所在的任何地方分配不同的随机数。看上去是这样的。太懒散了,所以名字和我原来的帖子有点不同:
/* Iterative hack to fill 6 slots with a random number between 1 and 13.
A random number *must not* repeat within a household_id.
*/
update c3_lalfinal a
set a.rand_inst = b.rnum
from (
select household_id
,slot_nbr
,row_number() over (partition by household_id,rnd order by null) as rnum
from c3_lalfinal
) b
where a.household_id = b.household_id
and a.slot_nbr = b.slot_nbr
;
update c3_lalfinal
set rnd = CAST(0.5 + random() * (13-1+1) as INT)
where rand_inst>1
;
/* Repeat until this query returns 0: */
select count(*) from (
select household_id from c3_lalfinal group by 1 having count(distinct(rnd)) <> 6
) x
;
发布于 2013-07-31 01:40:55
要在1和hh中的行数之间获得一个随机数,可以使用:
select hh, slot, row_number() over (partition by hh order by random()) as rn
from t;
更大范围的值更具有挑战性。下面的表(称为randoms
)具有相同范围内的数字和随机位置。然后使用slot
索引位置,并从randoms
表中提取随机数:
with nums as (
select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9
),
randoms as (
select n, row_number() over (order by random()) as pos
from nums
)
select t.hh, t.slot, hnum.n
from (select hh, randoms.n, randoms.pos
from (select distinct hh
from t
) t cross join
randoms
) hnum join
t
on t.hh = hnum.hh and
t.slot = hnum.pos;
这里是一个在Postgres中演示这一点的SQLFiddle,我认为它与Netezza非常接近,具有匹配的语法。
发布于 2013-07-31 01:38:24
我不是SQL方面的专家,但可能会这样做:
https://stackoverflow.com/questions/17960138
复制相似问题