Netezza SQL For循环

时间:2015-04-06 21:13:38

标签: sql loops for-loop netezza

我有以下查询,我将调用query1:

with a as (
select customer_key as cust,
sum(sales)*1.0/4 as avg_sales
sum(returns)*1.0/4 as avg_return
count(distinct order_key)*1.04 as avg_num_orders
from orders_table
where purch_year between 2011 and 2014
group by cust
order by random()
),

b as (
select *
from a
where avg_num_orders > .25
limit 100000
)

select case 
       when avg_num_orders <= 1 then 'Low'
       when avg_num_orders between 1 and 4 then 'Medium'
       when avg_num_orders > 4 then 'High'
   end as estimated_frequency,
   count(cust) as num_purchasers_year,
   sum(avg_num_orders) as num_orders_year,
   avg(avg_num_orders) as avg_num_order_year,
   sum(avg_sales) as avg_sales_year,
   sum(avg_total_return) as avg_return_year,
   avg_sales_year/num_orders_year as AOV,
   avg_sales_year/num_purchasers_year as ACS,
   stddev(avg_sales) as sales_stddev
from b
where avg_num_orders > .25
group by estimated_frequency
order by  estimated_frequency;

我想编写执行以下操作的代码(这是不起作用的,我提供了伪代码)。我无权创建程序。

Create table temp1
for i in 1..100 loop
insert into temp1 the result of QUERY1
end loop

然后

select estimated_frequency,
       avg(acs),
       avg(sales_stddev)
from temp1
group by estimated_frequency 

基本上,我想运行query1 100次,并将结果存储在名为temp1的表中,然后在完成所有操作后计算temp1上的一些平均值。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我会将此添加为评论,但没有足够的代表。

我能看到的唯一选择是在Netezza之外执行此操作,并在批处理文件/ shell脚本/ Python脚本中编写循环/...

我尝试了以下操作,但请注意,这不起作用,因为随机数只生成一次然后重复使用,因此您可以获得100个相同的样本。

-- Test view which gives some random data from an existing table.
create view my_view as 
    select 
        m.*
    from my_table t
    join (
        select (floor(random()*10)+1)::integer rand_id -- assuming I have ids from 1 to 10
    ) x on x.rand_id = t.id;

create table results (id integer, data double precision);

insert into results
    select v.*
    from my_view v
    cross join table(generate_series(1,100));

Generate_series是用户定义的表函数,您可以从the Enzee Community website获取。