Oracle中的复杂查询出现问题

时间:2010-12-08 09:43:13

标签: sql oracle join group-by

SELECT a.coupon_upc,a.coup_desc,a.coup_dep,a.manuf,a.coupon_type,r.hh_red,r.red_qty, 
       r.red_val,a.coups_mailed,a.households_mailed
FROM

       (SELECT coupon_upc,
               min(coupon_description) coup_desc,
               min(campaign_manufacturer_desc) manuf, 
               min(coupon_department) coup_dep,
               coupon_type,
               sum(households_red) hh_red,
               sum(coup_qty) red_qty,
               sum(redeem_value) red_val
          FROM lcm_eval.lcm_compare_redeemed_cell r,
               dim_campaign c
         WHERE r.campaign_id = c.campaign_id
           and c.campaign_code = '&LCMXXXX' 
           and r.cell_key in (41) 
         group
            by coupon_upc,
               coupon_type) r,

        (SELECT /*+ use_hash(a b c) */
                d.coupon_upc,
                d.coup_desc,
                d.manuf,d.coup_dep,
                d.coupon_type,
                sum(a.counts) coups_mailed,
                sum(a.households) households_mailed
          FROM lcm_eval.lcm_current_coupon_alloc_cell a,
               dim_campaign c,
               (SELECT distinct
                       coupon_upc,
                       min(coupon_description) coup_desc,
                       min(campaign_manufacturer_desc) manuf, 
                       min(coupon_department) coup_dep,
                       coupon_type
                  FROM contact_coupon_details
                 WHERE schedule_key = '23'
                 group by coupon_upc,coupon_type) d
         WHERE a.campaign_id = c.campaign_id
           and c.campaign_code = '&LCMXXXX' code
           and a.cell_key in (41) 
           and a.coupon_upc = d.coupon_upc
         group by d.coupon_upc, d.coup_desc, d.manuf, d.coup_dep, d.coupon_type) a
 WHERE a.coupon_upc = r.coupon_upc (+)
 ORDER by a.coupon_type;

我需要总结的一栏是(r.red_qty / a.households_mailed)。这必须以(cellkey,a.coupon_type)为一组。即(cellkey,a.coupon_type)的每个组合(r.red_qty / a.households_mailed)的值是什么。

有人可以建议单个SQL执行相同的操作或匿名阻止吗?

2 个答案:

答案 0 :(得分:2)

您可以将查询包装在另一个选择查询中,该查询将按cellkeycoupon_type列执行摘要。

示例:

SELECT 
    cellkey, coupon_type, SUM(red_qty)/SUM(households_mailed) summary
FROM 
    (<select query you posted>)
GROUP BY
    cellkey, coupon_type

答案 1 :(得分:0)

我认为您正在搜索rollup/cube

如果你想过滤掉一些你不关心的细节线,你可以将它包装在另一个选择中。

with r as
     (select 1 red_qty , 55 cell_key , 123 coupon_upc from dual 
       union
        select 2 red_qty , 66 cell_key , 123 coupon_upc  from dual 
       union
        select 3 red_qty , 55 cell_key , 345 coupon_upc  from dual 
       union
        select 4 red_qty , 66 cell_key , 345 coupon_upc  from dual
       union
        select 4 red_qty , 77 cell_key , 345 coupon_upc  from dual      
       union
        select 5 red_qty , 55 cell_key , 678 coupon_upc  from dual 
       union
        select 6 red_qty , 77 cell_key , 678 coupon_upc  from dual
      union
        select 6 red_qty , 88 cell_key , 678 coupon_upc  from dual      
        ),
        a as(
      select 'aa' coupon_type , 50  households_mailed , 123 coupon_upc from dual
        union
        select 'bb' coupon_type , 100 households_mailed , 345 coupon_upc from dual
        union
        select 'cc' coupon_type , 200 households_mailed , 678 coupon_upc from dual
        )
       select (sum(r.red_qty)/ sum(a.households_mailed)) rqty_hhmailed_ratio,
                r.cell_key, 
                  a.coupon_type
          from r
               inner join
                 a on r.coupon_upc = a.coupon_upc
            group by rollup (r.cell_key, a.coupon_type)

导致

RQTY_HHMAILED_RATIO    CELL_KEY               COUPON_TYPE 
---------------------- ---------------------- ----------- 
0.02                   55                     aa          
0.03                   55                     bb          
0.025                  55                     cc          
0.02572857142857142857 55                                 
0.04                   66                     aa          
0.04                   66                     bb          
0.04                   66                                 
0.04                   77                     bb          
0.03                   77                     cc          
0.03333333333333333333 77                                 
0.03                   88                     cc          
0.03                   88                                 
0.031