select  sum(A.)  
FROM  A  
where  A.qx_riqi  between  to_date('2007-8-1',  'yyyy-mm-dd')  and  
             to_date('2007-8-30',  'yyyy-mm-dd')  
and  A.id  ='0075'  
union  
SELECT  SUM(B.piao_)  
From    B  
WHERE    B.piao_riqi  between  to_date('2007-8-1',  'yyyy-mm-dd')  and  
               to_date('2007-8-30',  'yyyy-mm-dd')  
and    B.id  ='0075'  
所得的结果是  
------------  
 sum(A.)  
1: 0.1
2: 0.9
--------
实际上我想要的结果是
   sum(A.)  sum(b.)
1:  0.1            0.9
请问怎样修改这个sql语句呢?两个select语句的时间参数,和id是一样的

解决方案 »

  1.   

    这样写就可以了select *
      from (select sum(A.)
              FROM A
             where A.qx_riqi between to_date('2007-8-1', 'yyyy-mm-dd') and
                   to_date('2007-8-30', 'yyyy-mm-dd')
               and A.id = '0075'),
           (SELECT SUM(B.piao_)
              From B
             WHERE B.piao_riqi between to_date('2007-8-1', 'yyyy-mm-dd') and
                   to_date('2007-8-30', 'yyyy-mm-dd')
               and B.id = '0075');
      

  2.   

    在此基础上再处理一下:
    select  decode(flag,'a',sum_val,0) val1,
            decode(flag,'b',sum_val,0) val2
      from (
            select  sum(A.) sum_val ,'a' flag
            FROM  A  
            where  A.qx_riqi  between  to_date('2007-8-1',  'yyyy-mm-dd')  and  
                         to_date('2007-8-30',  'yyyy-mm-dd')  
            and  A.id  ='0075'  
            union  all
            SELECT  SUM(B.piao_)  sum_val, 'b' flag
            From    B  
            WHERE    B.piao_riqi  between  to_date('2007-8-1',  'yyyy-mm-dd')  and  
                           to_date('2007-8-30',  'yyyy-mm-dd')  
            and    B.id  ='0075'  
            )
      

  3.   

    to yugas:您的方法我试了试结果是:
    val1  val2
    0.07   0
    0      0.2
    枫叶的方法倒是可行,继续等待其他方法
      

  4.   

    select (select  sum(A.)  
    FROM  A  
    where  A.qx_riqi  between  to_date('2007-8-1',  'yyyy-mm-dd')  and  
                 to_date('2007-8-30',  'yyyy-mm-dd')  
    and  A.id  ='0075') as sum(A.),
    (SELECT  SUM(B.piao_) 
    From    B  
    WHERE    B.piao_riqi  between  to_date('2007-8-1',  'yyyy-mm-dd')  and  
                   to_date('2007-8-30',  'yyyy-mm-dd')  
    and    B.id  ='0075') as SUM(B.piao_) from dual