现在的表是这样的
------------------------
id c1 c2
1 1 2
2 2 3
3 3 4
4 1 2
5 2 3
------------------------
比如我要对c1,c2两列作汇总,或者是记数,然后把结果放在结果集里面,我现在的作法好象只能用union,比如作汇总select id,
c1,
c2
from table1 t
union
select '-1' id,
sum (tt.c1) c1,
sum(tt.c2) c2
from table tt
结果是------------------------
id c1 c2
-1 9 14
1 1 2
2 2 3
3 3 4
4 1 2
5 2 3
------------------------*************************************************如果是记数的话,我要统计c1列中1的个数,c2列中4的个数,
select id,
c1,
c2
from table t
union
select '-1' id,
count(decode(tt.c1,1,'x',null)) c1,
count(decode(tt.c2,4,'x',null)) c2
from table tt结果是------------------------
id c1 c2
-1 2 1
1 1 2
2 2 3
3 3 4
4 1 2
5 2 3
------------------------**************************************有没有更简单的SQL语句啊,最好是只对table查一次的,而且不要用union操作,谢谢了!
------------------------
id c1 c2
1 1 2
2 2 3
3 3 4
4 1 2
5 2 3
------------------------
比如我要对c1,c2两列作汇总,或者是记数,然后把结果放在结果集里面,我现在的作法好象只能用union,比如作汇总select id,
c1,
c2
from table1 t
union
select '-1' id,
sum (tt.c1) c1,
sum(tt.c2) c2
from table tt
结果是------------------------
id c1 c2
-1 9 14
1 1 2
2 2 3
3 3 4
4 1 2
5 2 3
------------------------*************************************************如果是记数的话,我要统计c1列中1的个数,c2列中4的个数,
select id,
c1,
c2
from table t
union
select '-1' id,
count(decode(tt.c1,1,'x',null)) c1,
count(decode(tt.c2,4,'x',null)) c2
from table tt结果是------------------------
id c1 c2
-1 2 1
1 1 2
2 2 3
3 3 4
4 1 2
5 2 3
------------------------**************************************有没有更简单的SQL语句啊,最好是只对table查一次的,而且不要用union操作,谢谢了!
sum(c1) as c1,sum(c2) c2
from table group by rollup(id)