如何在FROM子句查询中找到特定id的替代值?

时间:2017-12-08 06:09:15

标签: sql oracle

请查看详细要求,详情如下。给定的查询给出了查询A中可用的quote_revision_id的结果,但如果查询中不存在quote_revivsion_id但是查询A以外的其他quote_reivsion_id则显示那些quote_revision_id的所有细节。

我已经在我的预期结果中提到过,比如给定的查询,我需要Quote_revision_id 5& 5的结果。 6但是根据我给出的查询,这些记录还没有到来。

Oracle版本:12.1.0

请在下面找到详细要求和必要的详细信息,

表格

create table t1 (quote_revision_id number, qty number,item_parts 
varchar2(100),cost number, resale number, item_tag varchar2(10))
/
create table t2 (opportunity_id number,rebate number)
/
create table t3 (revision_id number,quote_id number)
/
create table t4 (quote_id number,opportunity_id number)
/
create table t5 (opportunity_id number,shipping_fee_taxes number,
agent_commissions number, total_program_costs number, service_fees number)

/

插入

insert into t1 values (1,10,'P1',100,100,null)
/
insert into t1 values (2,20,'P2',110,110,null)
/
insert into t1 values (3,10,'P3',120,120,null)
/
insert into t1 values (4,30,'P4',130,130,null)
/
insert into t1 values (5,20,'P5',140,null,null)
/   
insert into t1 values (6,10,'P6',150,150,'5.0')
/

insert into t2 values (100,10)
/
insert into t2 values (101,20)
/
insert into t2 values (102,10)
/
insert into t2 values (103,0)
/
insert into t2 values (104,20)
/
insert into t2 values (105,10)
/

insert into t3 values (1,10)
/
insert into t3 values (2,20)
/
insert into t3 values (3,30)
/
insert into t3 values (4,40)
/
insert into t3 values (5,50)
/
insert into t3 values (6,60)
/

insert into t4 values (10,100)
/  
insert into t4 values (20,101)
/
insert into t4 values (30,102)
/
insert into t4 values (40,103)
/
insert into t4 values (50,104)
/
insert into t4 values (60,105)
/

insert into t5 values (100,1,2,3,4)
/
insert into t5 values (101,1,2,3,4)
/
insert into t5 values (102,1,2,3,4)
/
insert into t5 values (103,1,2,3,4)
/
insert into t5 values (104,1,2,3,4)
/
insert into t5 values (105,1,2,3,4)
/

commit
/

查询:

