--!syntax_pg
--TPC-DS Q39

-- start query 1 in stream 0 using template ../query_templates/query39.tpl
with inv as
(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
       ,stdev,mean, case mean when 0::numeric then null::numeric else stdev/mean end cov
 from(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
            ,stddev_samp(inv_quantity_on_hand) stdev,avg(inv_quantity_on_hand) mean
      from plato.inventory
          ,plato.item
          ,plato.warehouse
          ,plato.date_dim
      where inv_item_sk = i_item_sk
        and inv_warehouse_sk = w_warehouse_sk
        and inv_date_sk = d_date_sk
        and d_year =1998
      group by w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy) foo
 where case mean when 0::numeric then 0::numeric else stdev/mean end > 1::numeric)
select inv1.w_warehouse_sk as inv1_w_warehouse_sk,inv1.i_item_sk as inv1_i_item_sk,inv1.d_moy as inv1_d_moy,inv1.mean as inv1_mean, inv1.cov as inv1_cov
        ,inv2.w_warehouse_sk as inv2_w_warehouse_sk,inv2.i_item_sk as inv2_i_item_sk,inv2.d_moy as inv2_d_moy,inv2.mean as inv2_mean, inv2.cov as inv2_cov
from inv inv1,inv inv2
where inv1.i_item_sk = inv2.i_item_sk
  and inv1.w_warehouse_sk =  inv2.w_warehouse_sk
  and inv1.d_moy=4
  and inv2.d_moy=4+1
order by inv1_w_warehouse_sk,inv1_i_item_sk,inv1_d_moy,inv1_mean,inv1_cov
        ,inv2_d_moy,inv2_mean, inv2_cov
;
with inv as
(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
       ,stdev,mean, case mean when 0::numeric then null::numeric else stdev/mean end cov
 from(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
            ,stddev_samp(inv_quantity_on_hand) stdev,avg(inv_quantity_on_hand) mean
      from plato.inventory
          ,plato.item
          ,plato.warehouse
          ,plato.date_dim
      where inv_item_sk = i_item_sk
        and inv_warehouse_sk = w_warehouse_sk
        and inv_date_sk = d_date_sk
        and d_year =1998
      group by w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy) foo
 where case mean when 0::numeric then 0::numeric else stdev/mean end > 1::numeric)
select inv1.w_warehouse_sk as inv1_w_warehouse_sk,inv1.i_item_sk as inv1_i_item_sk,inv1.d_moy as inv1_d_moy,inv1.mean as inv1_mean, inv1.cov as inv1_cov
        ,inv2.w_warehouse_sk as inv2_w_warehouse_sk,inv2.i_item_sk as inv2_i_item_sk,inv2.d_moy as inv2_d_moy,inv2.mean as inv2_mean, inv2.cov as inv2_cov
from inv inv1,inv inv2
where inv1.i_item_sk = inv2.i_item_sk
  and inv1.w_warehouse_sk =  inv2.w_warehouse_sk
  and inv1.d_moy=4
  and inv2.d_moy=4+1
  and inv1.cov > 1.5::numeric
order by inv1_w_warehouse_sk,inv1_i_item_sk,inv1_d_moy,inv1_mean,inv1_cov
        ,inv2_d_moy,inv2_mean, inv2_cov
;

-- end query 1 in stream 0 using template ../query_templates/query39.tpl
