随机选择Netezza

时间:2017-09-29 07:41:51

标签: sql random netezza

我需要Netezza随机选择的帮助。

我有4个客户,每个客户都有一些变量选项。

客户|变量

1    |     4
1    |     5
1    |     6
2    |     10
2    |     12
2    |     16
3    |     1
3    |     2 
3    |     3
3    |     6
4    |     5
4    |     8

我需要使用随机选择为每个客户选择一个选项。 解决方案示例:

客户|变量

1    |     5
2    |     10
3    |     3
4    |     8

谢谢!

1 个答案:

答案 0 :(得分:1)

IBM Netezza提供内置的random()函数Random number functions,您可以使用它来获取所需的输出。以下解决方案在NPS Release 7.2.1.0

上进行了测试

测试数据:

create table TEST 
(
Customer int,
Variable int

)
distribute on random;

insert into TEST values(1,4);
insert into TEST values(1,5);
insert into TEST values(1,6);
insert into TEST values(2,10);
insert into TEST values(2,12);
insert into TEST values(2,16);
insert into TEST values(3,1);
insert into TEST values(3,2 );
insert into TEST values(3,3);
insert into TEST values(3,6);
insert into TEST values(4,5);
insert into TEST values(4,8);

<强>解决方案:

    SELECT CUSTOMER
        ,max(VARIABLE) AS VARIABLE
    FROM (
        SELECT CUSTOMER 
            ,first_value(VARIABLE) OVER (
                PARTITION BY CUSTOMER ORDER BY random()
                ) AS VARIABLE

        FROM TEST
        ) a

GROUP BY CUSTOMER
ORDER BY 1;

输出迭代1:

CUSTOMER VARIABLE
1           5
2           16
3           2
4           8

输出迭代2:

CUSTOMER VARIABLE
1           6
2           10
3           1
4           5