假如有一个表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的同样查询,怎么让两次查询并列到查询结果? 帮忙看看,谢谢!
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的同样查询,怎么让两次查询并列到查询结果? 帮忙看看,谢谢!
col(002)是什么啊?
还有group by 中没有col(002),语句报错.
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的同样查询,怎么让两次查询并列到查询结果? 帮忙看看,谢谢!
from a
where 条件1
union all
select sum(col001),sum(col002) from a
where 条件2
group by col003
order by col003
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
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
leo_lesley(leo)的写法会提示:除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
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
where 条件2
group by col003
order by col003这部分是正常的。
2007之前:col001,col002
200701:col001,col002
200702:col001,col002
200703:col001,col002
200704:col001,col002
但现在显示的结果200701-200704的顺序是打乱的,不是按月份排序的,帮忙在看看
from a
where 条件1
union all
select sum(col001),col(002) from a
where 条件2
group by col003
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例如我需要的结果是:
2007之前:col001,col002
200701:col001,col002
200702:col001,col002
200703:col001,col002
200704:col001,col002
但现在显示的结果200701开始到200704的顺序是打乱的,不是按月份排序的,帮忙在看看
FROM a
WHERE (条件1 AND 条件2)
GROUP BY col003
ORDER BY col003
where 条件2
group by col003
with rollup
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
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
你前个语句也排下序,试下
所以我觉得 应该是 select * from (A union all B) order by col ,不知道思路有没错