SQL 临时表应用问题。

agoni9091 2011-09-22 09:47:06
select cj.nu,name,age,km,fen from cj,xx where xx.nu=cj.nu --编号,名字,年龄,科目,分数
这是原本的查询语句。现在我想在这些列的后面加上一个分数的和。我用了临时表:

create table #c
(nu varchar(5) null,name varchar(15) null,age varchar(5) null,km varchar(15) null,fen decimal(3,1) null)
insert into #c select cj.nu,name,age,km,fen from cj,xx where xx.nu=cj.nu group by cj.nu,name,age,km,fen
select * from #c
drop table #c --保存原本的查询。

create table #a
(nu varchar(5) null,name varchar(15) null,age varchar(5) null,km varchar(15) null,fen decimal(3,1) null,sun decimal(4,1) null)
insert into #a select nu,name,age,km,fen ,sum(isnull(fen,0))from #c group by nu,name,age,km,fen
select * from #a
drop table #a --增加分数总和列

可就出来的结果并没有对分数进行求和,那个sun(fen)的数据和fen的数据是一样的,求解。
...全文
62 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
agoni9091 2011-09-22
  • 打赏
  • 举报
回复
select cj.nu,name,age,ly,km,fen,(select sum(fen) from cj,xx where xx.nu=cj.nu) from cj,xx where xx.nu=cj.nu

这样又可以了,我晕死。。
agoni9091 2011-09-22
  • 打赏
  • 举报
回复
select cj.nu,name,age,ly,km,fen,(select sum(fen) from cj where xx.nu=cj.nu) from cj,xx where xx.nu=cj.nu 用这个方法弄了下也是一样,但就单独把select sum(fen) from cj,xx where xx.nu=cj.nu拿出来是可以求出总和的,但是到了那里面就不行了,蛋疼, ,
agoni9091 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 acherat 的回复:]
你的fen是哪个表的!
[/Quote]


cj
AcHerat 2011-09-22
  • 打赏
  • 举报
回复
你的fen是哪个表的!
agoni9091 2011-09-22
  • 打赏
  • 举报
回复
都不能啊,不会是我的表有问题的把, ,
-晴天 2011-09-22
  • 打赏
  • 举报
回复
select cj.nu,name,age,km,fen,
(select sum(fen) from cj where nu=xx.nu)zf
from cj,xx where xx.nu=cj.nu
agoni9091 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
SQL code

select a.nu,name,age,km,fen,(select sum(fen) from cj where nu = a.nu) sfen
into #c
from cj a,xx b
where b.nu=a.nu

select *
from #c
[/Quote]

错了,是这个,结果还是一样的
agoni9091 2011-09-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xuam 的回复:]
SQL code
select nu,name,age,km,sum(isnull(fen,0))from #c group by nu,name,age,km
[/Quote]

这个出来的结果还是一样的。 。
--小F-- 2011-09-22
  • 打赏
  • 举报
回复
select
a.nu,name,age,km,fen,(select sum(fen) from cj where nu = a.nu) sfen
into
#c
from
cj a join xx b
on
b.nu=a.nu

select * from #c
AcHerat 2011-09-22
  • 打赏
  • 举报
回复

select a.nu,name,age,km,fen,(select sum(fen) from cj where nu = a.nu) sfen
into #c
from cj a,xx b
where b.nu=a.nu

select *
from #c
xuam 2011-09-22
  • 打赏
  • 举报
回复
select nu,name,age,km,sum(isnull(fen,0))from #c group by nu,name,age,km

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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