SELECT ROUND(SUM(NVL(a.total_resale,0)),2) total_resale,
     ROUND(SUM(NVL(a.total_cost,0)),2) total_cost,
     ROUND(SUM(nvl(e.total_adjustments,0)),2) total_adjustments,
       a.quote_revision_id,
     CASE WHEN SUM(NVL(a.total_resale,0)) <> 0  THEN
        ROUND(((SUM(NVL(a.total_resale,0)) -
               SUM(NVL(a.total_cost,0))) /
               SUM(NVL(a.total_resale,0))*100),2)
        ELSE 0
     END parts_margin,
     ROUND(SUM(nvl(a.net_margin,0)),2) net_margin,
     SUM(nvl(c.total_no_of_parts_requested,0))  total_no_of_parts_requested,
     SUM(nvl(d.total_no_of_parts_quoted,0)) total_no_of_parts_quoted,
     (SUM(nvl(c.total_no_of_parts_requested,0))- 
SUM(nvl(d.total_no_of_parts_quoted,0))) total_no_of_part_not_quoted
FROM
(SELECT  SUM(NVL(t1.resale,0) * NVL(t1.qty,0)) total_resale,
              SUM(NVL(t1.cost,0) * NVL(t1.qty,0)) total_cost,
              t1.quote_revision_id quote_revision_id,
              CASE WHEN (SUM(nvl(t1.resale,0) * nvl(t1.qty,0)) +
                         SUM(nvl(t5.service_fees,0))) <> 0 THEN
                (((SUM(NVL(t1.resale,0) * NVL(t1.qty,0)) +
                    SUM(NVL(t5.service_fees,0))) -
                    (SUM(NVL(t1.cost,0) * NVL(t1.qty,0)) +
                     (SUM(NVL(t2.rebate,0) + NVL(t5.shipping_fee_taxes,0) +
                         NVL(t5.agent_commissions,0) + 
    NVL(t5.total_program_costs,0)))- (sum(nvl(t5.service_fees,0))))) /
                  (SUM(NVL(t1.resale,0) * NVL(t1.qty,0)) +
                   SUM(nvl(t5.service_fees,0))) *100) 
              ELSE 0 END net_margin
         FROM t1 ,
              t2,
              t3,
              t4,
              t5
        WHERE t1.quote_revision_id = t3.revision_id AND
              t3.quote_id = t4.quote_id AND
              t2.opportunity_id = t4.opportunity_id AND
              t5.opportunity_id = t4.opportunity_id AND
              t1.resale is not null AND
              ((t1.item_tag <> '5.0' and t1.item_tag <> 5) or (t1.item_tag 
       is null)) --added constraint for excluding parts with item_tag 5 for           
       both alphanumeric and number values of item tag
     GROUP BY t1.quote_revision_id)a,
     (SELECT  t2.opportunity_id,
              MAX(t1.quote_revision_id) quote_revision_id
        FROM  t2,
              t4,
              t3,
              t1
       WHERE  t2.opportunity_id = t4.opportunity_id AND
              t4.quote_id = t3.quote_id AND
              t3.revision_id = t1.quote_revision_id   
    GROUP BY  t2.opportunity_id) b,
    (SELECT  t1.quote_revision_id,
              COUNT(t1.item_parts) total_no_of_parts_requested
        FROM  t1
    GROUP BY  t1.quote_revision_id) c,
    (SELECT  t1.quote_revision_id,
            COUNT(t1.item_parts)
         total_no_of_parts_quoted
        FROM  t1
        where t1.COST is not null
        and t1.RESALE is not null
    GROUP BY  t1.quote_revision_id) d,
            (SELECT  t2.opportunity_id,
             ((SUM(NVL(t2.rebate,0) +
                  NVL(t5.shipping_fee_taxes,0) +
                  NVL(t5.agent_commissions,0) +
                  NVL(t5.total_program_costs,0))) -
              SUM(nvl(t5.service_fees,0))) total_adjustments
              FROM  t2,
              t5
        where t2.opportunity_id = t5.opportunity_id
    GROUP BY  t2.opportunity_id
    ) e

  where a.quote_revision_id  = b.quote_revision_id AND      
   c.quote_revision_id(+) = b.quote_revision_id AND
   d.quote_revision_id(+) = b.quote_revision_id AND
   e.opportunity_id(+) = b.opportunity_id
 group by a.quote_revision_id
  order by a.quote_revision_id;  

预期结果:

+--------+------+------+--------------+----+--------+----+----+-----+
| RESALE | COST | Adj. | Quote_Rev_id | PM |   NM   | PR | PQ | PNQ |
+--------+------+------+--------------+----+--------+----+----+-----+
|   1000 | 1000 |   12 |            1 |  0 |   -0.8 |  1 |  1 |   0 |
|   2200 | 2200 |   22 |            2 |  0 |  -0.82 |  1 |  1 |   0 |
|   1200 | 1200 |   12 |            3 |  0 |  -0.66 |  1 |  1 |   0 |
|   3900 | 3900 |    2 |            4 |  0 |   0.05 |  1 |  1 |   0 |
|      0 |    0 |    0 |            5 |  0 |      0 |  1 |  0 |   1 |
|      0 |    0 |    0 |            6 |  0 |      0 |  1 |  1 |   0 |
+--------+------+------+--------------+----+--------+----+----+-----+

实际结果:

+--------+------+------+--------------+----+-------+----+----+-----+
| RESALE | COST | Adj. | Quote_Rev_id | PM |  NM   | PR | PQ | PNQ |
+--------+------+------+--------------+----+-------+----+----+-----+
|   1000 | 1000 |   12 |            1 |  0 |  -0.8 |  1 |  1 |   0 |
|   2200 | 2200 |   22 |            2 |  0 | -0.82 |  1 |  1 |   0 |
|   1200 | 1200 |   12 |            3 |  0 | -0.66 |  1 |  1 |   0 |
|   3900 | 3900 |    2 |            4 |  0 |  0.05 |  1 |  1 |   0 |
+--------+------+------+--------------+----+-------+----+----+-----+

请建议如何从给定查询中获得预期结果。

由于

0 个答案:

没有答案