select   
   sum(quan) sum_quan,null sum_hsl,
    null sum_qsf,sum(mtquan*quan/100) sum_sfl,
   from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008' and t1.month >='1' and t1.month <='03' and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0
     group by bd_coal_id,outtype
临时表A  select 
sum(quan) quan,null hsl,
  from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008'        and  t1.month >='04' and t1.month <='06'and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0
     group by bd_coal_id,outtype 
临时表B 
欲返回表A 表B所有字段和行,怎么写连接或者子查询
说的不是很清楚希望高手们说说思路。

解决方案 »

  1.   

    select bd_coal_id, outtype, sum_quan,quan,sum_hsl,hsl
      from
    (
    select  
      sum(quan) sum_quan,null sum_hsl, 
        null sum_qsf,sum(mtquan*quan/100) sum_sfl, 
      from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008' and t1.month >='1' and t1.month <='03' and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0 
      group by bd_coal_id,outtype 
    ) temp_a a 
      natural join
    (
     select 
    sum(quan) quan,null hsl, 
      from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008'        and  t1.month >='04' and t1.month <='06'and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0 
      group by bd_coal_id,outtype 
    ) temp_b b
      

  2.   

    select a.*,b.*
    from(
       select  
             sum(quan) sum_quan,null sum_hsl, 
             null sum_qsf,sum(mtquan*quan/100) sum_sfl, 
      from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008' and t1.month >='1' and t1.month <='03' and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0 
      group by bd_coal_id,outtype 
    )a
    natural join --这里是自然连接,自动将表A与表B的记录匹配在一起
    select
    from(
      select 
             sum(quan) quan,null hsl, 
      from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008' and  t1.month >='04' and t1.month <='06'and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0 
      group by bd_coal_id,outtype 
    )b
      

  3.   

    才看到你是返回表A跟表B所有行吧,那么就用full join全连接吧:select a.*,b.*
    from(
       select  
             sum(quan) sum_quan,null sum_hsl, 
             null sum_qsf,sum(mtquan*quan/100) sum_sfl, 
      from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008' and t1.month >='1' and t1.month <='03' and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0 
      group by bd_coal_id,outtype 
    )a
    full join --应该用全连接啊,这是返回所有行的
    select
    from(
      select 
             sum(quan) quan,null hsl, 
      from zk_cq_siftproduct t1 inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id where t1.year='2008' and  t1.month >='04' and t1.month <='06'and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0 
      group by bd_coal_id,outtype 
    )b
    on a.bd_coal_id=b.bd_coal_id
      

  4.   

    SELECT SUM (quan) sum_quan, NULL sum_hsl, NULL sum_qsf,
           SUM (mtquan * quan / 100) sum_sfl
      FROM zk_cq_siftproduct t1 INNER JOIN zk_cq_siftproduct_b t2 ON t1.cq_siftproduct_id =
                                                                       t2.cq_siftproduct_id
     WHERE t1.YEAR = '2008'
       AND t1.MONTH BETWEEN '01' AND '03'
       AND NVL (t1.dr, 0) = 0
       AND NVL (t2.dr, 0) = 0
    UNION ALL
    SELECT   SUM (quan) quan, NULL hsl, NULL, NULL
        FROM zk_cq_siftproduct t1 INNER JOIN zk_cq_siftproduct_b t2 ON t1.cq_siftproduct_id =
                                                                         t2.cq_siftproduct_id
       WHERE t1.YEAR = '2008'
         AND t1.MONTH BETWEEN '04' AND '06'
         AND NVL (t1.dr, 0) = 0
         AND NVL (t2.dr, 0) = 0
    GROUP BY bd_coal_id, outtype;
      

  5.   

    说的确实不是很清楚,有3楼和5楼的两种理解方式,看你要的是那种了,  3楼的SQL要改下。  
    同时你的LZ的SQL有问题(你编译过了吗??)  FROM 前面还有","(逗号)存在呢!SELECT A.SUM_QUAN, A.SUM_HSL, A.SUM_QSF, A.SUM_SFL, B.QUAN, B.HSL
      FROM (SELECT BD_COAL_ID,
                   OUTTYPE,
                   
                   SUM(QUAN) SUM_QUAN,
                   NULL SUM_HSL,
                   NULL SUM_QSF,
                   SUM(MTQUAN * QUAN / 100) SUM_SFL
              FROM ZK_CQ_SIFTPRODUCT T1
             INNER JOIN ZK_CQ_SIFTPRODUCT_B T2 ON T1.CQ_SIFTPRODUCT_ID =
                                                  T2.CQ_SIFTPRODUCT_ID
             WHERE T1.YEAR = '2008'
               AND T1.MONTH >= '1'
               AND T1.MONTH <= '03'
               AND NVL(T1.DR, 0) = 0
               AND NVL(T2.DR, 0) = 0
             GROUP BY BD_COAL_ID, OUTTYPE) A
      FULL JOIN --应该用全连接啊,这是返回所有行的
    SELECT FROM (SELECT BD_COAL_ID, OUTTYPE, SUM(QUAN) QUAN, NULL HSL
                   FROM ZK_CQ_SIFTPRODUCT T1
                  INNER JOIN ZK_CQ_SIFTPRODUCT_B T2 ON T1.CQ_SIFTPRODUCT_ID =
                                                       T2.CQ_SIFTPRODUCT_ID
                  WHERE T1.YEAR = '2008'
                    AND T1.MONTH >= '04'
                    AND T1.MONTH <= '06'
                    AND NVL(T1.DR, 0) = 0
                    AND NVL(T2.DR, 0) = 0
                  GROUP BY BD_COAL_ID, OUTTYPE) B ON A.BD_COAL_ID =
                                                     B.BD_COAL_ID
                                                 AND A.OUTTYPE = B.OUTTYPE
      

  6.   

    看你的意思,把两句sql语句合起来,不知道是否达到你的要求?select  sum(quan) sum_quan,null sum_hsl, null sum_qsf,null hsl
    ,sum(case when t1.month >='01' and t1.month <='03' then mtquan*quan/100) else 0 end) sum_sfl
    from zk_cq_siftproduct t1 
    inner join zk_cq_siftproduct_b t2 on t1.cq_siftproduct_id=t2.cq_siftproduct_id 
    where t1.year='2008' and t1.month >='01' and t1.month <='06' 
        and nvl(t1.dr,0)=0 and nvl(t2.dr,0)=0 
    group by bd_coal_id,outtype