如何用SQL语句根据表2中的分值计算出表1中各字段的合计值?

chengduforest 2006-09-22 11:47:36
表1

ID 字段1 字段2 字段3 字段4
1 A D C A
2 A A D C
3 B C B A
4 C B A B
5 A C B A
6 D A B A

表2

字段1 字段2
A 5
B 4
C 3
D 2

如何用SQL语句根据表2中的分值计算出表1中各字段的合计值?

谢谢!
...全文
254 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengduforest 2006-09-23
  • 打赏
  • 举报
回复
谢谢楼上的各位,结贴!
chengpei_chen 2006-09-22
  • 打赏
  • 举报
回复
那你表1种根据那个字段进行合并?
hellowork 2006-09-22
  • 打赏
  • 举报
回复
declare @t1 table(id int ,f1 varchar(10),f2 varchar(10),f3 varchar(10),f4 varchar(10))
insert @t1
select 1, 'A', 'D', 'C', 'A' union all
select 2, 'A', 'A', 'D', 'C' union all
select 3, 'B', 'C', 'B', 'A' union all
select 4, 'C', 'B', 'A', 'B' union all
select 5, 'A', 'C', 'B', 'A' union all
select 6, 'D', 'A', 'B', 'A'
declare @t2 table(f1 varchar(10),f2 int)
insert @t2
select 'A', 5 union all
select 'B', 4 union all
select 'C', 3 union all
select 'D', 2

select sum(a.f2),sum(b.f2),sum(c.f2),sum(d.f2)
from @t1 t
inner join @t2 a on t.f1 = a.f1
inner join @t2 b on t.f2 = b.f1
inner join @t2 c on t.f3 = c.f1
inner join @t2 d on t.f4 = d.f1

/*结果
24 22 22 27
*/
chengduforest 2006-09-22
  • 打赏
  • 举报
回复
楼上的各位,谢谢回复,我想要的是计算表1字段的纵向合计值,即Sum(字段1),Sum(字段2),而不是横向的字段合计
dawugui 2006-09-22
  • 打赏
  • 举报
回复
select * from 表1 into #t

update #t
set 字段1= 表2.字段2
from #t, 表2
where #t.字段1 = 表2.字段1
update #t
set 字段2= 表2.字段2
from #t, 表2
where #t.字段2 = 表2.字段2
update #t
set 字段3= 表2.字段2
from #t, 表2
where #t.字段3 = 表2.字段2
update #t
set 字段4= 表2.字段2
from #t, 表2
where #t.字段4 = 表2.字段2

select id , 字段1+字段2+字段3+字段4 as total from #t

pengda1i 2006-09-22
  • 打赏
  • 举报
回复
select a.id,b.字段2+c.字段2+d.字段2+e.字段2 as 合计值
from 表1 a,表2 b,表2 c,表2 d,表2 e
where a.字段1=b.字段1
and a.字段2=c.字段1
and a.字段3=d.字段1
and a.字段4=e.字段1


hellowork 2006-09-22
  • 打赏
  • 举报
回复
declare @t1 table(id int ,f1 varchar(10),f2 varchar(10),f3 varchar(10),f4 varchar(10))
insert @t1
select 1, 'A', 'D', 'C', 'A' union all
select 2, 'A', 'A', 'D', 'C' union all
select 3, 'B', 'C', 'B', 'A' union all
select 4, 'C', 'B', 'A', 'B' union all
select 5, 'A', 'C', 'B', 'A' union all
select 6, 'D', 'A', 'B', 'A'
declare @t2 table(f1 varchar(10),f2 int)
insert @t2
select 'A', 5 union all
select 'B', 4 union all
select 'C', 3 union all
select 'D', 2

select t.*, total = a.f2 + b.f2 + c.f2 + d.f2
from @t1 t
inner join @t2 a on t.f1 = a.f1
inner join @t2 b on t.f2 = b.f1
inner join @t2 c on t.f3 = c.f1
inner join @t2 d on t.f4 = d.f1

/*结果
id total
---------------------------
1 15
2 15
3 16
4 16
5 17
6 16
*/
九斤半 2006-09-22
  • 打赏
  • 举报
回复
存储过程不太适合我做的项目,求SQL语句来实现此功能,谢谢!
-----------------------------------------------------------------------------
我写的行不?我的不是存储过程啊!蹭点分真不容易啊!!!
chengduforest 2006-09-22
  • 打赏
  • 举报
回复
存储过程不太适合我做的项目,求SQL语句来实现此功能,谢谢!
chengduforest 2006-09-22
  • 打赏
  • 举报
回复
up
九斤半 2006-09-22
  • 打赏
  • 举报
回复
create table test1(ID int identity(1,1),字段1 char(1),字段2 char(1),字段3 char(1),字段4 char(1))
insert test1(字段1,字段2,字段3,字段4)
select 'A','D','C','A' union all
select 'A','A','D','C' union all
select 'B','C','B','A' union all
select 'C','B','A','B' union all
select 'A','C','B','A' union all
select 'D','A','B','A'
select * from test1

create table test2(字段1 char(1),字段2 int)
insert test2
select 'A',5 union all
select 'B',4 union all
select 'C',3 union all
select 'D',2
--select * from test2

select
sum(a.字段2) as 字段1,
sum(b.字段2) as 字段1,
sum(c.字段2) as 字段1,
sum(d.字段2) as 字段1
from test1 i,test2 a,test2 b,test2 c,test2 d
where i.字段1=a.字段1 and i.字段2=b.字段1 and i.字段3=c.字段1 and i.字段4=d.字段1

drop table test1,test2
chengduforest 2006-09-22
  • 打赏
  • 举报
回复
up
chengduforest 2006-09-22
  • 打赏
  • 举报
回复
chengpei_chen() ( ) 信誉:100 Blog 2006-09-22 12:58:00 得分: 0


那你表1种根据那个字段进行合并?


----------------------------------------------------------------
不合并,要的结果就是Select Sum(表1.字段1的分数值),Sum(表1.字段2的分数值),Sum(表1.字段3的分数值),Sum(表1.字段4的分数值)

34,593

社区成员

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

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