请问怎么实现这个功能?

s_hluo 2006-12-31 02:47:08
假如我有这样一个表, 有两列: A, B. 都为int型

A B
1 4
3 6
4 6
...

我想实现统计A列, 统计条件是 0=< C<=1 , 3=<D<=4, 现在要统计A列中符合C, D条件的值, 并把符合C,D条件对应的B列和求出来, 结果类似如下:
result r_sum
1 4
2 12


请问应该怎么实现? SQL语句应该怎么写? 谢谢.
...全文
199 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
borcas2010 2007-01-02
  • 打赏
  • 举报
回复
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



这好象才是楼主想要的:)
marco08 2007-01-02
  • 打赏
  • 举报
回复
create table T(A int, B int)
insert T select 1, 4
union all select 3, 6
union all select 4, 6

select result=1, r_sum=sum(B) from T
where A between 0 and 1
union all
select 2, sum(B) from T
where A between 3 and 4

--result
result r_sum
----------- -----------
1 4
2 12

(2 row(s) affected)
InFerNaL_LioN 2007-01-02
  • 打赏
  • 举报
回复
create table #temp
(
A int,
B int
)

insert into #temp values(1,4)
insert into #temp values(3,6)
insert into #temp values(4,6)


select 1,
(
select sum(B) total from #temp
where A>=0
and A<=1
)
union all
select 2,
(
select sum(B) total from #temp
where A>=3
and A<=4
)
Well 2007-01-02
  • 打赏
  • 举报
回复
union all进行联合
中国风 2006-12-31
  • 打赏
  • 举报
回复
用表变量@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

(所影响的行数为 3 行)

result r_sum
----------- -----------
1 4
2 12

(所影响的行数为 2 行)

中国风 2006-12-31
  • 打赏
  • 举报
回复
用表变量@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

(所影响的行数为 3 行)

a b
----------- -----------
1 4
2 12

(所影响的行数为 2 行)

hrb133yqq 2006-12-31
  • 打赏
  • 举报
回复
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
caixia615 2006-12-31
  • 打赏
  • 举报
回复
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
akuzou 2006-12-31
  • 打赏
  • 举报
回复
楼上的语法估计有问题,你建个表试试看
caixia615 2006-12-31
  • 打赏
  • 举报
回复
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
s_hluo 2006-12-31
  • 打赏
  • 举报
回复
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
caixia615 2006-12-31
  • 打赏
  • 举报
回复
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
fellowcheng 2006-12-31
  • 打赏
  • 举报
回复
一楼
caixia615 2006-12-31
  • 打赏
  • 举报
回复
select result=1,r_sum=sum(b) from tablename where a between 1 and 2
union all
select result=2,r_sum=sum(b) from tablename where a between 3 and 4
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