假如有一个表A(有n行3列)现在要把
select sum(col001),col(002) 
from a 
where 条件1 

select sum(col001),col(002) from a 
where 条件2 
group by col003
order by col003
这两个查询结果显示到一起,请问怎么关联?如果还有对表a的同样查询,怎么让两次查询并列到查询结果? 帮忙看看,谢谢!

解决方案 »

  1.   

    你的语句本身就有错误啊.
    col(002)是什么啊?
    还有group by 中没有col(002),语句报错.
      

  2.   

    怎么编辑帖子啊,我漏了点假如有一个表A(有n行3列)现在要把  
    select  sum(col001),sum(col002)    
    from  a    
    where  条件1    
    与  
    select  sum(col001),sum(col002)  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003  
    这两个查询结果显示到一起,请问怎么关联?如果还有对表a的同样查询,怎么让两次查询并列到查询结果?  帮忙看看,谢谢!
      

  3.   

    select  sum(col001) as col001,sum(col002) as col002  
    from  a    
    where  条件1    
    union all 
    select  sum(col001),sum(col002)  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003
      

  4.   

    select  sum(col001) s1 ,sum(col002) s2    
    from  a    
    where  条件1    
    union all
    select s1,s2 from 
    (select top 100 percent coloo3,sum(col001) s1,sum(col002) s2  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003) t
      

  5.   

    select * 
    from (     select sum(col001),col(002) 
               from a 
               where 条件1 
               union all 
               select sum(col001),col(002) from a 
               where 条件2 
               group by col003
               order by col003) t
      

  6.   

    xiangjin0106(千载难feng) 的写法我试过了,会提示:如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。
    leo_lesley(leo)的写法会提示:除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
      

  7.   

    --使用了union后,派生表T中如果使用了order by ,前面的字段列表中要加TOP,同时在Order by---子句中出现的列名必须在选择的字段列表中出现(col003)
    select  sum(col001) s1 ,sum(col002) s2    
    from  a    
    where  条件1    
    union all
    select s1,s2 from 
    (select top 100 percent coloo3,sum(col001) s1,sum(col002) s2  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003) t
      

  8.   

    先谢谢 ydage(Ljr) ,你的语句整个执行下来后半部分的查询结果没有按order by排序啊,我的col003是年月 比如200701,整个语句执行后的结果不是按200701,200702,200703,200704月排序的,但是单独执行select top 100 percent coloo3,sum(col001) s1,sum(col002) s2  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003这部分是正常的。
      

  9.   

    我需要的结果是:
    2007之前:col001,col002
    200701:col001,col002
    200702:col001,col002
    200703:col001,col002
    200704:col001,col002
    但现在显示的结果200701-200704的顺序是打乱的,不是按月份排序的,帮忙在看看
      

  10.   

    select sum(col001),col(002) 
    from a 
    where 条件1 
    union all
    select sum(col001),col(002) from a 
    where 条件2 
    group by col003
    order by col003
      

  11.   

    不是简单的查出来排序的问题阿,更不是简单的将所要的两部分union all就能出来的阿
      

  12.   

    ydage(Ljr)的语句已经大概实现了
    select  sum(col001) s1 ,sum(col002) s2    
    from  a    
    where  条件1    
    union all
    select s1,s2 from 
    (select top 100 percent coloo3,sum(col001) s1,sum(col002) s2  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003) t例如我需要的结果是:
    2007之前:col001,col002
    200701:col001,col002
    200702:col001,col002
    200703:col001,col002
    200704:col001,col002
    但现在显示的结果200701开始到200704的顺序是打乱的,不是按月份排序的,帮忙在看看
      

  13.   

    SELECT SUM(col001),SUM(col002) 
    FROM a 
    WHERE (条件1 AND 条件2) 
    GROUP BY col003 
    ORDER BY col003
      

  14.   

    select col003,sum(col001),col(002) from a
    where 条件2
    group by col003
    with rollup
      

  15.   

    --加个order by col003,再试下
    select col003,sum(col001) s1 ,sum(col002) s2    
    from  a    
    where  条件1  
    group by col003  
    union all
    select col003,s1,s2 from 
    (select top 100 percent coloo3,sum(col001) s1,sum(col002) s2  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003) t
    order by col003
      

  16.   

    select  sum(col001) s1 ,sum(col002) s2    
    from  a    
    where  条件1
    group  by  col003  
    order  by  col003    
    union all 
    select sum(col001) s1,sum(col002) s2  from  a    
    where  条件2    
    group  by  col003  
    order  by  col003
    你前个语句也排下序,试下
      

  17.   

    union all后结果集就是个新的集合了,需要重新对新集合中的数排序
    所以我觉得 应该是 select * from (A union all B) order by col ,不知道思路有没错