hrb133yqq() ( ) 信誉:100 Blog 2006-12-31 16:09:55 得分: 0
create table table2(col1 int, col2 int)
insert into table2(col1,col2)
select 1, 4 union all
select 3, 6 union all
select 4, 6
select count(col1) as a ,sum(col2) as b from table2 where col1 between 1 and 2
union
select count(col1),sum(col2) from table2 where col1 between 3 and 4
用表变量@ta测试:
--把表变量@ta改为表名就行了
declare @ta table (A int, B int)
insert @ta
select 1, 4
union all select 3, 6
union all select 4, 6
select result=case when a between 1 and 2 then 1 when a between 3 and 4 then 2 end,
r_sum=sum(b)
from @ta
group by case when a between 1 and 2 then 1 when a between 3 and 4 then 2 end
用表变量@ta测试:
declare @ta table (A int, B int)
insert @ta
select 1, 4
union all select 3, 6
union all select 4, 6
select a=case when a between 1 and 2 then 1 when a between 3 and 4 then 2 end,
b=sum(b)
from @ta
group by case when a between 1 and 2 then 1 when a between 3 and 4 then 2 end
insert into table2(col1,col2)
select 1, 4 union all
select 3, 6 union all
select 4, 6
select count(col1) as a ,sum(col2) as b from table2 where col1 between 1 and 2
union
select count(col1),sum(col2) from table2 where col1 between 3 and 4
select result,sum(b) as r_sum from(select result=(case when a between 0 and 1 then 1 end),b from @a where a between 0 and 1)a
group by result
union all
select result,sum(b) as r_sum from(select result=(case when a between 3 and 4 then 2 end),b from @a where a between 3 and 4)b
group by result
select result=(case when a between 0 and 1 then 1 end),r_sum=sum(b) from tablename where a between 0 and 1
~~~~~~~~这个不能写死, 因为它也是根据条件计算出来的.
union all
select result=(case when a between 3 and 4 then 2 end),r_sum=sum(b) from tablename where a between 3 and 4
select result=1,r_sum=sum(b) from tablename where a between 0 and 1
~~~~~~~~这个不能写死, 因为它也是根据条件计算出来的.
union all
select result=2,r_sum=sum(b) from tablename where a between 3 and 4